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