1   package com.atlassian.plugins.codegen.modules.common;
2   
3   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
4   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
5   
6   import org.dom4j.Document;
7   import org.dom4j.Node;
8   import org.junit.Before;
9   import org.junit.Test;
10  
11  import static org.junit.Assert.assertNotNull;
12  
13  /**
14   * @since 3.6
15   */
16  public class TemplateContextItemTest extends AbstractCodegenTestCase<TemplateContextItemProperties>
17  {
18  
19      public static final String MODULE_NAME = "My Template Context Item";
20      public static final String COMPONENT_REF = "i18nResolver";
21      public static final String CLASSNAME = "com.atlassian.component.SomeSingleton";
22      public static final String CONTEXT_KEY = "i18n";
23  
24      @Before
25      public void runGenerator() throws Exception
26      {
27          setCreator(new TemplateContextItemModuleCreator());
28          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
29                  .resourcesDirectory(resourcesDir)
30                  .testDirectory(testDir)
31                  .templateDirectory(templateDir)
32                  .build());
33  
34          setProps(new TemplateContextItemProperties(MODULE_NAME, CONTEXT_KEY));
35  
36          props.setIncludeExamples(false);
37  
38      }
39  
40      @Test
41      public void componentRefModuleIsValid() throws Exception
42      {
43          props.setComponentRef(COMPONENT_REF);
44          creator.createModule(moduleLocation, props);
45  
46          Document pluginDoc = getXmlDocument(pluginXml);
47          String xpath = "/atlassian-plugin/template-context-item[@name='My Template Context Item' and @key='my-template-context-item' and @i18n-name-key='my-template-context-item.name' and @component-ref='" + COMPONENT_REF + "' and @global='false' and @context-key='" + CONTEXT_KEY + "']";
48          Node itemNode = pluginDoc.selectSingleNode(xpath);
49  
50          assertNotNull("component ref context item not found", itemNode);
51      }
52  
53      @Test
54      public void classModuleIsValid() throws Exception
55      {
56          props.setFullyQualifiedClassname(CLASSNAME);
57          creator.createModule(moduleLocation, props);
58  
59          Document pluginDoc = getXmlDocument(pluginXml);
60          String xpath = "/atlassian-plugin/template-context-item[@name='My Template Context Item' and @key='my-template-context-item' and @i18n-name-key='my-template-context-item.name' and @class='" + CLASSNAME + "' and @global='false' and @context-key='" + CONTEXT_KEY + "']";
61          Node itemNode = pluginDoc.selectSingleNode(xpath);
62  
63          assertNotNull("class context item not found", itemNode);
64      }
65  
66      @Test
67      public void globalModuleIsValid() throws Exception
68      {
69          props.setComponentRef(COMPONENT_REF);
70          props.setGlobal(true);
71          creator.createModule(moduleLocation, props);
72  
73          Document pluginDoc = getXmlDocument(pluginXml);
74          String xpath = "/atlassian-plugin/template-context-item[@name='My Template Context Item' and @key='my-template-context-item' and @i18n-name-key='my-template-context-item.name' and @component-ref='" + COMPONENT_REF + "' and @global='true' and @context-key='" + CONTEXT_KEY + "']";
75          Node itemNode = pluginDoc.selectSingleNode(xpath);
76  
77          assertNotNull("component ref context item not found", itemNode);
78      }
79  }