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