1 package com.atlassian.plugins.codegen;
2
3 import com.atlassian.plugins.codegen.modules.PluginModuleCreator;
4 import com.atlassian.plugins.codegen.modules.PluginModuleProperties;
5
6 import com.google.common.collect.ImmutableList;
7 import com.google.common.collect.Iterables;
8
9 import org.dom4j.Document;
10 import org.dom4j.DocumentFactory;
11 import org.dom4j.Element;
12
13 import static com.atlassian.fugue.Option.some;
14 import static junit.framework.Assert.fail;
15 import static org.junit.Assert.assertEquals;
16
17
18
19
20 public abstract class AbstractCodegenTestCase<T extends PluginModuleProperties>
21 {
22 public static final String PACKAGE_NAME = "com.atlassian.plugins.test";
23 public static final String FUNC_TEST_PACKAGE_NAME = "it.com.atlassian.plugins.test";
24
25 protected T props;
26 protected PluginModuleCreator creator;
27 protected PluginProjectChangeset changeset;
28
29 public void setProps(T props)
30 {
31 this.props = props;
32 }
33
34 public void setCreator(PluginModuleCreator creator)
35 {
36 this.creator = creator;
37 }
38
39 protected PluginProjectChangeset getChangesetForModule() throws Exception
40 {
41 return creator.createModule(props);
42 }
43
44 protected <T extends PluginProjectChange> ImmutableList<T> getChangesetForModule(Class<T> itemClass) throws Exception
45 {
46 return ImmutableList.copyOf(getChangesetForModule().getItems(itemClass));
47 }
48
49 protected void failWithChangeset(PluginProjectChangeset changeset, String message)
50 {
51 fail(message + "; generated changeset was " + changeset.toString());
52 }
53
54 protected void assertChangesetContains(PluginProjectChange... changes) throws Exception
55 {
56 PluginProjectChangeset changeset = getChangesetForModule();
57 for (PluginProjectChange change : changes)
58 {
59 if (!Iterables.contains(changeset.getItems(), change))
60 {
61 failWithChangeset(changeset, "did not generate expected change: " + change);
62 }
63 }
64 }
65
66 protected boolean hasGeneratedModulesOfType(String name) throws Exception
67 {
68 PluginProjectChangeset changeset = getChangesetForModule();
69 for (ModuleDescriptor module : changeset.getItems(ModuleDescriptor.class))
70 {
71 if (name.equals(module.getType()))
72 {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 protected Document getAllGeneratedModulesOfType(String name) throws Exception
80 {
81 PluginProjectChangeset changeset = getChangesetForModule();
82 boolean found = false;
83 Document ret = DocumentFactory.getInstance().createDocument();
84 ret.addElement("modules");
85 for (ModuleDescriptor module : changeset.getItems(ModuleDescriptor.class))
86 {
87 if (module.getType().equals(name))
88 {
89 ret.getRootElement().add(module.getContent());
90 found = true;
91 }
92 }
93 if (!found)
94 {
95 failWithChangeset(changeset, "did not generate any module descriptor of type \"" + name + "\"");
96 }
97 return ret;
98 }
99
100 protected ComponentDeclaration getComponentOfClass(ClassId classId) throws Exception
101 {
102 PluginProjectChangeset changeset = getChangesetForModule();
103 for (ComponentDeclaration component : changeset.getItems(ComponentDeclaration.class))
104 {
105 if (component.getClassId().equals(classId))
106 {
107 return component;
108 }
109 }
110 failWithChangeset(changeset, "did not generate any component declaration of type \"" + classId + "\"");
111 return null;
112 }
113
114 protected ComponentImport getComponentImportOfInterface(ClassId interfaceId) throws Exception
115 {
116 PluginProjectChangeset changeset = getChangesetForModule();
117 for (ComponentImport component : changeset.getItems(ComponentImport.class))
118 {
119 if (component.getInterfaceClass().equals(interfaceId))
120 {
121 return component;
122 }
123 }
124 failWithChangeset(changeset, "did not generate any component import for interface \"" + interfaceId + "\"");
125 return null;
126 }
127
128 protected ArtifactDependency getDependency(String groupId, String artifactId) throws Exception
129 {
130 ArtifactId searchFor = ArtifactId.artifactId(some(groupId), artifactId);
131 PluginProjectChangeset changeset = getChangesetForModule();
132 for (ArtifactDependency dependency : changeset.getItems(ArtifactDependency.class))
133 {
134 if (searchFor.equals(dependency.getGroupAndArtifactId()))
135 {
136 return dependency;
137 }
138 }
139 failWithChangeset(changeset, "did not generate any dependency for " + searchFor);
140 return null;
141 }
142
143 protected Element getGeneratedModule(String name) throws Exception
144 {
145 Document results = getAllGeneratedModulesOfType(name);
146 assertEquals("found too many modules of type \"" + name + "\"", 1, results.selectNodes("//" + name).size());
147 return (Element) results.selectSingleNode("//modules/" + name);
148 }
149
150 protected I18nString getI18nString(String name) throws Exception
151 {
152 PluginProjectChangeset changeset = getChangesetForModule();
153 for (I18nString i : changeset.getItems(I18nString.class))
154 {
155 if (i.getName().equals(name))
156 {
157 return i;
158 }
159 }
160 failWithChangeset(changeset, "did not generate i18n string '" + name + "'");
161 return null;
162 }
163
164 protected I18nString getI18nString(String name, String value) throws Exception
165 {
166 I18nString i = getI18nString(name);
167 assertEquals("i18n string '" + name + "' had wrong value", value, i.getValue());
168 return i;
169 }
170
171 protected SourceFile getSourceFile(String packageName, String className) throws Exception
172 {
173 return getSourceFile(SourceFile.SourceGroup.MAIN, packageName, className);
174 }
175
176 protected SourceFile getTestSourceFile(String packageName, String className) throws Exception
177 {
178 return getSourceFile(SourceFile.SourceGroup.TESTS, packageName, className);
179 }
180
181 protected SourceFile getSourceFile(SourceFile.SourceGroup group, String packageName, String className) throws Exception
182 {
183 ClassId searchFor = ClassId.packageAndClass(packageName, className);
184 PluginProjectChangeset changeset = getChangesetForModule();
185 for (SourceFile sourceFile : changeset.getItems(SourceFile.class))
186 {
187 if (sourceFile.getClassId().equals(searchFor) && sourceFile.getSourceGroup().equals(group))
188 {
189 return sourceFile;
190 }
191 }
192 failWithChangeset(changeset, "did not generate a source file for " + searchFor + " in " + group);
193 return null;
194 }
195
196 protected ResourceFile getResourceFile(String path, String filename) throws Exception
197 {
198 PluginProjectChangeset changeset = getChangesetForModule();
199 for (ResourceFile resourceFile : changeset.getItems(ResourceFile.class))
200 {
201 if (resourceFile.getRelativePath().equals(path) && resourceFile.getName().equals(filename))
202 {
203 return resourceFile;
204 }
205 }
206 failWithChangeset(changeset, "did not generate resource file " + path + "/" + filename);
207 return null;
208 }
209 }