Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.LogMessageGeneratorTest

LineHitsNoteSource
1  /*
2   * $Id: LogMessageGeneratorTest.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  import java.io.File;
36  import org.apache.tools.ant.BuildException;
37  import org.apache.tools.ant.Location;
38  import org.apache.tools.ant.Project;
39  import org.jcoderz.commons.TestCase;
40  
41  
42  /**
43   * JUnit test for the Ant task
44   * {@link org.jcoderz.commons.taskdefs.LogMessageGenerator}.
45   *
46   * @author Michael Griffel
47   */
48100 public class LogMessageGeneratorTest
49        extends TestCase
50  {
51     private LogMessageGenerator mGenerator;
52     private File mDestDir;
53  
54     /** {@inheritDoc} */
55     protected void setUp ()
56           throws Exception
57     {
58100       super.setUp();
59100       mDestDir = mkdir("build/test");
60100       final File in = new File(getBaseDir(), "test/xml/log-message-info.xml");
61100       final File out = new File(mDestDir, "log-message-info.out");
62100       final LogMessageGenerator g = new LogMessageGenerator();
63100       g.setApplication("FawkeZ");
64100       g.setDestdir(mDestDir);
65100       g.setForce(true);
66100       g.setIn(in);
67100       g.setOut(out);
68100       g.setTaskName("test-message-generator");
69100       g.setFailonerror(true);
70100       g.setLocation(new Location("location"));
71  
72100       final Project project = new Project();
73100       project.setBaseDir(getBaseDir());
74100       project.setName("JUnit test");
75100       g.setProject(project);
76100       mGenerator = g;
77100    }
78  
79     /** Tests the Ant task. */
80     public void testExecute ()
81     {
82100       mGenerator.execute();
83  
84100       final File testFile = new File(mDestDir,
85              "org/jcoderz/commons/TstLogMessage.java");
86100       assertTrue("Generated Java File " + testFile + " exists",
87              testFile.exists());
88100    }
89  
90     /** Tests the Ant task. */
91     public void testExecuteBadInFile ()
92     {
93100       mGenerator.setIn(null);
94100       executeAndExpectBuildException("missing input file.");
95100       mGenerator.setIn(new File("foo"));
96100       executeAndExpectBuildException("non existing input file.");
97100       mGenerator.setIn(new File(getBaseDir(), "build.xml"));
98100       executeAndExpectBuildException("bogus input file.");
99100    }
100  
101     /** Tests the Ant task. */
102     public void testExecuteBadDestDir ()
103     {
104100       mGenerator.setDestdir(null);
105100       executeAndExpectBuildException("missing destination directory.");
106100    }
107  
108     /** Tests the Ant task. */
109     public void testExecuteBadOutFile ()
110     {
111100       mGenerator.setOut(null);
112100       executeAndExpectBuildException("missing out file.");
113100    }
114  
115     /** Tests the Ant task. */
116     public void testExecuteBadApplicationName ()
117     {
118100       mGenerator.setApplication(null);
119100       executeAndExpectBuildException("missing out file.");
120100       mGenerator.setApplication("bogus");
121100       executeAndExpectBuildException("non-existing application name.");
122100    }
123  
124     /** Tests the Ant task. */
125     public void testExecuteBadXslFile ()
126     {
127100       mGenerator.setXsl("bogus.xsl"); // -> use default stylesheet
128100    }
129  
130     /**
131      * @param g
132    */
133     private void executeAndExpectBuildException (String msg)
134     {
135        try
136        {
1370          mGenerator.execute();
1380          fail("Expected BuildException for " + msg);
139        }
140100       catch (BuildException expected)
141        {
142           // expected
143100(1)(2)          System.err.println("" + expected);
144100(3)          expected.printStackTrace();
1450       }
146100    }
147  
148     static File mkdir (String relativePath)
149     {
150100       final File dir = new File(getBaseDir(), relativePath);
151100       if (!dir.exists())
152        {
153100          if (!dir.mkdirs())
154           {
1550             fail("Cannot create destination directory " + dir);
156           }
157        }
158100       return dir;
159     }
160  
161  }

Findings in this File

i (4) LogMessageGeneratorTest.mGenerator not initialized in constructor (test code)
f (5) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
i (1) 143 : 0 method org.jcoderz.commons.taskdefs.LogMessageGeneratorTest.executeAndExpectBuildException(String) concatenates an empty string to effect type conversion (test code) Decreased severity from 'warning' for testcode.
d (2) 143 : 11 System.out.print is used
d (3) 144 : 11 Avoid printStackTrace(); use a logger call instead.