View Javadoc
1   package org.apache.maven.jxr;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.Locale;
27  
28  import org.apache.maven.jxr.pacman.FileManager;
29  import org.apache.maven.jxr.pacman.PackageManager;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   * JUnit test for {@link JavaCodeTransform}.
37   */
38  public class JavaCodeTransformTest
39  {
40      /** JavaCodeTransform object under test */
41      private JavaCodeTransform codeTransform;
42  
43      /**
44       * Set up this test.
45       */
46      @Before
47      public void setUp()
48      {
49          FileManager fileManager = new FileManager();
50          codeTransform = new JavaCodeTransform( new PackageManager( fileManager ), fileManager);
51      }
52  
53      /**
54       * Test basic transformation of a java source file.
55       */
56      @Test
57      public void testTransform()
58          //test transforms its own sourcefile, so add some comments
59          throws Exception // single line despite /*
60      {
61          Path sourceFile = Paths.get( "src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java" );
62          assertTrue( /* mid-line comment */ Files.exists( sourceFile ) ); /*
63  
64          multiline comment text
65  
66          */ codeTransform.transform( sourceFile, Paths.get( "target/JavaCodeTransformTest.html" ) // additional comment
67             , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "./javadocs-test" ), "", "" );
68          assertTrue( /**/ Files.exists( Paths.get( "target/JavaCodeTransformTest.html" ) ) );
69  
70          byte[] bytes = Files.readAllBytes( Paths.get( "target/JavaCodeTransformTest.html" ) );
71          String content = new String( bytes, StandardCharsets.ISO_8859_1 );
72          assertTrue( content.contains( "<title>JavaCodeTransformTest xref</title>" ) );
73          assertTrue( content.contains( "<a href=\"./javadocs-test/org/apache/maven/jxr/JavaCodeTransformTest.html\">"
74                                            + "View Javadoc</a>" ) );
75      }
76  
77      /**
78       * Test what happens with an empty sourcefile.
79       */
80      @Test
81      public void testTransformWithEmptyClassFile()
82          throws Exception
83      {
84          Path sourceFile = Paths.get( "src/test/resources/EmptyClass.java" );
85          assertTrue( Files.exists( sourceFile ) );
86  
87          codeTransform.transform( sourceFile, Paths.get( "target/EmptyClass.html" )
88              , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "javadocs" ), "", "" );
89          assertTrue( Files.exists( Paths.get( "target/EmptyClass.html" ) ) );
90  
91          byte[] bytes = Files.readAllBytes( Paths.get( "target/EmptyClass.html" ) );
92          String content = new String( bytes, StandardCharsets.ISO_8859_1 );
93          assertTrue( content.contains( "<title>EmptyClass xref</title>" ) );
94          assertTrue( content.contains( "<a href=\"javadocs/EmptyClass.html\">View Javadoc</a>" ) );
95      }
96  
97      /**
98       * Test proper handling of link
99       */
100     @Test
101     public void testLinkHandling()
102         throws Exception
103     {
104         Path sourceFile = Paths.get( "src/test/resources/ClassWithLink.java" );
105         assertTrue( Files.exists( sourceFile ) );
106 
107         codeTransform.transform( sourceFile, Paths.get( "target/ClassWithLink.html" )
108             , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
109         assertTrue( Files.exists( Paths.get( "target/ClassWithLink.html" ) ) );
110 
111         byte[] bytes = Files.readAllBytes( Paths.get( "target/ClassWithLink.html" ) );
112         String content = new String( bytes, StandardCharsets.ISO_8859_1 );
113         // The proper link in its full length
114         assertTrue( content.contains(
115             "<a href=\"http://www.apache.org/licenses/LICENSE-2.0\" " +
116             "target=\"alexandria_uri\">http://www.apache.org/licenses/LICENSE-2.0</a></em>" ) );
117         // ...and the same link with https protocol
118         assertTrue( content.contains(
119             "<a href=\"https://www.apache.org/licenses/LICENSE-2.0\" " +
120             "target=\"alexandria_uri\">https://www.apache.org/licenses/LICENSE-2.0</a></em>" ) );
121 
122     }
123 
124     /**
125      * Test what happens with unknown java type.
126      */
127     @Test
128     public void testTransformWithUnknownJavaType()
129         throws Exception
130     {
131         Path sourceFile = Paths.get( "src/test/resources/UnknownType.java" );
132         assertTrue( Files.exists( sourceFile ) );
133 
134         codeTransform.transform( sourceFile, Paths.get( "target/UnknownType.html" )
135             , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "javadocs" ), "", "" );
136         assertTrue( Files.exists( Paths.get( "target/UnknownType.html" ) ) );
137 
138         byte[] bytes = Files.readAllBytes( Paths.get( "target/UnknownType.html" ) );
139         String content = new String( bytes, StandardCharsets.ISO_8859_1 );
140         assertTrue( content.contains( "<title>UnknownType xref</title>" ) );
141         assertTrue( content.contains( "<a href=\"javadocs/example/UnknownType.html\">View Javadoc</a>" ) );
142     }
143 
144 }