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