1 package org.apache.maven.jxr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38 public class JavaCodeTransformTest
39 {
40
41 private JavaCodeTransform codeTransform;
42
43
44
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
55
56 @Test
57 public void testTransform()
58
59 throws Exception
60 {
61 Path sourceFile = Paths.get( "src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java" );
62 assertTrue( Files.exists( sourceFile ) );
63
64
65
66 codeTransform.transform( sourceFile, Paths.get( "target/JavaCodeTransformTest.html" )
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
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
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
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
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
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 }