1   package org.apache.maven.javadoc;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  import java.io.BufferedReader;
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.FileReader;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  import junit.textui.TestRunner;
29  
30  /**
31   * Test case.
32   *
33   * @author Steven Caswell
34   * @version $Id: JavadocWarningsTextToXmlTest.java 531612 2007-04-23 21:28:38Z ltheussl $
35   */
36  public class JavadocWarningsTextToXmlTest extends TestCase
37  {
38      private static final String INPUT_FILE_NAME_EXCEPTION = "Input file name must be specified";
39      private static final String OUTPUT_FILE_NAME_EXCEPTION = "Output file name must be specified";
40  
41      /**
42       * Creates a new JavadocWarningsTextToXmlTest object.
43       *
44       */
45      public JavadocWarningsTextToXmlTest(final String name)
46      {
47          super(name);
48      }
49  
50      public static void main(final String[] main)
51      {
52          TestRunner.run(suite());
53      }
54  
55      public static Test suite()
56      {
57          return new TestSuite(JavadocWarningsTextToXmlTest.class);
58      }
59  
60      /**
61       * Initialize per test data
62       *
63       * @throws Exception when there is an unexpected problem
64       */
65      public void setUp() throws Exception
66      {
67          String baseDir = System.getProperty("basedir");
68  
69          System.out.println("Base dir : " + baseDir);
70          assertNotNull("The system property basedir was not defined.", baseDir);
71      }
72  
73      public void testBuild() throws Exception
74      {
75          JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();
76  
77          bean.setVerbose(true);
78          bean.setInputFileName("target/test-classes/report.txt");
79          bean.setOutputFileName("test.xml");
80          bean.setOutputEncoding("ISO-8859-1");
81          bean.build();
82  
83          // Read and test the output file contents
84          BufferedReader reader = new BufferedReader(new FileReader("test.xml"));
85          String line = reader.readLine();
86  
87          assertTrue("line 1 start",
88              line.startsWith("<?xml version=\"1.0\" encoding=\""));
89          assertTrue("line 1 end", line.endsWith("ISO-8859-1\"?>"));
90          line = reader.readLine();
91          assertEquals("line 2", "<javadoc>", line);
92          line = reader.readLine();
93          assertTrue("line 3 start", line.startsWith("<file name"));
94          assertTrue("line 3 end", line.endsWith("Javadoc1.java\">"));
95          line = reader.readLine();
96          assertEquals("line 4",
97              "<error line=\"8\" severity=\"warning\" message=\"@inheritDoc used but method1() does not override or implement any method.\"/>",
98              line);
99          line = reader.readLine();
100         assertEquals("line 5", "</file>", line);
101         line = reader.readLine();
102         assertTrue("line 6 start", line.startsWith("<file name"));
103         assertTrue("line 6 end", line.endsWith("Javadoc2.java\">"));
104         line = reader.readLine();
105         assertEquals("line 7",
106             "<error line=\"8\" severity=\"warning\" message=\"@todo is an unknown tag.\"/>",
107             line);
108         line = reader.readLine();
109         assertEquals("line 8",
110             "<error line=\"15\" severity=\"warning\" message=\"Tag @link: malformed: &quot;Javadoc1#method1(&quot;\"/>",
111             line);
112         line = reader.readLine();
113         assertEquals("line 9", "</file>", line);
114         line = reader.readLine();
115         assertEquals("line 10", "</javadoc>", line);
116 
117         reader.close();
118 
119         // Get rid of the xml file
120         File file = new File("test.xml");
121 
122         file.delete();
123     }
124 
125     public void testInputFile()
126         throws Exception
127     {
128         JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();
129 
130         bean.setOutputFileName("test.xml");
131         bean.setOutputEncoding("ISO-8859-1]");
132 
133         try
134         {
135             bean.build();
136         }
137         catch (IllegalArgumentException e)
138         {
139             assertEquals("exception message", INPUT_FILE_NAME_EXCEPTION,
140                 e.getMessage());
141         }
142     }
143 
144     public void testInputFileNotFound()
145         throws Exception
146     {
147         JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();
148 
149         bean.setInputFileName("junk.dat");
150         bean.setOutputFileName("test.xml");
151         bean.setOutputEncoding("ISO-8859-1]");
152 
153         try
154         {
155             bean.build();
156             fail("Expected null pointer exception for input file name");
157         }
158         catch (FileNotFoundException e)
159         {
160             ; // expected file not found
161         }
162     }
163 
164     public void testOutputFile()
165         throws Exception
166     {
167         JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();
168 
169         bean.setInputFileName("report.txt");
170         bean.setOutputEncoding("ISO-8859-1]");
171 
172         try
173         {
174             bean.build();
175             fail("Expected null pointer exception for input file name");
176         }
177         catch (IllegalArgumentException e)
178         {
179             assertEquals("exception message", OUTPUT_FILE_NAME_EXCEPTION,
180                 e.getMessage());
181         }
182     }
183 }