KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > javasource > test > JavaSourceParserTest


1 package de.java2html.javasource.test;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Reader JavaDoc;
5
6 import de.java2html.javasource.JavaSource;
7 import de.java2html.javasource.JavaSourceParser;
8 import de.java2html.javasource.JavaSourceType;
9 import de.java2html.options.JavaSourceConversionOptions;
10
11 /**
12  * @author Markus Gebhard
13  */

14 public class JavaSourceParserTest extends JavaSourceParserTestCase {
15   public void testParseNull() throws IOException JavaDoc {
16     JavaSourceParser parser = new JavaSourceParser();
17     try {
18       parser.parse((Reader JavaDoc) null);
19       fail();
20     }
21     catch (IllegalArgumentException JavaDoc expected) {
22       //expected
23
}
24   }
25
26   public void testParseEmpty() throws IOException JavaDoc {
27     JavaSource source = doParse(""); //$NON-NLS-1$
28
assertNotNull(source);
29     assertEquals("", source.getCode()); //$NON-NLS-1$
30
assertEquals(0, source.getLineCount());
31     assertEquals(0, source.getMaxLineLength());
32     assertNotNull(source.getClassification());
33     assertEquals(0, source.getClassification().length);
34     assertNotNull(source.getStatistic());
35   }
36
37   public void testParseSimple() throws IOException JavaDoc {
38     JavaSource source = doParse("public String text =\"test\";"); //$NON-NLS-1$
39
assertNotNull(source);
40     assertEquals("public String text =\"test\";", source.getCode()); //$NON-NLS-1$
41
assertEquals(1, source.getLineCount());
42     assertEquals(27, source.getMaxLineLength());
43     assertNotNull(source.getClassification());
44     assertEquals(27, source.getClassification().length);
45     assertNotNull(source.getStatistic());
46   }
47
48   public void testParseSingleTab() throws IOException JavaDoc {
49     int tabSize = JavaSourceConversionOptions.getDefault().getTabSize();
50     JavaSource source = doParse("\t"); //$NON-NLS-1$
51
assertNotNull(source);
52     assertEquals(tabSize, source.getCode().length());
53   }
54
55   public void testParseTabs() throws IOException JavaDoc {
56     int tabSize = JavaSourceConversionOptions.getDefault().getTabSize();
57
58     JavaSource source = doParse("\t\tpublic String text =\"test\";"); //$NON-NLS-1$
59
assertNotNull(source);
60     assertEquals(tabSize * 2 + "public String text =\"test\";".length(), source.getCode().length()); //$NON-NLS-1$
61
assertEquals("public String text =\"test\";", source.getCode().trim()); //$NON-NLS-1$
62
}
63
64   public void testParse() throws IOException JavaDoc {
65     JavaSource source = doParse("package test.it; public class Main{ public static void main(String [] " //$NON-NLS-1$
66
+ "args){System.out.println(}}\"Hello World!\");}}"); //$NON-NLS-1$
67
assertNotNull(source);
68   }
69
70   public void testSimplePackage() throws Exception JavaDoc {
71     JavaSource source = doParse("package de.java2html;"); //$NON-NLS-1$
72
assertEquals("de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
73
}
74
75   public void testPackageWithPrefix() throws Exception JavaDoc {
76     JavaSource source = doParse("/* foo.. */ package de.java2html;"); //$NON-NLS-1$
77
assertEquals("de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
78
}
79
80   public void testPackageWithEmptySpace() throws Exception JavaDoc {
81     JavaSource source = doParse("package de.java2html ;"); //$NON-NLS-1$
82
assertEquals("de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
83
}
84
85   // public void testHandlesSimpleLineBreakCorrect() throws Exception {
86
// String TEXT1 = "test\nline two";
87
// JavaSource source = doParse(TEXT1);
88
// assertEquals(TEXT1.length(), source.getCode().length());
89
// assertEquals(TEXT1.length(), source.getClassification().length);
90
// }
91

92   // public void testHandlesDoubleLineBreakCorrect() throws Exception {
93
// String TEXT1 = "test\r\nline two";
94
// JavaSource source = doParse(TEXT1);
95
// assertEquals(TEXT1.length(), source.getCode().length());
96
// assertEquals(TEXT1.length(), source.getClassification().length);
97
// }
98

99   public void testParseTypeCHAR_CONSTANT() throws IOException JavaDoc {
100     String JavaDoc text = "'c'"; //$NON-NLS-1$
101
String JavaDoc type = "'''"; //$NON-NLS-1$
102
assertParsedTypesEquals(text, type);
103   }
104
105   public void testParseTypeCODE() throws IOException JavaDoc {
106     String JavaDoc text = "doThis()"; //$NON-NLS-1$
107
String JavaDoc type = "CCCCCC{{"; //$NON-NLS-1$
108
assertParsedTypesEquals(text, type);
109   }
110
111   public void testParseTypeCODE_TYPE() throws IOException JavaDoc {
112     String JavaDoc text = "int doThis()"; //$NON-NLS-1$
113
String JavaDoc type = "TTT_CCCCCC{{"; //$NON-NLS-1$
114
assertParsedTypesEquals(text, type);
115   }
116
117   public void testParseTypeCOMMENT_BLOCK() throws IOException JavaDoc {
118     String JavaDoc text = "/* int doThis() */"; //$NON-NLS-1$
119
String JavaDoc type = "##_###_########_##"; //$NON-NLS-1$
120
assertParsedTypesEquals(text, type);
121   }
122
123   public void testParseTypeCOMMENT_LINE() throws IOException JavaDoc {
124     String JavaDoc text = "// /** int doThis()"; //$NON-NLS-1$
125
String JavaDoc type = "//_///_///_////////"; //$NON-NLS-1$
126
assertParsedTypesEquals(text, type);
127   }
128
129   public void testParseTypeJAVADOC() throws IOException JavaDoc {
130     String JavaDoc text = "/** int doThis() */"; //$NON-NLS-1$
131
String JavaDoc type = "***_***_********_**"; //$NON-NLS-1$
132
assertParsedTypesEquals(text, type);
133   }
134
135   public void testParseTypeJAVADOC_HTML_TAG() throws IOException JavaDoc {
136     String JavaDoc text = "/** <code>int</code> doThis() */"; //$NON-NLS-1$
137
String JavaDoc type = "***_<<<<<<***<<<<<<<_********_**"; //$NON-NLS-1$
138
assertParsedTypesEquals(text, type);
139   }
140
141   public void testParseTypeJAVADOC_KEYWORD() throws IOException JavaDoc {
142     String JavaDoc text = "/** @deprecated doThis() */"; //$NON-NLS-1$
143
String JavaDoc type = "***_@@@@@@@@@@@_********_**"; //$NON-NLS-1$
144
assertParsedTypesEquals(text, type);
145   }
146
147   public void testParseTypeJAVADOC_KEYWORDWithoutSpace() throws IOException JavaDoc {
148     String JavaDoc text = "/**@see*/"; //$NON-NLS-1$
149
String JavaDoc type = "***@@@@**"; //$NON-NLS-1$
150
assertParsedTypesEquals(text, type);
151   }
152
153   public void testParseTypeKEYWORD() throws IOException JavaDoc {
154     String JavaDoc text = "public void"; //$NON-NLS-1$
155
String JavaDoc type = "KKKKKK_TTTT"; //$NON-NLS-1$
156
assertParsedTypesEquals(text, type);
157   }
158
159   public void testParseTypeNUM_CONSTANT() throws IOException JavaDoc {
160     String JavaDoc text = "int i = 1;"; //$NON-NLS-1$
161
String JavaDoc type = "TTT_C_C_1C"; //$NON-NLS-1$
162
assertParsedTypesEquals(text, type);
163   }
164
165   public void testParseTypePARENTHESIS() throws IOException JavaDoc {
166     String JavaDoc text = "{ }"; //$NON-NLS-1$
167
String JavaDoc type = "{_{"; //$NON-NLS-1$
168
assertParsedTypesEquals(text, type);
169   }
170
171   public void testParseTypeSTRING() throws IOException JavaDoc {
172     String JavaDoc TEXT = "text = \"\\\"\";"; //$NON-NLS-1$
173
JavaSource source = doParse(TEXT);
174     assertEquals(JavaSourceType.STRING, source.getClassification()[8]);
175     assertEquals(JavaSourceType.STRING, source.getClassification()[9]);
176   }
177
178   public void testLineBreaks1() throws IOException JavaDoc {
179     String JavaDoc TEXT = "this\nand that"; //$NON-NLS-1$
180
JavaSource source = doParse(TEXT);
181     assertEquals(2, source.getLineCount());
182     // assertEquals(TEXT, source.getCode());
183
}
184
185   public void testLineBreaks2() throws IOException JavaDoc {
186     String JavaDoc TEXT = "this\r\nand that"; //$NON-NLS-1$
187
JavaSource source = doParse(TEXT);
188     assertEquals(2, source.getLineCount());
189   }
190
191   //TODO: BufferedReader cuts linebreaks at the end - not serious, but look at it somewhen.
192
// public void testLineBreaks3() throws IOException{
193
// JavaSource source = JavaSourceParserTest.doParse("a\n");
194
// assertEquals("a\r\n", source.getCode());
195
//
196
// source = JavaSourceParserTest.doParse("a\n\n");
197
// assertEquals("a\r\n\r\n", source.getCode());
198
// }
199

200   public void testParseTypeJAVADOC_LINKAsOrdinaryTag() throws IOException JavaDoc {
201     String JavaDoc text = "/** @link this... */"; //$NON-NLS-1$
202
String JavaDoc type = "***_@@@@@_*******_**"; //$NON-NLS-1$
203
assertParsedTypesEquals(text, type);
204   }
205
206   public void testParseTypeJAVADOC_LINKS() throws IOException JavaDoc {
207     String JavaDoc text = "/** {@link this...} */"; //$NON-NLS-1$
208
String JavaDoc type = "***_LLLLLL_LLLLLLLL_**"; //$NON-NLS-1$
209
assertParsedTypesEquals(text, type);
210   }
211
212   public void testParseTypeJAVADOC_LINKSDouble() throws IOException JavaDoc {
213     String JavaDoc text = "/** {@link abc}{@link def} */"; //$NON-NLS-1$
214
String JavaDoc type = "***_LLLLLL_LLLLLLLLLL_LLLL_**"; //$NON-NLS-1$
215
assertParsedTypesEquals(text, type);
216   }
217
218   public void testParseTypeJAVADOC_LINKSOutliers() throws IOException JavaDoc {
219     String JavaDoc text = "/**@link*/"; //$NON-NLS-1$
220
String JavaDoc type = "***@@@@@**"; //$NON-NLS-1$
221
assertParsedTypesEquals(text, type);
222
223     text = "/**{@link*/"; //$NON-NLS-1$
224
type = "****@@@@@**"; //$NON-NLS-1$
225
assertParsedTypesEquals(text, type);
226
227     text = "/**@linka*/"; //$NON-NLS-1$
228
type = "***********"; //Not a valid keyword //$NON-NLS-1$
229
assertParsedTypesEquals(text, type);
230
231     text = "/**@link a*/"; //$NON-NLS-1$
232
type = "***@@@@@_***"; //$NON-NLS-1$
233
assertParsedTypesEquals(text, type);
234
235     text = "/**{@link}*/"; //$NON-NLS-1$
236
type = "***LLLLLLL**"; //$NON-NLS-1$
237
assertParsedTypesEquals(text, type);
238
239     text = "/**{@link }*/"; //$NON-NLS-1$
240
type = "***LLLLLL_L**"; //$NON-NLS-1$
241
assertParsedTypesEquals(text, type);
242   }
243
244   //Unmatched JavaDoc-Link should be a JavaDoc tag
245
public void testParseTypeJAVADOC_LINKSOutlier1() throws IOException JavaDoc {
246     String JavaDoc text = "/** {@link text */ public String[] texts=new char[]{'t', 'u'};"; //$NON-NLS-1$
247
String JavaDoc type = "***_*@@@@@_****_**_KKKKKK_CCCCCC{{_CCCCCCKKK_TTTT{{{'''C_'''{C"; //$NON-NLS-1$
248
assertParsedTypesEquals(text, type);
249   }
250
251   public void testEnumKeyword() throws IOException JavaDoc {
252     String JavaDoc text = "public enum Coin {"; //$NON-NLS-1$
253
String JavaDoc type = "KKKKKK_KKKK_CCCC_{"; //$NON-NLS-1$
254
assertParsedTypesEquals(text, type);
255   }
256
257   public void testAnnotation() throws IOException JavaDoc {
258     String JavaDoc text = "public class Test { @java.lang.Deprecated public void bla() {}}"; //$NON-NLS-1$
259
String JavaDoc type = "KKKKKK_KKKKK_CCCC_{_AAAAAAAAAAAAAAAAAAAAA_KKKKKK_TTTT_CCC{{_{{{"; //$NON-NLS-1$
260
assertParsedTypesEquals(text, type);
261   }
262
263   public void testAnnotationInterfaceKeyword() throws IOException JavaDoc {
264     String JavaDoc text = "public @interface Coin {"; //$NON-NLS-1$
265
String JavaDoc type = "KKKKKK_KKKKKKKKKK_CCCC_{"; //$NON-NLS-1$
266
assertParsedTypesEquals(text, type);
267   }
268
269   //TODO Mar 12, 2004 (Markus Gebhard) Generics:
270
// public void testGenerics() throws IOException {
271
// String text = "Vector<String>";
272
// String type = "CCCCCCGGGGGGGG";
273
// assertParsedTypesEquals(text, type);
274
//
275
// text = "Seq<Seq<A>>";
276
// type = "CCCGGGGGGGG";
277
// assertParsedTypesEquals(text, type);
278
//
279
// text = "Seq<String>.Zipper<Integer>";
280
// type = "CCCGGGGGGGGCCCCCCCGGGGGGGGG";
281
// assertParsedTypesEquals(text, type);
282
//
283
// text = "Collection<Integer>";
284
// type = "CCCCCCCCCCGGGGGGGGG";
285
// assertParsedTypesEquals(text, type);
286
//
287
// text = "Pair<String,String>";
288
// type = "CCCCGGGGGGGGGGGGGGG";
289
// assertParsedTypesEquals(text, type);
290
// }
291
}
Popular Tags