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 IssueTabPanelTest 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 IssueTabPanelModuleCreator());
30          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
31                  .resourcesDirectory(resourcesDir)
32                  .testDirectory(testDir)
33                  .templateDirectory(templateDir)
34                  .build());
35  
36          setProps(new TabPanelProperties(PACKAGE_NAME + ".MyIssueTabPanel"));
37  
38          props.setIncludeExamples(false);
39          props.setUseCustomClass(true);
40  
41          templatePath = new File(templateDir, "tabpanels");
42  
43      }
44  
45      @Test
46      public void allFilesAreGenerated() throws Exception
47      {
48          creator.createModule(moduleLocation, props);
49  
50          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
51          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyIssueTabPanel.java").exists());
52          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyIssueTabPanelTest.java").exists());
53          assertTrue("view template not generated", new File(templatePath, "my-issue-tab-panel.vm").exists());
54          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
55  
56      }
57  
58      @Test
59      public void moduleIsValid() throws Exception
60      {
61          String xpath = "/atlassian-plugin/issue-tabpanel[@name='My Issue Tab Panel' and @key='my-issue-tab-panel' and @i18n-name-key='my-issue-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyIssueTabPanel']";
62  
63          creator.createModule(moduleLocation, props);
64          Document pluginDoc = getXmlDocument(pluginXml);
65  
66          assertNotNull("valid issue-tabpanel not found", pluginDoc.selectSingleNode(xpath));
67      }
68  
69      @Test
70      public void labelIsAdded() throws Exception
71      {
72          String xpath = "/atlassian-plugin/issue-tabpanel[@name='My Issue Tab Panel' and @key='my-issue-tab-panel' and @i18n-name-key='my-issue-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyIssueTabPanel']";
73  
74          Label label = new Label("common.concepts.issue.tabpanel", "my issue panel");
75          props.setLabel(label);
76          creator.createModule(moduleLocation, props);
77  
78          String labelXpath = "/atlassian-plugin/issue-tabpanel/label[@key='common.concepts.issue.tabpanel']";
79          Document pluginDoc = getXmlDocument(pluginXml);
80  
81          assertNotNull("valid custom issue-tabpanel not found", pluginDoc.selectSingleNode(xpath));
82  
83          Node labelNode = pluginDoc.selectSingleNode(labelXpath);
84          assertNotNull("label not found", labelNode);
85  
86          Properties i18nprops = loadI18nProperties();
87          assertTrue("label i18n not found", i18nprops.containsKey(label.getKey()));
88          assertEquals("label i18n has wrong value", label.getValue(), i18nprops.getProperty(label.getKey()));
89  
90          Document viewDoc = getXmlDocument(new File(templatePath, "my-issue-tab-panel.vm"));
91          String viewLableXpath = "/div/h3[text() = \"$i18n.getText('" + label.getKey() + "')\"]";
92          assertNotNull("label not found in view template", viewDoc.selectSingleNode(viewLableXpath));
93      }
94  
95      @Test
96      public void orderIsAdded() throws Exception
97      {
98          String xpath = "/atlassian-plugin/issue-tabpanel[@name='My Issue Tab Panel' and @key='my-issue-tab-panel' and @i18n-name-key='my-issue-tab-panel.name' and @class='" + PACKAGE_NAME + ".MyIssueTabPanel']";
99          props.setOrder(10);
100         creator.createModule(moduleLocation, props);
101 
102         String orderXpath = "/atlassian-plugin/issue-tabpanel/order[text() = '10']";
103         Document pluginDoc = getXmlDocument(pluginXml);
104 
105         assertNotNull("valid custom issue-tabpanel not found", pluginDoc.selectSingleNode(xpath));
106 
107         Node orderNode = pluginDoc.selectSingleNode(orderXpath);
108         assertNotNull("order not found", orderNode);
109 
110     }
111 
112 }