KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > test > Java2HtmlTest


1 package de.java2html.test;
2
3 import java.io.IOException JavaDoc;
4 import java.io.StringReader JavaDoc;
5
6 import de.java2html.Java2Html;
7 import de.java2html.JavaSourceConversionSettings;
8 import de.java2html.javasource.JavaSource;
9 import de.java2html.javasource.JavaSourceIterator;
10 import de.java2html.javasource.JavaSourceParser;
11 import de.java2html.javasource.JavaSourceRun;
12 import de.java2html.options.HorizontalAlignment;
13 import de.java2html.options.JavaSourceConversionOptions;
14 import junit.framework.TestCase;
15
16 public class Java2HtmlTest extends TestCase {
17
18   public void testConvertToHtml() {
19     //Just a smoke test
20
String JavaDoc result = Java2Html.convertToHtml("public static void main(){}"); //$NON-NLS-1$
21
assertNotNull(result);
22     assertTrue(result.indexOf("public") != -1); //$NON-NLS-1$
23
assertTrue(result.indexOf("static") != -1); //$NON-NLS-1$
24
assertTrue(result.indexOf("void") != -1); //$NON-NLS-1$
25
assertTrue(result.indexOf("main") != -1); //$NON-NLS-1$
26
assertTrue(result.indexOf("<font") != -1); //$NON-NLS-1$
27
assertTrue(result.indexOf("</font>") != -1); //$NON-NLS-1$
28
assertTrue(result.indexOf("public static") == -1); //$NON-NLS-1$
29
}
30
31   public void testConvertWithTabs() {
32     String JavaDoc result = Java2Html.convertToHtml("public static void main(){\t\t}"); //$NON-NLS-1$
33
assertNotNull(result);
34   }
35
36   public void testConvertToHtmlPage() {
37     //Just a smoke test
38
String JavaDoc result = Java2Html.convertToHtmlPage("public static void main(){}"); //$NON-NLS-1$
39
assertNotNull(result);
40     assertTrue(result.indexOf("public") != -1); //$NON-NLS-1$
41
assertTrue(result.indexOf("static") != -1); //$NON-NLS-1$
42
assertTrue(result.indexOf("void") != -1); //$NON-NLS-1$
43
assertTrue(result.indexOf("main") != -1); //$NON-NLS-1$
44
assertTrue(result.indexOf("<font") != -1); //$NON-NLS-1$
45
assertTrue(result.indexOf("</font>") != -1); //$NON-NLS-1$
46
assertTrue(result.indexOf("public static") == -1); //$NON-NLS-1$
47
assertTrue(result.indexOf("<html>") != -1); //$NON-NLS-1$
48
assertTrue(result.indexOf("</html>") != -1); //$NON-NLS-1$
49
}
50
51   public void testConvertBug0() {
52     String JavaDoc result = Java2Html.convertToHtmlPage("package com.equate.core.bean;\r\n" //$NON-NLS-1$
53
+ "\r\n" //$NON-NLS-1$
54
+ "public interface ResultBean {\r\n" //$NON-NLS-1$
55
+ " \r\n" //$NON-NLS-1$
56
+ "}\r\n" //$NON-NLS-1$
57
+ "\r\n" //$NON-NLS-1$
58
+ "\r\n"); //$NON-NLS-1$
59
assertNotNull(result);
60   }
61
62   public void testConvertBug0Base() throws IOException JavaDoc {
63     String JavaDoc javaSource = "\r\n\r\n"; //$NON-NLS-1$
64

65     StringReader JavaDoc stringReader = new StringReader JavaDoc(javaSource);
66     JavaSource source = new JavaSourceParser().parse(stringReader);
67
68     //System.err.println("'"+source.getCode()+"'");
69
//System.err.println(source.getCode().length()+"
70
// "+source.getClassification().length);
71

72     // assertNotNull(source);
73

74     JavaSourceIterator iterator = source.getIterator();
75     JavaSourceRun run1 = iterator.getNext();
76     assertNotNull(run1);
77
78     // System.err.println("'"+run1.getCode()+"'");
79
// System.err.println("'"+run1.getType()+"'");
80
// System.err.println("'"+run1.isAtStartOfLine()+"'");
81
// System.err.println("'"+run1.isAtEndOfLine()+"'");
82

83     JavaSourceRun run2 = iterator.getNext();
84     assertNotNull(run2);
85     // System.err.println("'"+run2.getCode()+"'");
86
// System.err.println("'"+run2.getType()+"'");
87
// System.err.println("'"+run2.isAtStartOfLine()+"'");
88
// System.err.println("'"+run2.isAtEndOfLine()+"'");
89

90     Java2Html.convertToHtmlPage(javaSource);
91   }
92
93   public void testOptionsDefaultToNoAchor() {
94     assertFalse("By specification the html anchors must be off in default options.", JavaSourceConversionOptions //$NON-NLS-1$
95
.getDefault().isAddLineAnchors());
96   }
97
98   public void testNoAnchors() {
99     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
100     String JavaDoc result = createHelloWorldConversionResult(options);
101     assertTrue(result.indexOf("<a name=\"1\">") == -1); //$NON-NLS-1$
102
assertTrue(result.indexOf("<a name=\"2\">") == -1); //$NON-NLS-1$
103
assertTrue(result.indexOf("<a name=\"3\">") == -1); //$NON-NLS-1$
104
}
105
106   public void testAnchors() {
107     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
108     options.setAddLineAnchors(true);
109     String JavaDoc result = createHelloWorldConversionResult(options);
110     assertTrue(result.indexOf("<a name=\"1\">") != -1); //$NON-NLS-1$
111
assertTrue(result.indexOf("<a name=\"2\">") != -1); //$NON-NLS-1$
112
assertTrue(result.indexOf("<a name=\"3\">") != -1); //$NON-NLS-1$
113
}
114
115   public void testAnchorPrefix() {
116     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
117     options.setAddLineAnchors(true);
118     options.setLineAnchorPrefix("prefix"); //$NON-NLS-1$
119
String JavaDoc result = createHelloWorldConversionResult(options);
120     assertTrue(result.indexOf("<a name=\"prefix1\">") != -1); //$NON-NLS-1$
121
assertTrue(result.indexOf("<a name=\"prefix2\">") != -1); //$NON-NLS-1$
122
assertTrue(result.indexOf("<a name=\"prefix3\">") != -1); //$NON-NLS-1$
123
}
124
125   public void testLeftTableAlignment() {
126     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
127     options.setHorizontalAlignment(HorizontalAlignment.LEFT);
128     String JavaDoc result = createHelloWorldConversionResult(options);
129     assertTrue(result.indexOf(" align=\"left\"") != -1); //$NON-NLS-1$
130
}
131
132   public void testCenterTableAlignment() {
133     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
134     options.setHorizontalAlignment(HorizontalAlignment.CENTER);
135     String JavaDoc result = createHelloWorldConversionResult(options);
136     assertTrue(result.indexOf(" align=\"center\"") != -1); //$NON-NLS-1$
137
}
138
139   public void testRightTableAlignment() {
140     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
141     options.setHorizontalAlignment(HorizontalAlignment.RIGHT);
142     String JavaDoc result = createHelloWorldConversionResult(options);
143     assertTrue(result.indexOf(" align=\"right\"") != -1); //$NON-NLS-1$
144
}
145
146   private String JavaDoc createHelloWorldConversionResult(JavaSourceConversionOptions options) {
147     return Java2Html.convertToHtmlPage(
148         "public static void main(){\n System.out.println(\"Hello\");\n}", //$NON-NLS-1$
149
new JavaSourceConversionSettings(options));
150   }
151 }
Popular Tags