1   package com.atlassian.plugins.codegen;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.net.MalformedURLException;
7   import java.util.Properties;
8   import java.util.UUID;
9   
10  import com.atlassian.plugins.codegen.annotations.asm.ModuleCreatorAnnotationParser;
11  import com.atlassian.plugins.codegen.modules.*;
12  
13  import org.apache.commons.io.FileUtils;
14  import org.apache.commons.io.IOUtils;
15  import org.dom4j.Document;
16  import org.dom4j.DocumentException;
17  import org.dom4j.io.SAXReader;
18  import org.junit.After;
19  import org.junit.Before;
20  
21  /**
22   *
23   */
24  public abstract class AbstractCodegenTestCase<T extends PluginModuleProperties>
25  {
26      protected File tempDir;
27      protected File srcDir;
28      protected File testDir;
29      protected File resourcesDir;
30      protected File templateDir;
31      protected File pluginXml;
32      protected PluginModuleCreatorRegistry pluginModuleCreatorRegistry;
33      protected ModuleCreatorAnnotationParser parser;
34  
35      protected PluginModuleLocation moduleLocation;
36      protected T props;
37      protected PluginModuleCreator creator;
38  
39      public void setProps(T props)
40      {
41          this.props = props;
42      }
43  
44      public void setCreator(PluginModuleCreator creator)
45      {
46          this.creator = creator;
47      }
48  
49      public void setModuleLocation(PluginModuleLocation moduleLocation)
50      {
51          this.moduleLocation = moduleLocation;
52      }
53  
54      @Before
55      public void setup() throws Exception
56      {
57  
58          pluginModuleCreatorRegistry = new PluginModuleCreatorRegistryImpl();
59          parser = new ModuleCreatorAnnotationParser(pluginModuleCreatorRegistry);
60          parser.parse();
61  
62          final File sysTempDir = new File("target");
63          String dirName = UUID.randomUUID()
64                  .toString();
65          tempDir = new File(sysTempDir, dirName);
66          srcDir = new File(tempDir, "src");
67          testDir = new File(tempDir, "test-src");
68          resourcesDir = new File(tempDir, "resources");
69          templateDir = new File(resourcesDir, "templates");
70          pluginXml = new File(resourcesDir, "atlassian-plugin.xml");
71  
72          tempDir.mkdirs();
73          srcDir.mkdirs();
74          resourcesDir.mkdirs();
75          templateDir.mkdirs();
76  
77          InputStream is = this.getClass()
78                  .getResourceAsStream("/empty-plugin.xml");
79          IOUtils.copy(is, FileUtils.openOutputStream(pluginXml));
80  
81      }
82  
83      @After
84      public void removeTempDir() throws IOException
85      {
86          FileUtils.deleteQuietly(tempDir);
87      }
88  
89      protected Document getXmlDocument(File xmlFile) throws MalformedURLException, DocumentException
90      {
91          SAXReader reader = new SAXReader();
92          return reader.read(xmlFile);
93      }
94  
95      protected Properties loadI18nProperties() throws IOException
96      {
97          File i18nFile = new File(resourcesDir, AbstractPluginModuleCreator.DEFAULT_I18N_NAME + ".properties");
98          Properties props = new Properties();
99  
100         InputStream is = null;
101         try
102         {
103             is = FileUtils.openInputStream(i18nFile);
104             props.load(is);
105 
106         } finally
107         {
108             IOUtils.closeQuietly(is);
109         }
110 
111         return props;
112     }
113 }