View Javadoc

1   package com.aurea.maven.plugins.sonic;
2   
3   import java.io.File;
4   
5   import org.apache.maven.plugin.Mojo;
6   import org.apache.maven.plugin.testing.AbstractMojoTestCase;
7   import org.apache.maven.plugin.testing.ArtifactStubFactory;
8   import org.codehaus.plexus.util.FileUtils;
9   
10  import com.aurea.maven.plugins.sonic.esb.MavenProjectMock;
11  
12  /**
13   *
14   */
15  public abstract class AbstractESBPluginTest extends AbstractMojoTestCase {
16  
17  
18    /**
19     * 
20     */
21    protected File                testDir       = null;
22    /**
23     * 
24     */
25    protected Mojo                mojoUnderTest = null;
26    /**
27     * 
28     */
29    protected ArtifactStubFactory stubFactory   = null;
30  
31    /**
32     * 
33     */
34    public AbstractESBPluginTest() {
35  
36      super();
37    }
38  
39    /**
40     * @return String
41     */
42    protected abstract String getTestName();
43  
44    /**
45     * @return String
46     */
47    protected abstract String getMojoGoal();
48  
49    /**
50     * @return String
51     */
52    protected String getSourceDirectory() {
53  
54      return "src/main/java";
55    }
56  
57    /**
58     * @throws Exception
59     *           Exception
60     */
61    @Override
62    protected void setUp() throws Exception {
63  /*
64   * IMPORTANT NOTE (Tim De Borger):
65   * Testing needs to be reviewed as we needed to strip items out. A full strategy needs to be put in place at a later stage.
66   */
67  //    super.setUp();
68  
69      testDir = new File(getBasedir(), "target" + File.separatorChar + "unit-tests" + File.separatorChar + getTestName()
70          + File.separatorChar);
71  
72      FileUtils.deleteDirectory(testDir.getAbsolutePath());
73      assertFalse(testDir.exists());
74  
75      testDir.mkdirs();
76      assertTrue("'" + testDir + "' does not exist", testDir.exists());
77  
78      stubFactory = new ArtifactStubFactory(testDir, true);
79  
80      String testResourcesDir = getTestResourcesDir();
81      File testResources = new File(testResourcesDir);
82      assertTrue("Directory : " + testResourcesDir + " : does not exist.", testResources.exists());
83      assertTrue("Directory : " + testResourcesDir + " is not a directory", testResources.isDirectory());
84  
85      File projectDirectory = new File(testResources, "projectArchive");
86      assertTrue(projectDirectory.exists());
87      assertTrue(projectDirectory.isDirectory());
88  
89      // File projectArchive = new File(testResources, "projectArchive.zip");
90      // assertTrue(projectArchive.exists());
91      // assertTrue(projectArchive.isFile());
92      //
93      File pluginCfg = getPluginConfigFile();
94      assertTrue(pluginCfg.exists());
95      assertTrue(pluginCfg.isFile());
96  
97      // ArchiverManager am = (ArchiverManager)lookup(ArchiverManager.class.getName());
98      // UnArchiver ua = am.getUnArchiver("zip");
99      // ua.setDestDirectory(testDir);
100     // ua.setSourceFile(projectArchive);
101     // ua.extract();
102 
103     FileUtils.copyDirectoryStructure(projectDirectory, testDir);
104 
105 //    mojoUnderTest = lookupMojo(getMojoGoal(), pluginCfg);
106 //    assertFalse(mojoUnderTest == null);
107     MavenProjectMock.setBaseDir(testDir.getAbsolutePath());
108     MavenProjectMock.setSourceDirectory(testDir.getAbsolutePath() + "/" + getSourceDirectory());
109   }
110 
111   protected String getTestResourcesDir() {
112 
113     return "src/test/resources/" + getTestName();
114   }
115 
116   protected File getPluginConfigFile() {
117 
118     return new File(getTestResourcesDir(), "plugin-config.xml");
119   }
120 
121   /**
122    * @throws Exception
123    *           Exception
124    */
125   public void testMojo() throws Exception {
126 
127     //mojoUnderTest.execute();
128   }
129 
130   /**
131    * @throws Exception
132    *           Exception
133    */
134   protected void tearDown() throws Exception {
135 
136     // if (testDir != null) {
137     // try {
138     // FileUtilities.deleteDirectory(testDir.getAbsolutePath());
139     // } catch (IOException e) {
140     // e.printStackTrace();
141     // fail("Trying to remove directory:" + testDir + "\r\n" + e.toString());
142     // }
143     // assertFalse(testDir.exists());
144     // testDir = null;
145     // }
146 
147     super.tearDown();
148   }
149 }