1+ /*
2+ * Copyright © 2019 dataliquid GmbH | www.dataliquid.com
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .dataliquid .maven .distribution .verifier .mojo ;
17+
18+ import org .junit .Test ;
19+ import org .junit .Rule ;
20+ import org .junit .rules .TemporaryFolder ;
21+ import java .io .File ;
22+ import org .apache .commons .io .FileUtils ;
23+
24+ import static org .junit .Assert .*;
25+
26+ /**
27+ * Integration test for GenerateMojo
28+ */
29+ public class GenerateMojoIT {
30+
31+ @ Rule
32+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
33+
34+ @ Test
35+ public void testGenerateMojoInstantiation () throws Exception {
36+ GenerateMojo mojo = new GenerateMojo ();
37+ assertNotNull ("Mojo should be instantiated" , mojo );
38+ }
39+
40+ @ Test
41+ public void testGenerateWhitelist () throws Exception {
42+ GenerateMojo mojo = new GenerateMojo ();
43+
44+ File distributionFile = new File ("src/test/resources/generate-whitelist/generate_whitelist.zip" );
45+ File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
46+
47+ mojo .setDistributionArchiveFile (distributionFile );
48+ mojo .setWhitelist (outputFile );
49+
50+ // Execute the generation
51+ mojo .execute ();
52+
53+ // Verify the file was created
54+ assertTrue ("Whitelist file should be created" , outputFile .exists ());
55+
56+ // Verify the content contains expected structure
57+ String content = FileUtils .readFileToString (outputFile , "UTF-8" );
58+ assertTrue ("Should contain whitelist root element" , content .contains ("<whitelist>" ));
59+ assertTrue ("Should contain entry elements" , content .contains ("<entry" ));
60+ assertTrue ("Should contain path attributes" , content .contains ("path=" ));
61+ }
62+
63+ @ Test
64+ public void testGenerateWhitelistWithTemplateComparison () throws Exception {
65+ GenerateMojo mojo = new GenerateMojo ();
66+
67+ File distributionFile = new File ("src/test/resources/generate-whitelist/generate_whitelist.zip" );
68+ File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
69+ File templateFile = new File ("src/test/resources/generate-whitelist/whitelist.tmpl.xml" );
70+
71+ mojo .setDistributionArchiveFile (distributionFile );
72+ mojo .setWhitelist (outputFile );
73+
74+ // Execute the generation
75+ mojo .execute ();
76+
77+ // Read both files
78+ String generatedContent = FileUtils .readFileToString (outputFile , "UTF-8" );
79+ String templateContent = FileUtils .readFileToString (templateFile , "UTF-8" );
80+
81+ // Both should have similar structure (though MD5s might differ)
82+ assertTrue ("Generated content should contain whitelist element" , generatedContent .contains ("<whitelist>" ));
83+ assertTrue ("Template content should contain whitelist element" , templateContent .contains ("<whitelist>" ));
84+
85+ // Count entries - should be similar
86+ int generatedEntries = countOccurrences (generatedContent , "<entry" );
87+ int templateEntries = countOccurrences (templateContent , "<entry" );
88+ assertEquals ("Should have same number of entries" , templateEntries , generatedEntries );
89+ }
90+
91+ @ Test (expected = org .apache .maven .plugin .MojoExecutionException .class )
92+ public void testGenerateWhitelistMissingDistribution () throws Exception {
93+ GenerateMojo mojo = new GenerateMojo ();
94+
95+ File distributionFile = new File ("src/test/resources/non-existent.zip" );
96+ File outputFile = new File (temporaryFolder .getRoot (), "generated-whitelist.xml" );
97+
98+ mojo .setDistributionArchiveFile (distributionFile );
99+ mojo .setWhitelist (outputFile );
100+
101+ // This should throw MojoExecutionException
102+ mojo .execute ();
103+ }
104+
105+ private int countOccurrences (String str , String findStr ) {
106+ int lastIndex = 0 ;
107+ int count = 0 ;
108+ while (lastIndex != -1 ) {
109+ lastIndex = str .indexOf (findStr , lastIndex );
110+ if (lastIndex != -1 ) {
111+ count ++;
112+ lastIndex += findStr .length ();
113+ }
114+ }
115+ return count ;
116+ }
117+ }
0 commit comments