Project Testing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(16 intermediate revisions by the same user not shown)
Line 12: Line 12:


<properties>
<properties>
    <novaordis.utilities.version>4.4.0-SNAPSHOT-6</novaordis.utilities.version>
     <test.scratch.directory>target/test-scratch</test.scratch.directory>
     <test.scratch.directory>target/test-scratch</test.scratch.directory>
</properties>
</properties>


</pre>
Note that the below section must be declared under the <plugins> and not <pluginManagement>. The novaordis-utilities dependency is necessary for its Files utilities.
<pre>
...
...


<plugin>
<build>
    <groupId>org.apache.maven.plugins</groupId>
    <plugins>
    <artifactId>maven-antrun-plugin</artifactId>
        <plugin>
     <dependencies>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant</artifactId>
                    <version>1.7.0</version>
                  </dependency>
              </dependencies>
              <executions>
                  <execution>
                      <phase>generate-test-resources</phase>
                              <configuration>
                          <tasks>
                              <mkdir dir="${test.scratch.directory}" />
                          </tasks>
                      </configuration>
                      <goals>
                          <goal>run</goal>
                      </goals>
                </execution>
            </executions>
        </plugin>
     </plugins>
  ...
  <dependencies>
         <dependency>
         <dependency>
             <groupId>org.apache.ant</groupId>
             <groupId>io.novaordis.utilities</groupId>
             <artifactId>ant</artifactId>
             <artifactId>novaordis-utilities</artifactId>
             <version>1.7.0</version>
             <version>${novaordis.utilities.version}</version>
        </dependency>
            <scope>test</scope>
    </dependencies>
        </dependency>
    <executions>
  </dependencies>
        <execution>
  ...
            <phase>generate-test-resources</phase>
</build>
            <configuration>
                <tasks>
                    <mkdir dir="${test.scratch.directory}" />
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>


</pre>
</pre>


==Code==
==Code==
===Initialization===
<pre>
    ...
    protected File scratchDirectory;
    protected File baseDirectory;
    @Before
    public void before() throws Exception {
        String projectBaseDirName = System.getProperty("basedir");
        scratchDirectory = new File(projectBaseDirName, "target/test-scratch");
        assertTrue(scratchDirectory.isDirectory());
        baseDirectory = new File(System.getProperty("basedir"));
        assertTrue(baseDirectory.isDirectory());
    }
    ...
</pre>
===Access from Test===
<pre>
    ...
   
    scratchDirectory ...
    baseDirectory ...
  ...
   
</pre>
===Cleanup after Test===


<pre>
<pre>
String projectBaseDirName = System.getProperty("basedir");


File testScratchDir = new File(projectBaseDirName, "target/scratch");
    ...
 
    @After
    public void after() throws Exception {
       
        //
        // scratch directory cleanup
        //
 
        assertTrue(io.novaordis.utilities.Files.rmdir(scratchDirectory, false));
 
    }
 
    ...
 
</pre>
 
=Accessing Resource Files=


...
<pre>
String projectBaseDirName = System.getProperty("basedir");
File dir = new File(projectBaseDirName, "src/test/resources/data");
</pre>
</pre>

Latest revision as of 20:44, 19 April 2017

Internal

Testing Files in the Scratch Area of the Project

Maven Configuration


...

<properties>
    <novaordis.utilities.version>4.4.0-SNAPSHOT-6</novaordis.utilities.version>
    <test.scratch.directory>target/test-scratch</test.scratch.directory>
</properties>

Note that the below section must be declared under the <plugins> and not <pluginManagement>. The novaordis-utilities dependency is necessary for its Files utilities.

...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <dependencies>
                 <dependency>
                     <groupId>org.apache.ant</groupId>
                     <artifactId>ant</artifactId>
                     <version>1.7.0</version>
                  </dependency>
              </dependencies>
              <executions>
                  <execution>
                      <phase>generate-test-resources</phase>
                               <configuration>
                          <tasks>
                              <mkdir dir="${test.scratch.directory}" />
                          </tasks>
                      </configuration>
                      <goals>
                          <goal>run</goal>
                      </goals>
                 </execution>
             </executions>
         </plugin>
    </plugins>
   ...
   <dependencies>
        <dependency>
            <groupId>io.novaordis.utilities</groupId>
            <artifactId>novaordis-utilities</artifactId>
            <version>${novaordis.utilities.version}</version>
            <scope>test</scope>
        </dependency>
   </dependencies>
   ...
</build>

Code

Initialization


    ...

    protected File scratchDirectory;
    protected File baseDirectory;

    @Before
    public void before() throws Exception {

        String projectBaseDirName = System.getProperty("basedir");
        scratchDirectory = new File(projectBaseDirName, "target/test-scratch");
        assertTrue(scratchDirectory.isDirectory());

        baseDirectory = new File(System.getProperty("basedir"));
        assertTrue(baseDirectory.isDirectory());
    }

    ...

Access from Test


    ...
    
    scratchDirectory ...

    baseDirectory ...

   ...
    

Cleanup after Test


    ...

    @After
    public void after() throws Exception {
        
        //
        // scratch directory cleanup
        //

        assertTrue(io.novaordis.utilities.Files.rmdir(scratchDirectory, false));

    }

    ...

Accessing Resource Files

String projectBaseDirName = System.getProperty("basedir");
File dir = new File(projectBaseDirName, "src/test/resources/data");