1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import java.io.File;
4   import java.util.Properties;
5   import java.util.regex.Matcher;
6   
7   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
8   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9   import com.atlassian.plugins.codegen.modules.common.Label;
10  
11  import org.dom4j.Document;
12  import org.dom4j.Node;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.*;
17  
18  /**
19   * @since 3.6
20   */
21  public class ComponentTabPanelTest extends AbstractCodegenTestCase<TabPanelProperties>
22  {
23      public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.tabpanels";
24      protected File templatePath;
25  
26      @Before
27      public void runGenerator() throws Exception
28      {
29          setCreator(new ComponentTabPanelModuleCreator());
30          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
31                  .resourcesDirectory(resourcesDir)
32                  .testDirectory(testDir)
33                  .templateDirectory(templateDir)
34                  .build());
35  
36          setProps(new TabPanelProperties(PACKAGE_NAME + ".MyComponentTabPanel"));
37  
38          props.setIncludeExamples(false);
39  
40          templatePath = new File(templateDir, "tabpanels");
41      }
42  
43      @Test
44      public void customFilesAreGenerated() throws Exception
45      {
46          props.setUseCustomClass(true);
47          creator.createModule(moduleLocation, props);
48  
49          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
50          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyComponentTabPanel.java").exists());
51          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyComponentTabPanelTest.java").exists());
52          assertTrue("view template not generated", new File(templatePath, "my-component-tab-panel.vm").exists());
53          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
54  
55      }
56  
57      @Test
58      public void genericFilesAreGenerated() throws Exception
59      {
60          setProps(new TabPanelProperties(ComponentTabPanelModuleCreator.FQ_GENERIC_CLASS));
61          props.setUseCustomClass(false);
62          props.setModuleNameAndKey("My Component Tab Panel");
63          creator.createModule(moduleLocation, props);
64  
65          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
66          assertFalse("main class should not be generated", new File(srcDir, packagePath + File.separator + "MyComponentTabPanel.java").exists());
67          assertFalse("test class should not be generated", new File(testDir, packagePath + File.separator + "MyComponentTabPanelTest.java").exists());
68          assertTrue("view template not generated", new File(templatePath, "my-component-tab-panel.vm").exists());
69          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
70  
71      }
72  
73      @Test
74      public void customModuleIsValid() throws Exception
75      {
76          String xpath = "/atlassian-plugin/component-tabpanel[@name='My Component Tab Panel' and @key='my-component-tab-panel' and @i18n-name-key='my-component-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyComponentTabPanel']";
77          props.setUseCustomClass(true);
78          creator.createModule(moduleLocation, props);
79          Document pluginDoc = getXmlDocument(pluginXml);
80  
81          assertNotNull("valid custom component-tabpanel not found", pluginDoc.selectSingleNode(xpath));
82      }
83  
84      @Test
85      public void genericModuleIsValid() throws Exception
86      {
87          String xpath = "/atlassian-plugin/component-tabpanel[@name='My Component Tab Panel' and @key='my-component-tab-panel' and @i18n-name-key='my-component-tab-panel.name' and @class='" + ComponentTabPanelModuleCreator.FQ_GENERIC_CLASS + "']";
88  
89          setProps(new TabPanelProperties(ComponentTabPanelModuleCreator.FQ_GENERIC_CLASS));
90          props.setModuleNameAndKey("My Component Tab Panel");
91          props.setUseCustomClass(false);
92          creator.createModule(moduleLocation, props);
93          Document pluginDoc = getXmlDocument(pluginXml);
94  
95          assertNotNull("valid generic component-tabpanel not found", pluginDoc.selectSingleNode(xpath));
96      }
97  
98      @Test
99      public void labelIsAdded() throws Exception
100     {
101         String xpath = "/atlassian-plugin/component-tabpanel[@name='My Component Tab Panel' and @key='my-component-tab-panel' and @i18n-name-key='my-component-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyComponentTabPanel']";
102 
103         Label label = new Label("common.concepts.component.tabpanel", "my component panel");
104         props.setLabel(label);
105         props.setUseCustomClass(true);
106         creator.createModule(moduleLocation, props);
107 
108         String labelXpath = "/atlassian-plugin/component-tabpanel/label[@key='common.concepts.component.tabpanel']";
109         Document pluginDoc = getXmlDocument(pluginXml);
110 
111         assertNotNull("valid custom component-tabpanel not found", pluginDoc.selectSingleNode(xpath));
112 
113         Node labelNode = pluginDoc.selectSingleNode(labelXpath);
114         assertNotNull("label not found", labelNode);
115 
116         Properties i18nprops = loadI18nProperties();
117         assertTrue("label i18n not found", i18nprops.containsKey(label.getKey()));
118         assertEquals("label i18n has wrong value", label.getValue(), i18nprops.getProperty(label.getKey()));
119 
120         Document viewDoc = getXmlDocument(new File(templatePath, "my-component-tab-panel.vm"));
121         String viewLableXpath = "/div/h3[text() = \"$i18n.getText('" + label.getKey() + "')\"]";
122         assertNotNull("label not found in view template", viewDoc.selectSingleNode(viewLableXpath));
123     }
124 
125     @Test
126     public void orderIsAdded() throws Exception
127     {
128         String xpath = "/atlassian-plugin/component-tabpanel[@name='My Component Tab Panel' and @key='my-component-tab-panel' and @i18n-name-key='my-component-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyComponentTabPanel']";
129         props.setOrder(10);
130         props.setUseCustomClass(true);
131         creator.createModule(moduleLocation, props);
132 
133         String orderXpath = "/atlassian-plugin/component-tabpanel/order[text() = '10']";
134         Document pluginDoc = getXmlDocument(pluginXml);
135 
136         assertNotNull("valid custom component-tabpanel not found", pluginDoc.selectSingleNode(xpath));
137 
138         Node orderNode = pluginDoc.selectSingleNode(orderXpath);
139         assertNotNull("order not found", orderNode);
140 
141     }
142 
143 }