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