|
| 1 | +package org.utpsql.maven.plugin.test; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.apache.commons.io.FileUtils; |
| 12 | +import org.apache.maven.it.VerificationException; |
| 13 | +import org.apache.maven.it.Verifier; |
| 14 | +import org.apache.maven.it.util.ResourceExtractor; |
| 15 | +import org.apache.maven.model.Model; |
| 16 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 17 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class UtPLSQLMojoIT |
| 23 | +{ |
| 24 | + |
| 25 | + private static String pluginVersion = null; |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void setUp() throws VerificationException, IOException, XmlPullParserException { |
| 29 | + // Read plugin pom file |
| 30 | + MavenXpp3Reader reader = new MavenXpp3Reader(); |
| 31 | + Model model = reader.read(new FileReader("../pom.xml")); |
| 32 | + |
| 33 | + File pomFile = new File("../"); |
| 34 | + Verifier verifier = new Verifier(pomFile.getAbsolutePath()); |
| 35 | + |
| 36 | + // delete plugin |
| 37 | + //verifier.deleteArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), "pom" ); |
| 38 | + |
| 39 | + // install plugin |
| 40 | + verifier.setAutoclean(false); |
| 41 | + verifier.addCliOption("-Dmaven.skip.test=true"); |
| 42 | + verifier.addCliOption("-DskipITs"); |
| 43 | + verifier.executeGoal("install"); |
| 44 | + |
| 45 | + pluginVersion = model.getVersion(); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testSimpleDefinition() throws Exception |
| 51 | + { |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + final String PROJECT_NAME = "simple-project"; |
| 56 | + File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/"+ PROJECT_NAME); |
| 57 | + |
| 58 | + Verifier verifier; |
| 59 | + verifier = new Verifier(testProject.getAbsolutePath()); |
| 60 | + verifier.addCliOption("-N"); |
| 61 | + verifier.addCliOption("-Dutplsql-maven-plugin-version="+pluginVersion); |
| 62 | + verifier.addCliOption("-DdbUrl="+System.getProperty("dbUrl")); |
| 63 | + verifier.addCliOption("-DdbUser="+System.getProperty("dbUser")); |
| 64 | + verifier.addCliOption("-DdbPass="+System.getProperty("dbPass")); |
| 65 | + |
| 66 | + verifier.executeGoal("test"); |
| 67 | + |
| 68 | + checkReportsGenerated(PROJECT_NAME,"utplsql/sonar-test-reporter.xml","utplsql/coverage-sonar-reporter.xml"); |
| 69 | + } |
| 70 | + catch (Exception e) |
| 71 | + { |
| 72 | + e.printStackTrace(); |
| 73 | + Assert.fail("Unexpected Exception running the test of Definition "+e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRegexDefinition() throws Exception |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + final String PROJECT_NAME = "regex-project"; |
| 83 | + File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/"+ PROJECT_NAME); |
| 84 | + |
| 85 | + Verifier verifier; |
| 86 | + verifier = new Verifier(testProject.getAbsolutePath()); |
| 87 | + verifier.addCliOption("-N"); |
| 88 | + verifier.addCliOption("-Dutplsql-maven-plugin-version="+pluginVersion); |
| 89 | + verifier.addCliOption("-DdbUrl="+System.getProperty("dbUrl")); |
| 90 | + verifier.addCliOption("-DdbUser="+System.getProperty("dbUser")); |
| 91 | + verifier.addCliOption("-DdbPass="+System.getProperty("dbPass")); |
| 92 | + |
| 93 | + verifier.executeGoal("test"); |
| 94 | + |
| 95 | + checkReportsGenerated(PROJECT_NAME,"utplsql/sonar-test-reporter.xml","utplsql/coverage-sonar-reporter.xml"); |
| 96 | + } |
| 97 | + catch (Exception e) |
| 98 | + { |
| 99 | + e.printStackTrace(); |
| 100 | + Assert.fail("Unexpected Exception running the test of Definition"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testTypeMappingDefinition() throws Exception |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + final String PROJECT_NAME = "type-mapping-project"; |
| 110 | + File testProject = ResourceExtractor.simpleExtractResources(getClass(), "/"+ PROJECT_NAME); |
| 111 | + |
| 112 | + Verifier verifier; |
| 113 | + verifier = new Verifier(testProject.getAbsolutePath()); |
| 114 | + verifier.addCliOption("-N"); |
| 115 | + verifier.addCliOption("-Dutplsql-maven-plugin-version="+pluginVersion); |
| 116 | + verifier.addCliOption("-DdbUrl="+System.getProperty("dbUrl")); |
| 117 | + verifier.addCliOption("-DdbUser="+System.getProperty("dbUser")); |
| 118 | + verifier.addCliOption("-DdbPass="+System.getProperty("dbPass")); |
| 119 | + |
| 120 | + verifier.executeGoal("test"); |
| 121 | + |
| 122 | + checkReportsGenerated(PROJECT_NAME,"utplsql/sonar-test-reporter.xml","utplsql/coverage-sonar-reporter.xml"); |
| 123 | + } |
| 124 | + catch (Exception e) |
| 125 | + { |
| 126 | + e.printStackTrace(); |
| 127 | + Assert.fail("Unexpected Exception running the test of Definition"); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * |
| 133 | + * @param files |
| 134 | + */ |
| 135 | + private void checkReportsGenerated(String projectName, String... files) |
| 136 | + { |
| 137 | + for (String filename : files) |
| 138 | + { |
| 139 | + File outputFile = new File("target/test-classes/"+projectName+"/target",filename); |
| 140 | + File expectedOutputFile = new File("target/test-classes/"+projectName+"/expected-output", filename); |
| 141 | + |
| 142 | + Assert.assertTrue("The reporter for " + filename + " was not generated", outputFile.exists()); |
| 143 | + try { |
| 144 | + // Duration is set to 1 before comparing contents as it is always different. |
| 145 | + // Path separator is set to "/" to ensure windows / linux / mac compatibility |
| 146 | + Stream<String> stream = Files.lines(Paths.get("target","test-classes",projectName,"target",filename)); |
| 147 | + String outputContent = stream.map(line -> line.replaceAll("(duration=\"[0-9\\.]*\")", "duration=\"1\"")) |
| 148 | + .map(line -> line.replaceAll("\\\\", "/")) |
| 149 | + .map(line -> line.replaceAll("\r", "").replaceAll("\n", "")) |
| 150 | + .collect(Collectors.joining("\n")); |
| 151 | + |
| 152 | + stream.close(); |
| 153 | + Assert.assertEquals("The files differ!", |
| 154 | + outputContent, |
| 155 | + FileUtils.readFileToString(expectedOutputFile, "utf-8").replace("\r","")); |
| 156 | + } catch (IOException e) { |
| 157 | + // TODO Auto-generated catch block |
| 158 | + e.printStackTrace(); |
| 159 | + Assert.fail("Unexpected Exception running the test of Definition"); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments