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