1 package com.atlassian.plugins.codegen;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6
7 import org.apache.commons.io.FileUtils;
8 import org.junit.After;
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import static com.atlassian.plugins.codegen.ClassId.fullyQualified;
13 import static com.atlassian.plugins.codegen.I18nString.i18nString;
14 import static com.atlassian.plugins.codegen.PluginProjectChangeset.changeset;
15 import static com.atlassian.plugins.codegen.SourceFile.sourceFile;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertTrue;
18
19 public class ProjectFilesRewriterTest
20 {
21 protected static final ClassId CLASS = fullyQualified("com.atlassian.test.MyClass");
22 protected static final String CONTENT = "this is some amazing content";
23 protected static final String I18N_FILE_NAME = ProjectHelper.GROUP_ID + File.separator + ProjectHelper.ARTIFACT_ID + ".properties";
24
25 protected ProjectHelper helper;
26 protected ProjectFilesRewriter rewriter;
27 protected File i18nFile;
28
29 @Before
30 public void setup() throws Exception
31 {
32 helper = new ProjectHelper();
33 usePluginXml("empty-plugin.xml");
34 }
35
36 private void usePluginXml(String path) throws Exception
37 {
38 helper.usePluginXml(path);
39 rewriter = new ProjectFilesRewriter(helper.location);
40 i18nFile = new File(helper.location.getResourcesDir(), I18N_FILE_NAME);
41 }
42
43 @After
44 public void deleteTempDir() throws Exception
45 {
46 helper.destroy();
47 }
48
49 @Test
50 public void sourceFileIsCreated() throws Exception
51 {
52 PluginProjectChangeset changes = new PluginProjectChangeset()
53 .with(sourceFile(CLASS, SourceFile.SourceGroup.MAIN, CONTENT));
54 rewriter.applyChanges(changes);
55
56 assertTrue(new File(helper.srcDir, "com/atlassian/test/MyClass.java").exists());
57 }
58
59 @Test
60 public void sourceFileHasContent() throws Exception
61 {
62 PluginProjectChangeset changes = new PluginProjectChangeset()
63 .with(sourceFile(CLASS, SourceFile.SourceGroup.MAIN, CONTENT));
64 rewriter.applyChanges(changes);
65
66 assertEquals(CONTENT, FileUtils.readFileToString(new File(helper.srcDir, "com/atlassian/test/MyClass.java")));
67 }
68
69 @Test
70 public void testSourceFileIsCreated() throws Exception
71 {
72 PluginProjectChangeset changes = new PluginProjectChangeset()
73 .with(sourceFile(CLASS, SourceFile.SourceGroup.TESTS, CONTENT));
74 rewriter.applyChanges(changes);
75
76 assertTrue(new File(helper.testDir, "com/atlassian/test/MyClass.java").exists());
77 }
78
79 @Test
80 public void testSourceFileHasContent() throws Exception
81 {
82 PluginProjectChangeset changes = new PluginProjectChangeset()
83 .with(sourceFile(CLASS, SourceFile.SourceGroup.TESTS, CONTENT));
84 rewriter.applyChanges(changes);
85
86 assertEquals(CONTENT, FileUtils.readFileToString(new File(helper.testDir, "com/atlassian/test/MyClass.java")));
87 }
88
89 @Test
90 public void resourceFileIsCreated() throws Exception
91 {
92 PluginProjectChangeset changes = new PluginProjectChangeset()
93 .with(ResourceFile.resourceFile("templates/test", "template.vm", CONTENT));
94 rewriter.applyChanges(changes);
95
96 assertTrue(new File(helper.resourcesDir, "templates/test/template.vm").exists());
97 }
98
99 @Test
100 public void resourceFileHasContent() throws Exception
101 {
102 PluginProjectChangeset changes = new PluginProjectChangeset()
103 .with(ResourceFile.resourceFile("templates/test", "template.vm", CONTENT));
104 rewriter.applyChanges(changes);
105
106 assertEquals(CONTENT, FileUtils.readFileToString(new File(helper.resourcesDir, "templates/test/template.vm")));
107 }
108
109 @Test
110 public void i18nPropertyFileIsCreatedWithDefaultLocation() throws Exception
111 {
112 rewriter.applyChanges(changeset().with(i18nString("foo", "bar")));
113
114 assertTrue(i18nFile.exists());
115 }
116
117 @Test
118 public void i18nPropertyFileHasPropertyValue() throws Exception
119 {
120 rewriter.applyChanges(changeset().with(i18nString("foo", "bar")));
121
122 Properties props = new Properties();
123 props.load(new FileInputStream(i18nFile));
124 assertEquals("bar", props.getProperty("foo"));
125 }
126
127 @Test
128 public void duplicateI18nPropertyNameIsNotAdded() throws Exception
129 {
130 FileUtils.writeStringToFile(i18nFile, "foo=goo\n");
131 rewriter.applyChanges(changeset().with(i18nString("foo", "bar")));
132
133 assertEquals("foo=goo\n", FileUtils.readFileToString(i18nFile));
134 }
135
136 @Test
137 public void propertiesAreNotReordered() throws Exception
138 {
139 FileUtils.writeStringToFile(i18nFile, "foo=goo\nall=lol");
140 rewriter.applyChanges(changeset().with(i18nString("gal", "pal"), i18nString("bar", "car")));
141
142 assertEquals("foo=goo\nall=lol\ngal=pal\nbar=car\n", FileUtils.readFileToString(i18nFile));
143 }
144
145 @Test
146 public void i18nPropertyFileIsCreatedWithCustomLocation() throws Exception
147 {
148 usePluginXml("plugin-with-same-i18n-name.xml");
149
150 rewriter.applyChanges(changeset().with(i18nString("foo", "bar")));
151
152 assertTrue(new File(helper.resourcesDir, "nonstandard-location.properties").exists());
153 }
154 }