1   package com.atlassian.plugins.codegen;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.util.Properties;
6   import java.util.UUID;
7   
8   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9   
10  import com.google.common.collect.ImmutableMap;
11  
12  import org.apache.commons.io.FileUtils;
13  import org.junit.After;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import static com.atlassian.plugins.codegen.ClassId.fullyQualified;
18  import static com.atlassian.plugins.codegen.SourceFile.sourceFile;
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  public class ProjectFilesRewriterTest
23  {
24      public static final ClassId CLASS = fullyQualified("com.atlassian.test.MyClass");
25      public static final String CONTENT = "this is some amazing content";
26      
27      protected File tempDir;
28      protected File srcDir;
29      protected File testDir;
30      protected File resourcesDir;
31  
32      protected PluginModuleLocation moduleLocation;
33      protected ProjectFilesRewriter rewriter;
34      
35      @Before
36      public void setup() throws Exception
37      {
38          final File sysTempDir = new File("target");
39          String dirName = UUID.randomUUID().toString();
40          tempDir = new File(sysTempDir, dirName);
41          srcDir = new File(tempDir, "src");
42          testDir = new File(tempDir, "test-src");
43          resourcesDir = new File(tempDir, "resources");
44  
45          tempDir.mkdirs();
46          srcDir.mkdirs();
47          resourcesDir.mkdirs();
48          
49          moduleLocation = new PluginModuleLocation.Builder(srcDir)
50              .resourcesDirectory(resourcesDir)
51              .testDirectory(testDir)
52              .build();
53          
54          rewriter = new ProjectFilesRewriter(moduleLocation);
55      }
56      
57      @After
58      public void deleteTempDir() throws Exception
59      {
60          FileUtils.deleteDirectory(tempDir);
61      }
62      
63      @Test
64      public void sourceFileIsCreated() throws Exception
65      {
66          PluginProjectChangeset changes = new PluginProjectChangeset()
67              .withSourceFile(sourceFile(CLASS, SourceFile.SourceGroup.MAIN, CONTENT));
68          rewriter.applyChanges(changes);
69          
70          assertTrue(new File(srcDir, "com/atlassian/test/MyClass.java").exists());
71      }
72      
73      @Test
74      public void sourceFileHasContent() throws Exception
75      {
76          PluginProjectChangeset changes = new PluginProjectChangeset()
77              .withSourceFile(sourceFile(CLASS, SourceFile.SourceGroup.MAIN, CONTENT));
78          rewriter.applyChanges(changes);
79          
80          assertEquals(CONTENT, FileUtils.readFileToString(new File(srcDir, "com/atlassian/test/MyClass.java")));
81      }
82      
83      @Test
84      public void testSourceFileIsCreated() throws Exception
85      {
86          PluginProjectChangeset changes = new PluginProjectChangeset()
87              .withSourceFile(sourceFile(CLASS, SourceFile.SourceGroup.TESTS, CONTENT));
88          rewriter.applyChanges(changes);
89          
90          assertTrue(new File(testDir, "com/atlassian/test/MyClass.java").exists());
91      }
92      
93      @Test
94      public void testSourceFileHasContent() throws Exception
95      {
96          PluginProjectChangeset changes = new PluginProjectChangeset()
97              .withSourceFile(sourceFile(CLASS, SourceFile.SourceGroup.TESTS, CONTENT));
98          rewriter.applyChanges(changes);
99          
100         assertEquals(CONTENT, FileUtils.readFileToString(new File(testDir, "com/atlassian/test/MyClass.java")));
101     }
102     
103     @Test
104     public void resourceFileIsCreated() throws Exception
105     {
106         PluginProjectChangeset changes = new PluginProjectChangeset()
107             .withResourceFile(ResourceFile.resourceFile("templates/test", "template.vm", CONTENT));
108         rewriter.applyChanges(changes);
109         
110         assertTrue(new File(resourcesDir, "templates/test/template.vm").exists());        
111     }
112     
113     @Test
114     public void resourceFileHasContent() throws Exception
115     {
116         PluginProjectChangeset changes = new PluginProjectChangeset()
117             .withResourceFile(ResourceFile.resourceFile("templates/test", "template.vm", CONTENT));
118         rewriter.applyChanges(changes);
119         
120         assertEquals(CONTENT, FileUtils.readFileToString(new File(resourcesDir, "templates/test/template.vm")));
121     }
122     
123     @Test
124     public void i18nPropertyFileIsCreated() throws Exception
125     {
126         PluginProjectChangeset changes = new PluginProjectChangeset()
127             .withI18nProperties(ImmutableMap.of("foo", "bar"));
128         rewriter.applyChanges(changes);
129         
130         assertTrue(new File(resourcesDir, ProjectRewriter.DEFAULT_I18N_NAME + ".properties").exists());        
131     }
132     
133     @Test
134     public void i18nPropertyFileHasPropertyValue() throws Exception
135     {
136         PluginProjectChangeset changes = new PluginProjectChangeset()
137             .withI18nProperties(ImmutableMap.of("foo", "bar"));
138         rewriter.applyChanges(changes);
139         
140         Properties props = new Properties();
141         props.load(new FileInputStream(new File(resourcesDir, ProjectRewriter.DEFAULT_I18N_NAME + ".properties")));
142         assertEquals("bar", props.getProperty("foo"));
143     }
144 }