1   package com.atlassian.maven.plugins.amps;
2   
3   import com.atlassian.maven.plugins.amps.product.ProductHandler;
4   import com.atlassian.maven.plugins.amps.product.RefappProductHandler;
5   import org.apache.commons.io.FileUtils;
6   import org.apache.maven.model.Build;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.maven.plugin.logging.SystemStreamLog;
9   import org.apache.maven.project.MavenProject;
10  import org.junit.After;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  import java.io.File;
15  import java.io.IOException;
16  import java.util.Collections;
17  import java.util.Enumeration;
18  import java.util.List;
19  import java.util.UUID;
20  import java.util.zip.ZipEntry;
21  import java.util.zip.ZipFile;
22  
23  import static org.junit.Assert.*;
24  import static org.mockito.Mockito.*;
25  
26  public class TestMavenGoalsHomeZip
27  {
28      public static final String PRODUCT_ID = "noplacelike";
29      public static final String INSTANCE_ID = "noplacelike1";
30      public static final String TMP_RESOURCES = "tmp-resources";
31      public static final String GENERATED_HOME = "generated-home";
32      public static final String PLUGINS = "plugins";
33      public static final String BUNDLED_PLUGINS = "bundled-plugins";
34      public static final String ZIP_PREFIX = "generated-resources/" + PRODUCT_ID + "-home";
35  
36      private ProductHandler productHandler;
37      private MavenContext ctx;
38      private File tempDir;
39      private File productDir;
40      private File tempResourcesDir;
41      private File generatedHomeDir;
42      private File pluginsDir;
43      private File bundledPluginsDir;
44      private ZipFile zip;
45      private Product product;
46  
47      @Before
48      public void setup()
49      {
50          // Create the temp dir
51          final File sysTempDir = new File("target");
52          String dirName = UUID.randomUUID().toString();
53          tempDir = new File(sysTempDir, dirName);
54          productDir = new File(tempDir, INSTANCE_ID);
55          tempResourcesDir = new File(productDir, TMP_RESOURCES);
56          generatedHomeDir = new File(tempResourcesDir, GENERATED_HOME);
57          pluginsDir = new File(generatedHomeDir, PLUGINS);
58          bundledPluginsDir = new File(generatedHomeDir, BUNDLED_PLUGINS);
59  
60          // setup maven mocks
61          MavenProject project = mock(MavenProject.class);
62          Build build = mock(Build.class);
63  
64          // Mock the product
65          product = mock(Product.class);
66          when(product.getId()).thenReturn(PRODUCT_ID);
67          when(product.getInstanceId()).thenReturn(INSTANCE_ID);
68  
69          // Mockito throws NoClassDefFoundError: org/apache/maven/project/ProjectBuilderConfiguration
70          // when mocking the session
71          // MavenSession session = mock(MavenSession.class);
72  
73          SystemStreamLog log = new SystemStreamLog();
74          List<MavenProject> reactor = Collections.<MavenProject> emptyList();
75          ctx = mock(MavenContext.class);
76  
77          when(build.getDirectory()).thenReturn(tempDir.getAbsolutePath());
78          when(project.getBuild()).thenReturn(build);
79          when(ctx.getProject()).thenReturn(project);
80          when(ctx.getLog()).thenReturn(log);
81          when(ctx.getReactor()).thenReturn(reactor);
82          when(ctx.getSession()).thenReturn(null);
83  
84          productHandler = new RefappProductHandler(ctx, null);
85      }
86  
87      @After
88      public void removeTempDir() throws IOException
89      {
90          // make sure zip is closed, else delete fails on windows
91          if (zip != null)
92          {
93              try
94              {
95                  zip.close();
96              }
97              catch (IOException e)
98              {
99                  // ignore
100             }
101             zip = null;
102         }
103         FileUtils.deleteDirectory(tempDir);
104     }
105 
106     @Test
107     public void skipNullHomeDir() throws MojoExecutionException
108     {
109         File zip = new File(tempDir, "nullHomeZip.zip");
110 
111         productHandler.createHomeZip(null, zip, product);
112 
113         assertFalse("zip for null home should not exist", zip.exists());
114     }
115 
116     @Test
117     public void skipNonExistentHomeDir() throws MojoExecutionException
118     {
119         File zip = new File(tempDir, "noExistHomeZip.zip");
120         File fakeHomeDir = new File(tempDir, "this-folder-does-not-exist");
121 
122         productHandler.createHomeZip(fakeHomeDir, zip, product);
123 
124         assertFalse("zip for non-existent home should not exist", zip.exists());
125     }
126 
127     @Test
128     public void existingGeneratedDirGetsDeleted() throws IOException, MojoExecutionException
129     {
130         generatedHomeDir.mkdirs();
131         File deletedFile = new File(generatedHomeDir, "should-be-deleted.txt");
132         FileUtils.writeStringToFile(deletedFile, "This file should have been deleted!");
133 
134         File zip = new File(tempDir, "deleteGenHomeZip.zip");
135         File homeDir = new File(tempDir, "deleteGenHomeDir");
136         homeDir.mkdirs();
137 
138         productHandler.createHomeZip(homeDir, zip, product);
139 
140         assertFalse("generated text file should have been deleted", deletedFile.exists());
141     }
142 
143     @Test
144     public void pluginsNotIncluded() throws IOException, MojoExecutionException
145     {
146         pluginsDir.mkdirs();
147 
148         File pluginFile = new File(pluginsDir, "plugin.txt");
149         FileUtils.writeStringToFile(pluginFile, "This file should have been deleted!");
150 
151         File zip = new File(tempDir, "deletePluginsHomeZip.zip");
152         File homeDir = new File(tempDir, "deletePluginsHomeDir");
153         homeDir.mkdirs();
154 
155         productHandler.createHomeZip(homeDir, zip, product);
156 
157         assertFalse("plugins file should have been deleted", pluginFile.exists());
158     }
159 
160     @Test
161     public void bundledPluginsNotIncluded() throws IOException, MojoExecutionException
162     {
163         bundledPluginsDir.mkdirs();
164 
165         File pluginFile = new File(bundledPluginsDir, "bundled-plugin.txt");
166         FileUtils.writeStringToFile(pluginFile, "This file should have been deleted!");
167 
168         File zip = new File(tempDir, "deleteBundledPluginsHomeZip.zip");
169         File homeDir = new File(tempDir, "deleteBundledPluginsHomeDir");
170         homeDir.mkdirs();
171 
172         productHandler.createHomeZip(homeDir, zip, product);
173 
174         assertFalse("bundled-plugins file should have been deleted", pluginFile.exists());
175     }
176 
177     @Test
178     public void zipContainsProperPrefix() throws IOException, MojoExecutionException
179     {
180         File zipFile = new File(tempDir, "prefixedHomeZip.zip");
181         File homeDir = new File(tempDir, "prefixedHomeDir");
182         File dataDir = new File(homeDir, "data");
183 
184         dataDir.mkdirs();
185 
186         productHandler.createHomeZip(homeDir, zipFile, product);
187 
188         zip = new ZipFile(zipFile);
189         final Enumeration<? extends ZipEntry> entries = zip.entries();
190 
191         while (entries.hasMoreElements())
192         {
193             final ZipEntry zipEntry = entries.nextElement();
194             String zipPath = zipEntry.getName();
195             String[] segments = zipPath.split("/");
196             if (segments.length > 1)
197             {
198                 String testPrefix = segments[0] + "/" + segments[1];
199                 assertEquals(ZIP_PREFIX, testPrefix);
200             }
201 
202         }
203     }
204 
205     @Test
206     public void zipContainsTestFile() throws IOException, MojoExecutionException
207     {
208         File zipFile = new File(tempDir, "fileHomeZip.zip");
209         File homeDir = new File(tempDir, "fileHomeDir");
210         File dataDir = new File(homeDir, "data");
211         File dataFile = new File(dataDir, "data.txt");
212 
213         dataDir.mkdirs();
214         FileUtils.writeStringToFile(dataFile, "This is some data.");
215 
216         productHandler.createHomeZip(homeDir, zipFile, product);
217 
218         boolean dataFileFound = false;
219         zip = new ZipFile(zipFile);
220         final Enumeration<? extends ZipEntry> entries = zip.entries();
221 
222         while (entries.hasMoreElements())
223         {
224             final ZipEntry zipEntry = entries.nextElement();
225             String zipPath = zipEntry.getName();
226             String fileName = zipPath.substring(zipPath.lastIndexOf("/") + 1);
227             if (fileName.equals(dataFile.getName()))
228             {
229                 dataFileFound = true;
230                 break;
231             }
232         }
233 
234         assertTrue("data file not found in zip.", dataFileFound);
235     }
236 }