1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import com.atlassian.plugins.codegen.AbstractModuleCreatorTestCase;
4   import com.atlassian.plugins.codegen.modules.PluginModuleCreator;
5   
6   import org.dom4j.Document;
7   import org.dom4j.DocumentHelper;
8   import org.junit.Before;
9   import org.junit.Test;
10  
11  import static org.junit.Assert.assertEquals;
12  import static org.junit.Assert.assertNotNull;
13  import static org.junit.Assert.assertTrue;
14  
15  /**
16   * @since 3.8
17   */
18  public abstract class AbstractTabPanelTest extends AbstractModuleCreatorTestCase<TabPanelProperties>
19  {
20      public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.tabpanels";
21      
22      public AbstractTabPanelTest(String type, PluginModuleCreator<TabPanelProperties> creator)
23      {
24          super(type, creator);
25      }
26      
27      @Before
28      public void setupObjects() throws Exception
29      {
30          setProps(new TabPanelProperties(PACKAGE_NAME + ".MyTabPanel"));
31          props.setIncludeExamples(false);
32      }
33  
34      @Test
35      public void customClassFileIsGenerated() throws Exception
36      {
37          props.setUseCustomClass(true);
38  
39          getSourceFile(PACKAGE_NAME, "MyTabPanel");
40      }
41  
42      @Test
43      public void customUnitTestFileIsGenerated() throws Exception
44      {
45          props.setUseCustomClass(true);
46  
47          getTestSourceFile(PACKAGE_NAME, "MyTabPanelTest");
48      }
49  
50      @Test
51      public void customTemplateFileIsGenerated() throws Exception
52      {
53          props.setUseCustomClass(true);
54  
55          getResourceFile("templates/tabpanels", "my-tab-panel.vm");
56      }
57      
58      @Test
59      public void genericClassFilesAreNotGenerated() throws Exception
60      {
61          props.setUseCustomClass(false);
62  
63          assertTrue(getChangesetForModule().getSourceFiles().isEmpty());
64      }
65  
66      @Test
67      public void customModuleHasClass() throws Exception
68      {
69          props.setUseCustomClass(true);
70          
71          assertEquals(PACKAGE_NAME + ".MyTabPanel", getGeneratedModule().attributeValue("class"));
72      }
73      
74      @Test
75      public void genericModuleHasGenericClass() throws Exception
76      {
77          props.setFullyQualifiedClassname(ComponentTabPanelModuleCreator.FQ_GENERIC_CLASS);
78          props.setUseCustomClass(true);
79          
80          assertEquals(ComponentTabPanelModuleCreator.FQ_GENERIC_CLASS, getGeneratedModule().attributeValue("class"));
81      }
82      
83      @Test
84      public void moduleHasOrder() throws Exception
85      {
86          props.setOrder(10);
87          
88          assertEquals("10", getGeneratedModule().selectSingleNode("order").getText());
89      }
90      
91      @Test
92      public void labelIsAdded() throws Exception
93      {
94          props.setLabel(label);
95          
96          assertNotNull(getGeneratedModule().selectSingleNode("label"));
97      }
98      
99      @Test
100     public void labelHasI18nKey() throws Exception
101     {
102         props.setLabel(label);
103         
104         assertEquals(label.getKey(), getGeneratedModule().selectSingleNode("label/@key").getText());
105     }
106 
107     @Test
108     public void labelHasParams() throws Exception
109     {
110         props.setLabel(label);
111         
112         assertEquals(2, getGeneratedModule().selectNodes("label/param").size());
113     }
114 
115     @Test
116     public void labelParam0HasName() throws Exception
117     {
118         props.setLabel(label);
119         
120         assertEquals("param0", getGeneratedModule().selectSingleNode("label/param[1]/@name").getText());
121     }
122 
123     @Test
124     public void labelParam0HasValue() throws Exception
125     {
126         props.setLabel(label);
127         
128         assertEquals(label.getParams().get("param0"), getGeneratedModule().selectSingleNode("label/param[1]/@value").getText());
129     }
130 
131     @Test
132     public void labelParam1HasName() throws Exception
133     {
134         props.setLabel(label);
135         
136         assertEquals("param1", getGeneratedModule().selectSingleNode("label/param[2]/@name").getText());
137     }
138 
139     @Test
140     public void labelParam1HasValue() throws Exception
141     {
142         props.setLabel(label);
143         
144         assertEquals(label.getParams().get("param1"), getGeneratedModule().selectSingleNode("label/param[2]/@value").getText());
145     }
146 
147     @Test
148     public void labelStringIsAddedToI18nProperties() throws Exception
149     {
150         props.setLabel(label);
151         
152         assertEquals(label.getValue(), getChangesetForModule().getI18nProperties().get(label.getKey()));
153     }
154 
155     @Test
156     public void labelIsUsedInViewTemplate() throws Exception
157     {
158         props.setLabel(label);
159         props.setUseCustomClass(true);
160         
161         Document viewDoc = DocumentHelper.parseText(getChangesetForModule().getResourceFiles().get(0).getContent());
162         assertNotNull(viewDoc.selectSingleNode("/div/h3[text() = \"$i18n.getText('" + label.getKey() + "')\"]"));
163     }
164 }