|
|
|
@ -1,49 +1,37 @@
|
|
|
|
|
package eu.dnetlib.maven.plugin.properties;
|
|
|
|
|
|
|
|
|
|
import static eu.dnetlib.maven.plugin.properties.WritePredefinedProjectProperties.PROPERTY_PREFIX_ENV;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import static org.mockito.Mockito.doReturn;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import org.apache.maven.plugin.MojoExecutionException;
|
|
|
|
|
import org.apache.maven.project.MavenProject;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.rules.TemporaryFolder;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import static eu.dnetlib.maven.plugin.properties.WritePredefinedProjectProperties.PROPERTY_PREFIX_ENV;
|
|
|
|
|
import static junit.framework.Assert.*;
|
|
|
|
|
import static org.mockito.Mockito.doReturn;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author mhorst
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
|
public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public TemporaryFolder testFolder = new TemporaryFolder();
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private MavenProject mavenProject;
|
|
|
|
|
|
|
|
|
|
private WritePredefinedProjectProperties mojo;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void init() {
|
|
|
|
|
@BeforeAll
|
|
|
|
|
public void init(@TempDir File testFolder) {
|
|
|
|
|
mojo = new WritePredefinedProjectProperties();
|
|
|
|
|
mojo.outputFile = getPropertiesFileLocation();
|
|
|
|
|
mojo.outputFile = getPropertiesFileLocation(testFolder);
|
|
|
|
|
mojo.project = mavenProject;
|
|
|
|
|
doReturn(new Properties()).when(mavenProject).getProperties();
|
|
|
|
|
}
|
|
|
|
@ -82,14 +70,14 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=MojoExecutionException.class)
|
|
|
|
|
public void testExecuteWithProjectPropertiesAndInvalidOutputFile() throws Exception {
|
|
|
|
|
public void testExecuteWithProjectPropertiesAndInvalidOutputFile(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
String key = "projectPropertyKey";
|
|
|
|
|
String value = "projectPropertyValue";
|
|
|
|
|
Properties projectProperties = new Properties();
|
|
|
|
|
projectProperties.setProperty(key, value);
|
|
|
|
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
|
|
|
|
mojo.outputFile = testFolder.getRoot();
|
|
|
|
|
mojo.outputFile = testFolder;
|
|
|
|
|
|
|
|
|
|
// execute
|
|
|
|
|
mojo.execute();
|
|
|
|
@ -144,7 +132,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromFile() throws Exception {
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromFile(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
String key = "projectPropertyKey";
|
|
|
|
|
String value = "projectPropertyValue";
|
|
|
|
@ -155,7 +143,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
projectProperties.setProperty(includedKey, includedValue);
|
|
|
|
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
|
|
|
|
|
|
|
|
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.properties");
|
|
|
|
|
File includedPropertiesFile = new File(testFolder, "included.properties");
|
|
|
|
|
Properties includedProperties = new Properties();
|
|
|
|
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
|
|
|
|
includedProperties.store(new FileWriter(includedPropertiesFile), null);
|
|
|
|
@ -217,7 +205,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromXmlFile() throws Exception {
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromXmlFile(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
String key = "projectPropertyKey";
|
|
|
|
|
String value = "projectPropertyValue";
|
|
|
|
@ -228,7 +216,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
projectProperties.setProperty(includedKey, includedValue);
|
|
|
|
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
|
|
|
|
|
|
|
|
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.xml");
|
|
|
|
|
File includedPropertiesFile = new File(testFolder, "included.xml");
|
|
|
|
|
Properties includedProperties = new Properties();
|
|
|
|
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
|
|
|
|
includedProperties.storeToXML(new FileOutputStream(includedPropertiesFile), null);
|
|
|
|
@ -247,7 +235,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=MojoExecutionException.class)
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromInvalidXmlFile() throws Exception {
|
|
|
|
|
public void testExecuteIncludingPropertyKeysFromInvalidXmlFile(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
String key = "projectPropertyKey";
|
|
|
|
|
String value = "projectPropertyValue";
|
|
|
|
@ -258,7 +246,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
projectProperties.setProperty(includedKey, includedValue);
|
|
|
|
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
|
|
|
|
|
|
|
|
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.xml");
|
|
|
|
|
File includedPropertiesFile = new File(testFolder, "included.xml");
|
|
|
|
|
Properties includedProperties = new Properties();
|
|
|
|
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
|
|
|
|
includedProperties.store(new FileOutputStream(includedPropertiesFile), null);
|
|
|
|
@ -294,7 +282,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testExecuteWithEnvironmentProperties() throws Exception {
|
|
|
|
|
public void testExecuteWithEnvironmentProperties(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
mojo.setIncludeEnvironmentVariables(true);
|
|
|
|
|
|
|
|
|
@ -303,7 +291,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
assertTrue(mojo.outputFile.exists());
|
|
|
|
|
Properties storedProperties = getStoredProperties();
|
|
|
|
|
Properties storedProperties = getStoredProperties(testFolder);
|
|
|
|
|
assertTrue(storedProperties.size() > 0);
|
|
|
|
|
for (Object currentKey : storedProperties.keySet()) {
|
|
|
|
|
assertTrue(((String)currentKey).startsWith(PROPERTY_PREFIX_ENV));
|
|
|
|
@ -330,7 +318,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testExecuteWithSystemPropertiesAndEscapeChars() throws Exception {
|
|
|
|
|
public void testExecuteWithSystemPropertiesAndEscapeChars(@TempDir File testFolder) throws Exception {
|
|
|
|
|
// given
|
|
|
|
|
String key = "systemPropertyKey ";
|
|
|
|
|
String value = "systemPropertyValue";
|
|
|
|
@ -344,7 +332,7 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
assertTrue(mojo.outputFile.exists());
|
|
|
|
|
Properties storedProperties = getStoredProperties();
|
|
|
|
|
Properties storedProperties = getStoredProperties(testFolder);
|
|
|
|
|
assertTrue(storedProperties.size() > 0);
|
|
|
|
|
assertFalse(storedProperties.containsKey(key));
|
|
|
|
|
assertTrue(storedProperties.containsKey(key.trim()));
|
|
|
|
@ -353,13 +341,13 @@ public class WritePredefinedProjectPropertiesTest {
|
|
|
|
|
|
|
|
|
|
// ----------------------------------- PRIVATE -------------------------------------------
|
|
|
|
|
|
|
|
|
|
private File getPropertiesFileLocation() {
|
|
|
|
|
return new File(testFolder.getRoot(), "test.properties");
|
|
|
|
|
private File getPropertiesFileLocation(File testFolder) {
|
|
|
|
|
return new File(testFolder, "test.properties");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Properties getStoredProperties() throws FileNotFoundException, IOException {
|
|
|
|
|
private Properties getStoredProperties(File testFolder) throws FileNotFoundException, IOException {
|
|
|
|
|
Properties properties = new Properties();
|
|
|
|
|
properties.load(new FileInputStream(getPropertiesFileLocation()));
|
|
|
|
|
properties.load(new FileInputStream(getPropertiesFileLocation(testFolder)));
|
|
|
|
|
return properties;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|