1   package com.atlassian.plugins.codegen.modules.common;
2   
3   import java.io.File;
4   
5   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
6   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
7   
8   import org.apache.commons.io.FilenameUtils;
9   import org.dom4j.Document;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  
16  /**
17   * @since 3.6
18   */
19  public class GadgetTest extends AbstractCodegenTestCase<GadgetProperties>
20  {
21  
22      @Before
23      public void runGenerator() throws Exception
24      {
25          setCreator(new GadgetModuleCreator());
26          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
27                  .resourcesDirectory(resourcesDir)
28                  .testDirectory(testDir)
29                  .templateDirectory(templateDir)
30                  .build());
31  
32          setProps(new GadgetProperties("My Gadget", "gadgets/mygadget/gadget.xml"));
33  
34          props.setIncludeExamples(false);
35  
36          creator.createModule(moduleLocation, props);
37      }
38  
39      @Test
40      public void allFilesAreGenerated() throws Exception
41      {
42  
43          File gadgetFolder = new File(resourcesDir, FilenameUtils.getPath(props.getLocation()));
44  
45          assertTrue("main gadget not generated", new File(gadgetFolder, FilenameUtils.getName(props.getLocation())).exists());
46          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
47  
48      }
49  
50      @Test
51      public void moduleIsValid() throws Exception
52      {
53          String xpath = "/atlassian-plugin/gadget[@name='My Gadget' and @key='my-gadget' and @i18n-name-key='my-gadget.name' and @location='" + props.getLocation() + "']";
54  
55  
56          Document pluginDoc = getXmlDocument(pluginXml);
57  
58          assertNotNull("valid gadget not found", pluginDoc.selectSingleNode(xpath));
59      }
60  
61  }