1 package com.atlassian.plugins.codegen.modules.common.component;
2
3 import java.io.File;
4 import java.util.regex.Matcher;
5
6 import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
7 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
8
9 import org.dom4j.Document;
10 import org.dom4j.Node;
11 import org.junit.Before;
12 import org.junit.Test;
13
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17
18
19
20 public class ComponentTest extends AbstractCodegenTestCase<ComponentProperties>
21 {
22 public static final String PACKAGE_NAME = "com.atlassian.plugins.component";
23
24 @Before
25 public void runGenerator() throws Exception
26 {
27 setCreator(new ComponentModuleCreator());
28 setModuleLocation(new PluginModuleLocation.Builder(srcDir)
29 .resourcesDirectory(resourcesDir)
30 .testDirectory(testDir)
31 .templateDirectory(templateDir)
32 .build());
33
34 setProps(new ComponentProperties(PACKAGE_NAME + ".CustomComponent"));
35
36 props.setIncludeExamples(false);
37
38 }
39
40 @Test
41 public void allFilesAreGenerated() throws Exception
42 {
43 props.setFullyQualifiedInterface(PACKAGE_NAME + ".CustomInterface");
44 props.setGenerateClass(true);
45 props.setGenerateInterface(true);
46 creator.createModule(moduleLocation, props);
47
48 String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
49 String itPackagePath = "it" + File.separator + packagePath;
50 assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "CustomComponent.java").exists());
51 assertTrue("interface not generated", new File(srcDir, packagePath + File.separator + "CustomInterface.java").exists());
52 assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "CustomComponentTest.java").exists());
53 assertTrue("funcTest class not generated", new File(testDir, itPackagePath + File.separator + "CustomComponentFuncTest.java").exists());
54 assertTrue("main class not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
55
56 }
57
58 @Test
59 public void componentAdded() throws Exception
60 {
61 props.setFullyQualifiedInterface(PACKAGE_NAME + ".CustomInterface");
62 props.setGenerateClass(true);
63 props.setGenerateInterface(true);
64 creator.createModule(moduleLocation, props);
65 Document pluginDoc = getXmlDocument(pluginXml);
66
67 String compXPath = "/atlassian-plugin/component[@name='Custom Component' and @key='custom-component' and @i18n-name-key='custom-component.name' and @class='" + PACKAGE_NAME + ".CustomComponent']";
68 String compIfaceXPath = "interface[text() = '" + PACKAGE_NAME + ".CustomInterface']";
69
70 Node compNode = pluginDoc.selectSingleNode(compXPath);
71 assertNotNull("component not found", compNode);
72 assertNotNull("interface not found", compNode.selectSingleNode(compIfaceXPath));
73
74 }
75
76 }