1515 */
1616package com .dataliquid .maven .distribution .verifier .mojo ;
1717
18- import org .apache .maven .plugin .MojoExecutionException ;
1918import org .apache .maven .plugin .testing .AbstractMojoTestCase ;
20- import org .apache .maven .plugin .testing .MojoRule ;
21- import org .junit .Rule ;
22- import org .junit .Test ;
23- import org .junit .Before ;
24- import org .junit .After ;
25- import org .junit .rules .TemporaryFolder ;
2619import org .apache .commons .io .FileUtils ;
27- import org .xmlunit .builder .DiffBuilder ;
28- import org .xmlunit .diff .Diff ;
29- import org .xmlunit .diff .Difference ;
3020
3121import java .io .File ;
32- import java .util .Iterator ;
3322
3423/**
35- * Integration test for GenerateMojo
24+ * Integration test for generate goal functionality
3625 */
3726public class GenerateMojoIT extends AbstractMojoTestCase {
3827
39- @ Rule
40- public MojoRule mojoRule = new MojoRule ();
41-
42- @ Rule
43- public TemporaryFolder temporaryFolder = new TemporaryFolder ();
44-
45- private File testResourcesDir ;
46-
47- @ Before
48- public void setUp () throws Exception {
28+ @ Override
29+ protected void setUp () throws Exception {
4930 super .setUp ();
50- testResourcesDir = new File ("src/test/resources" );
51- }
52-
53- @ After
54- public void tearDown () throws Exception {
55- super .tearDown ();
5631 }
5732
58- @ Test
5933 public void testGenerateWhitelist () throws Exception {
60- GenerateMojo mojo = new GenerateMojo ();
61-
62- File distributionFile = new File (testResourcesDir , "generate-whitelist/generate_whitelist.zip" );
63- File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
34+ // Create test POM
35+ File basedir = new File (getBasedir ());
36+ File testPom = createTestPom ("generate-whitelist-test" ,
37+ basedir + "/src/test/resources/generate-whitelist/generate_whitelist.zip" ,
38+ basedir + "/target/generated-whitelist.xml" );
6439
65- mojo . setDistributionArchiveFile ( distributionFile );
66- mojo . setWhitelist ( outputFile );
40+ GenerateMojo mojo = ( GenerateMojo ) lookupMojo ( "generate" , testPom );
41+ assertNotNull ( mojo );
6742
68- // Execute the generation
43+ // Execute mojo
6944 mojo .execute ();
7045
7146 // Verify the file was created
47+ File outputFile = new File (basedir , "target/generated-whitelist.xml" );
7248 assertTrue ("Whitelist file should be created" , outputFile .exists ());
7349
7450 // Verify the content contains expected structure
@@ -77,151 +53,44 @@ public void testGenerateWhitelist() throws Exception {
7753 assertTrue ("Should contain entry elements" , content .contains ("<entry" ));
7854 assertTrue ("Should contain path attributes" , content .contains ("path=" ));
7955 assertTrue ("Should contain md5 attributes" , content .contains ("md5=" ));
80-
81- // Verify XML is well-formed
82- assertNotNull ("Should be able to parse XML" , parseXml (outputFile ));
83- }
84-
85- @ Test
86- public void testGenerateWhitelistWithTemplateComparison () throws Exception {
87- GenerateMojo mojo = new GenerateMojo ();
88-
89- File distributionFile = new File (testResourcesDir , "generate-whitelist/generate_whitelist.zip" );
90- File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
91- File templateFile = new File (testResourcesDir , "generate-whitelist/whitelist.tmpl.xml" );
92-
93- mojo .setDistributionArchiveFile (distributionFile );
94- mojo .setWhitelist (outputFile );
95-
96- // Execute the generation
97- mojo .execute ();
98-
99- // Read both files
100- String generatedContent = FileUtils .readFileToString (outputFile , "UTF-8" );
101- String templateContent = FileUtils .readFileToString (templateFile , "UTF-8" );
102-
103- // Both should have similar structure (though MD5s might differ)
104- assertTrue ("Generated content should contain whitelist element" , generatedContent .contains ("<whitelist>" ));
105- assertTrue ("Template content should contain whitelist element" , templateContent .contains ("<whitelist>" ));
106-
107- // Count entries - should be similar
108- int generatedEntries = countOccurrences (generatedContent , "<entry" );
109- int templateEntries = countOccurrences (templateContent , "<entry" );
110- assertTrue ("Should have at least one entry" , generatedEntries > 0 );
111-
112- // Use XMLUnit to compare structure (ignoring MD5 values)
113- Diff diff = DiffBuilder .compare (templateContent )
114- .withTest (generatedContent )
115- .ignoreWhitespace ()
116- .ignoreComments ()
117- .checkForSimilar ()
118- .withAttributeFilter (attr -> !"md5" .equals (attr .getName ()))
119- .build ();
120-
121- assertFalse ("XML structures should be similar (ignoring md5 values)" , diff .hasDifferences ());
122- }
123-
124- @ Test (expected = MojoExecutionException .class )
125- public void testGenerateWhitelistMissingDistribution () throws Exception {
126- GenerateMojo mojo = new GenerateMojo ();
127-
128- File distributionFile = new File (testResourcesDir , "non-existent.zip" );
129- File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
130-
131- mojo .setDistributionArchiveFile (distributionFile );
132- mojo .setWhitelist (outputFile );
133-
134- // This should throw MojoExecutionException
135- mojo .execute ();
136- }
137-
138- @ Test
139- public void testGenerateWhitelistCreatesParentDirectory () throws Exception {
140- GenerateMojo mojo = new GenerateMojo ();
141-
142- File distributionFile = new File (testResourcesDir , "generate-whitelist/generate_whitelist.zip" );
143- File outputFile = new File (temporaryFolder .getRoot (), "subdir/nested/generated-whitelist.xml" );
144-
145- assertFalse ("Parent directory should not exist initially" , outputFile .getParentFile ().exists ());
146-
147- mojo .setDistributionArchiveFile (distributionFile );
148- mojo .setWhitelist (outputFile );
149-
150- // Execute the generation
151- mojo .execute ();
152-
153- // Verify the directory structure was created
154- assertTrue ("Parent directory should be created" , outputFile .getParentFile ().exists ());
155- assertTrue ("Whitelist file should be created" , outputFile .exists ());
156- }
157-
158- @ Test
159- public void testGenerateWhitelistOverwritesExisting () throws Exception {
160- GenerateMojo mojo = new GenerateMojo ();
161-
162- File distributionFile = new File (testResourcesDir , "generate-whitelist/generate_whitelist.zip" );
163- File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
164-
165- // Create an existing file with different content
166- FileUtils .writeStringToFile (outputFile , "<whitelist><entry path=\" /test\" /></whitelist>" , "UTF-8" );
167- assertTrue ("Pre-existing file should exist" , outputFile .exists ());
168-
169- long originalLength = outputFile .length ();
170-
171- mojo .setDistributionArchiveFile (distributionFile );
172- mojo .setWhitelist (outputFile );
173-
174- // Execute the generation
175- mojo .execute ();
176-
177- // Verify the file was overwritten
178- assertTrue ("Whitelist file should still exist" , outputFile .exists ());
179- assertFalse ("File content should have changed" , originalLength == outputFile .length ());
180-
181- String content = FileUtils .readFileToString (outputFile , "UTF-8" );
182- assertFalse ("Should not contain test entry" , content .contains ("path=\" /test\" " ));
183- }
184-
185- @ Test
186- public void testGenerateWhitelistWithEmptyZip () throws Exception {
187- // Create an empty zip file
188- File emptyZip = new File (temporaryFolder .getRoot (), "empty.zip" );
189- org .zeroturnaround .zip .ZipUtil .packEntries (new File [0 ], emptyZip );
190-
191- GenerateMojo mojo = new GenerateMojo ();
192- File outputFile = new File (temporaryFolder .getRoot (), "empty-whitelist.xml" );
193-
194- mojo .setDistributionArchiveFile (emptyZip );
195- mojo .setWhitelist (outputFile );
196-
197- // Execute the generation
198- mojo .execute ();
199-
200- // Verify the file was created with empty whitelist
201- assertTrue ("Whitelist file should be created" , outputFile .exists ());
202-
203- String content = FileUtils .readFileToString (outputFile , "UTF-8" );
204- assertTrue ("Should contain whitelist root element" , content .contains ("<whitelist>" ));
205- assertTrue ("Should contain closing whitelist tag" , content .contains ("</whitelist>" ));
206- assertFalse ("Should not contain entry elements" , content .contains ("<entry" ));
207- }
208-
209- private int countOccurrences (String str , String findStr ) {
210- int lastIndex = 0 ;
211- int count = 0 ;
212- while (lastIndex != -1 ) {
213- lastIndex = str .indexOf (findStr , lastIndex );
214- if (lastIndex != -1 ) {
215- count ++;
216- lastIndex += findStr .length ();
217- }
218- }
219- return count ;
22056 }
22157
222- private org .w3c .dom .Document parseXml (File xmlFile ) throws Exception {
223- javax .xml .parsers .DocumentBuilderFactory dbFactory = javax .xml .parsers .DocumentBuilderFactory .newInstance ();
224- javax .xml .parsers .DocumentBuilder dBuilder = dbFactory .newDocumentBuilder ();
225- return dBuilder .parse (xmlFile );
58+ private File createTestPom (String artifactId , String distributionFile , String outputFile ) throws Exception {
59+ String pomXml = "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
60+ "<project xmlns=\" http://maven.apache.org/POM/4.0.0\" \n " +
61+ " xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" \n " +
62+ " xsi:schemaLocation=\" http://maven.apache.org/POM/4.0.0\n " +
63+ " http://maven.apache.org/xsd/maven-4.0.0.xsd\" >\n " +
64+ " <modelVersion>4.0.0</modelVersion>\n " +
65+ " <groupId>com.dataliquid.test</groupId>\n " +
66+ " <artifactId>" + artifactId + "</artifactId>\n " +
67+ " <version>1.0.0</version>\n " +
68+ " <build>\n " +
69+ " <directory>${basedir}/target</directory>\n " +
70+ " <plugins>\n " +
71+ " <plugin>\n " +
72+ " <groupId>com.dataliquid.maven</groupId>\n " +
73+ " <artifactId>distribution-verifier-maven-plugin</artifactId>\n " +
74+ " <version>1.0.4-SNAPSHOT</version>\n " +
75+ " <configuration>\n " +
76+ " <distributionArchiveFile>" + distributionFile + "</distributionArchiveFile>\n " +
77+ " <whitelist>" + outputFile + "</whitelist>\n " +
78+ " </configuration>\n " +
79+ " <executions>\n " +
80+ " <execution>\n " +
81+ " <goals>\n " +
82+ " <goal>generate</goal>\n " +
83+ " </goals>\n " +
84+ " </execution>\n " +
85+ " </executions>\n " +
86+ " </plugin>\n " +
87+ " </plugins>\n " +
88+ " </build>\n " +
89+ "</project>" ;
90+
91+ File pom = new File (getBasedir (), "target/test-" + artifactId + "-pom.xml" );
92+ pom .getParentFile ().mkdirs ();
93+ FileUtils .writeStringToFile (pom , pomXml , "UTF-8" );
94+ return pom ;
22695 }
22796}
0 commit comments