001: package de.java2html.test;
002:
003: import java.io.IOException;
004: import java.io.StringReader;
005:
006: import de.java2html.Java2Html;
007: import de.java2html.JavaSourceConversionSettings;
008: import de.java2html.javasource.JavaSource;
009: import de.java2html.javasource.JavaSourceIterator;
010: import de.java2html.javasource.JavaSourceParser;
011: import de.java2html.javasource.JavaSourceRun;
012: import de.java2html.options.HorizontalAlignment;
013: import de.java2html.options.JavaSourceConversionOptions;
014: import junit.framework.TestCase;
015:
016: public class Java2HtmlTest extends TestCase {
017:
018: public void testConvertToHtml() {
019: //Just a smoke test
020: String result = Java2Html
021: .convertToHtml("public static void main(){}"); //$NON-NLS-1$
022: assertNotNull(result);
023: assertTrue(result.indexOf("public") != -1); //$NON-NLS-1$
024: assertTrue(result.indexOf("static") != -1); //$NON-NLS-1$
025: assertTrue(result.indexOf("void") != -1); //$NON-NLS-1$
026: assertTrue(result.indexOf("main") != -1); //$NON-NLS-1$
027: assertTrue(result.indexOf("<font") != -1); //$NON-NLS-1$
028: assertTrue(result.indexOf("</font>") != -1); //$NON-NLS-1$
029: assertTrue(result.indexOf("public static") == -1); //$NON-NLS-1$
030: }
031:
032: public void testConvertWithTabs() {
033: String result = Java2Html
034: .convertToHtml("public static void main(){\t\t}"); //$NON-NLS-1$
035: assertNotNull(result);
036: }
037:
038: public void testConvertToHtmlPage() {
039: //Just a smoke test
040: String result = Java2Html
041: .convertToHtmlPage("public static void main(){}"); //$NON-NLS-1$
042: assertNotNull(result);
043: assertTrue(result.indexOf("public") != -1); //$NON-NLS-1$
044: assertTrue(result.indexOf("static") != -1); //$NON-NLS-1$
045: assertTrue(result.indexOf("void") != -1); //$NON-NLS-1$
046: assertTrue(result.indexOf("main") != -1); //$NON-NLS-1$
047: assertTrue(result.indexOf("<font") != -1); //$NON-NLS-1$
048: assertTrue(result.indexOf("</font>") != -1); //$NON-NLS-1$
049: assertTrue(result.indexOf("public static") == -1); //$NON-NLS-1$
050: assertTrue(result.indexOf("<html>") != -1); //$NON-NLS-1$
051: assertTrue(result.indexOf("</html>") != -1); //$NON-NLS-1$
052: }
053:
054: public void testConvertBug0() {
055: String result = Java2Html
056: .convertToHtmlPage("package com.equate.core.bean;\r\n" //$NON-NLS-1$
057: + "\r\n" //$NON-NLS-1$
058: + "public interface ResultBean {\r\n" //$NON-NLS-1$
059: + " \r\n" //$NON-NLS-1$
060: + "}\r\n" //$NON-NLS-1$
061: + "\r\n" //$NON-NLS-1$
062: + "\r\n"); //$NON-NLS-1$
063: assertNotNull(result);
064: }
065:
066: public void testConvertBug0Base() throws IOException {
067: String javaSource = "\r\n\r\n"; //$NON-NLS-1$
068:
069: StringReader stringReader = new StringReader(javaSource);
070: JavaSource source = new JavaSourceParser().parse(stringReader);
071:
072: //System.err.println("'"+source.getCode()+"'");
073: //System.err.println(source.getCode().length()+"
074: // "+source.getClassification().length);
075:
076: // assertNotNull(source);
077:
078: JavaSourceIterator iterator = source.getIterator();
079: JavaSourceRun run1 = iterator.getNext();
080: assertNotNull(run1);
081:
082: // System.err.println("'"+run1.getCode()+"'");
083: // System.err.println("'"+run1.getType()+"'");
084: // System.err.println("'"+run1.isAtStartOfLine()+"'");
085: // System.err.println("'"+run1.isAtEndOfLine()+"'");
086:
087: JavaSourceRun run2 = iterator.getNext();
088: assertNotNull(run2);
089: // System.err.println("'"+run2.getCode()+"'");
090: // System.err.println("'"+run2.getType()+"'");
091: // System.err.println("'"+run2.isAtStartOfLine()+"'");
092: // System.err.println("'"+run2.isAtEndOfLine()+"'");
093:
094: Java2Html.convertToHtmlPage(javaSource);
095: }
096:
097: public void testOptionsDefaultToNoAchor() {
098: assertFalse(
099: "By specification the html anchors must be off in default options.", JavaSourceConversionOptions //$NON-NLS-1$
100: .getDefault().isAddLineAnchors());
101: }
102:
103: public void testNoAnchors() {
104: JavaSourceConversionOptions options = JavaSourceConversionOptions
105: .getDefault();
106: String result = createHelloWorldConversionResult(options);
107: assertTrue(result.indexOf("<a name=\"1\">") == -1); //$NON-NLS-1$
108: assertTrue(result.indexOf("<a name=\"2\">") == -1); //$NON-NLS-1$
109: assertTrue(result.indexOf("<a name=\"3\">") == -1); //$NON-NLS-1$
110: }
111:
112: public void testAnchors() {
113: JavaSourceConversionOptions options = JavaSourceConversionOptions
114: .getDefault();
115: options.setAddLineAnchors(true);
116: String result = createHelloWorldConversionResult(options);
117: assertTrue(result.indexOf("<a name=\"1\">") != -1); //$NON-NLS-1$
118: assertTrue(result.indexOf("<a name=\"2\">") != -1); //$NON-NLS-1$
119: assertTrue(result.indexOf("<a name=\"3\">") != -1); //$NON-NLS-1$
120: }
121:
122: public void testAnchorPrefix() {
123: JavaSourceConversionOptions options = JavaSourceConversionOptions
124: .getDefault();
125: options.setAddLineAnchors(true);
126: options.setLineAnchorPrefix("prefix"); //$NON-NLS-1$
127: String result = createHelloWorldConversionResult(options);
128: assertTrue(result.indexOf("<a name=\"prefix1\">") != -1); //$NON-NLS-1$
129: assertTrue(result.indexOf("<a name=\"prefix2\">") != -1); //$NON-NLS-1$
130: assertTrue(result.indexOf("<a name=\"prefix3\">") != -1); //$NON-NLS-1$
131: }
132:
133: public void testLeftTableAlignment() {
134: JavaSourceConversionOptions options = JavaSourceConversionOptions
135: .getDefault();
136: options.setHorizontalAlignment(HorizontalAlignment.LEFT);
137: String result = createHelloWorldConversionResult(options);
138: assertTrue(result.indexOf(" align=\"left\"") != -1); //$NON-NLS-1$
139: }
140:
141: public void testCenterTableAlignment() {
142: JavaSourceConversionOptions options = JavaSourceConversionOptions
143: .getDefault();
144: options.setHorizontalAlignment(HorizontalAlignment.CENTER);
145: String result = createHelloWorldConversionResult(options);
146: assertTrue(result.indexOf(" align=\"center\"") != -1); //$NON-NLS-1$
147: }
148:
149: public void testRightTableAlignment() {
150: JavaSourceConversionOptions options = JavaSourceConversionOptions
151: .getDefault();
152: options.setHorizontalAlignment(HorizontalAlignment.RIGHT);
153: String result = createHelloWorldConversionResult(options);
154: assertTrue(result.indexOf(" align=\"right\"") != -1); //$NON-NLS-1$
155: }
156:
157: private String createHelloWorldConversionResult(
158: JavaSourceConversionOptions options) {
159: return Java2Html
160: .convertToHtmlPage(
161: "public static void main(){\n System.out.println(\"Hello\");\n}", //$NON-NLS-1$
162: new JavaSourceConversionSettings(options));
163: }
164: }
|