001: package de.java2html.javasource.test;
002:
003: import java.io.IOException;
004: import java.io.Reader;
005:
006: import de.java2html.javasource.JavaSource;
007: import de.java2html.javasource.JavaSourceParser;
008: import de.java2html.javasource.JavaSourceType;
009: import de.java2html.options.JavaSourceConversionOptions;
010:
011: /**
012: * @author Markus Gebhard
013: */
014: public class JavaSourceParserTest extends JavaSourceParserTestCase {
015: public void testParseNull() throws IOException {
016: JavaSourceParser parser = new JavaSourceParser();
017: try {
018: parser.parse((Reader) null);
019: fail();
020: } catch (IllegalArgumentException expected) {
021: //expected
022: }
023: }
024:
025: public void testParseEmpty() throws IOException {
026: JavaSource source = doParse(""); //$NON-NLS-1$
027: assertNotNull(source);
028: assertEquals("", source.getCode()); //$NON-NLS-1$
029: assertEquals(0, source.getLineCount());
030: assertEquals(0, source.getMaxLineLength());
031: assertNotNull(source.getClassification());
032: assertEquals(0, source.getClassification().length);
033: assertNotNull(source.getStatistic());
034: }
035:
036: public void testParseSimple() throws IOException {
037: JavaSource source = doParse("public String text =\"test\";"); //$NON-NLS-1$
038: assertNotNull(source);
039: assertEquals("public String text =\"test\";", source.getCode()); //$NON-NLS-1$
040: assertEquals(1, source.getLineCount());
041: assertEquals(27, source.getMaxLineLength());
042: assertNotNull(source.getClassification());
043: assertEquals(27, source.getClassification().length);
044: assertNotNull(source.getStatistic());
045: }
046:
047: public void testParseSingleTab() throws IOException {
048: int tabSize = JavaSourceConversionOptions.getDefault()
049: .getTabSize();
050: JavaSource source = doParse("\t"); //$NON-NLS-1$
051: assertNotNull(source);
052: assertEquals(tabSize, source.getCode().length());
053: }
054:
055: public void testParseTabs() throws IOException {
056: int tabSize = JavaSourceConversionOptions.getDefault()
057: .getTabSize();
058:
059: JavaSource source = doParse("\t\tpublic String text =\"test\";"); //$NON-NLS-1$
060: assertNotNull(source);
061: assertEquals(
062: tabSize * 2 + "public String text =\"test\";".length(), source.getCode().length()); //$NON-NLS-1$
063: assertEquals(
064: "public String text =\"test\";", source.getCode().trim()); //$NON-NLS-1$
065: }
066:
067: public void testParse() throws IOException {
068: JavaSource source = doParse("package test.it; public class Main{ public static void main(String [] " //$NON-NLS-1$
069: + "args){System.out.println(}}\"Hello World!\");}}"); //$NON-NLS-1$
070: assertNotNull(source);
071: }
072:
073: public void testSimplePackage() throws Exception {
074: JavaSource source = doParse("package de.java2html;"); //$NON-NLS-1$
075: assertEquals(
076: "de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
077: }
078:
079: public void testPackageWithPrefix() throws Exception {
080: JavaSource source = doParse("/* foo.. */ package de.java2html;"); //$NON-NLS-1$
081: assertEquals(
082: "de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
083: }
084:
085: public void testPackageWithEmptySpace() throws Exception {
086: JavaSource source = doParse("package de.java2html ;"); //$NON-NLS-1$
087: assertEquals(
088: "de.java2html", source.getStatistic().getPackageName()); //$NON-NLS-1$
089: }
090:
091: // public void testHandlesSimpleLineBreakCorrect() throws Exception {
092: // String TEXT1 = "test\nline two";
093: // JavaSource source = doParse(TEXT1);
094: // assertEquals(TEXT1.length(), source.getCode().length());
095: // assertEquals(TEXT1.length(), source.getClassification().length);
096: // }
097:
098: // public void testHandlesDoubleLineBreakCorrect() throws Exception {
099: // String TEXT1 = "test\r\nline two";
100: // JavaSource source = doParse(TEXT1);
101: // assertEquals(TEXT1.length(), source.getCode().length());
102: // assertEquals(TEXT1.length(), source.getClassification().length);
103: // }
104:
105: public void testParseTypeCHAR_CONSTANT() throws IOException {
106: String text = "'c'"; //$NON-NLS-1$
107: String type = "'''"; //$NON-NLS-1$
108: assertParsedTypesEquals(text, type);
109: }
110:
111: public void testParseTypeCODE() throws IOException {
112: String text = "doThis()"; //$NON-NLS-1$
113: String type = "CCCCCC{{"; //$NON-NLS-1$
114: assertParsedTypesEquals(text, type);
115: }
116:
117: public void testParseTypeCODE_TYPE() throws IOException {
118: String text = "int doThis()"; //$NON-NLS-1$
119: String type = "TTT_CCCCCC{{"; //$NON-NLS-1$
120: assertParsedTypesEquals(text, type);
121: }
122:
123: public void testParseTypeCOMMENT_BLOCK() throws IOException {
124: String text = "/* int doThis() */"; //$NON-NLS-1$
125: String type = "##_###_########_##"; //$NON-NLS-1$
126: assertParsedTypesEquals(text, type);
127: }
128:
129: public void testParseTypeCOMMENT_LINE() throws IOException {
130: String text = "// /** int doThis()"; //$NON-NLS-1$
131: String type = "//_///_///_////////"; //$NON-NLS-1$
132: assertParsedTypesEquals(text, type);
133: }
134:
135: public void testParseTypeJAVADOC() throws IOException {
136: String text = "/** int doThis() */"; //$NON-NLS-1$
137: String type = "***_***_********_**"; //$NON-NLS-1$
138: assertParsedTypesEquals(text, type);
139: }
140:
141: public void testParseTypeJAVADOC_HTML_TAG() throws IOException {
142: String text = "/** <code>int</code> doThis() */"; //$NON-NLS-1$
143: String type = "***_<<<<<<***<<<<<<<_********_**"; //$NON-NLS-1$
144: assertParsedTypesEquals(text, type);
145: }
146:
147: public void testParseTypeJAVADOC_KEYWORD() throws IOException {
148: String text = "/** @deprecated doThis() */"; //$NON-NLS-1$
149: String type = "***_@@@@@@@@@@@_********_**"; //$NON-NLS-1$
150: assertParsedTypesEquals(text, type);
151: }
152:
153: public void testParseTypeJAVADOC_KEYWORDWithoutSpace()
154: throws IOException {
155: String text = "/**@see*/"; //$NON-NLS-1$
156: String type = "***@@@@**"; //$NON-NLS-1$
157: assertParsedTypesEquals(text, type);
158: }
159:
160: public void testParseTypeKEYWORD() throws IOException {
161: String text = "public void"; //$NON-NLS-1$
162: String type = "KKKKKK_TTTT"; //$NON-NLS-1$
163: assertParsedTypesEquals(text, type);
164: }
165:
166: public void testParseTypeNUM_CONSTANT() throws IOException {
167: String text = "int i = 1;"; //$NON-NLS-1$
168: String type = "TTT_C_C_1C"; //$NON-NLS-1$
169: assertParsedTypesEquals(text, type);
170: }
171:
172: public void testParseTypePARENTHESIS() throws IOException {
173: String text = "{ }"; //$NON-NLS-1$
174: String type = "{_{"; //$NON-NLS-1$
175: assertParsedTypesEquals(text, type);
176: }
177:
178: public void testParseTypeSTRING() throws IOException {
179: String TEXT = "text = \"\\\"\";"; //$NON-NLS-1$
180: JavaSource source = doParse(TEXT);
181: assertEquals(JavaSourceType.STRING,
182: source.getClassification()[8]);
183: assertEquals(JavaSourceType.STRING,
184: source.getClassification()[9]);
185: }
186:
187: public void testLineBreaks1() throws IOException {
188: String TEXT = "this\nand that"; //$NON-NLS-1$
189: JavaSource source = doParse(TEXT);
190: assertEquals(2, source.getLineCount());
191: // assertEquals(TEXT, source.getCode());
192: }
193:
194: public void testLineBreaks2() throws IOException {
195: String TEXT = "this\r\nand that"; //$NON-NLS-1$
196: JavaSource source = doParse(TEXT);
197: assertEquals(2, source.getLineCount());
198: }
199:
200: //TODO: BufferedReader cuts linebreaks at the end - not serious, but look at it somewhen.
201: // public void testLineBreaks3() throws IOException{
202: // JavaSource source = JavaSourceParserTest.doParse("a\n");
203: // assertEquals("a\r\n", source.getCode());
204: //
205: // source = JavaSourceParserTest.doParse("a\n\n");
206: // assertEquals("a\r\n\r\n", source.getCode());
207: // }
208:
209: public void testParseTypeJAVADOC_LINKAsOrdinaryTag()
210: throws IOException {
211: String text = "/** @link this... */"; //$NON-NLS-1$
212: String type = "***_@@@@@_*******_**"; //$NON-NLS-1$
213: assertParsedTypesEquals(text, type);
214: }
215:
216: public void testParseTypeJAVADOC_LINKS() throws IOException {
217: String text = "/** {@link this...} */"; //$NON-NLS-1$
218: String type = "***_LLLLLL_LLLLLLLL_**"; //$NON-NLS-1$
219: assertParsedTypesEquals(text, type);
220: }
221:
222: public void testParseTypeJAVADOC_LINKSDouble() throws IOException {
223: String text = "/** {@link abc}{@link def} */"; //$NON-NLS-1$
224: String type = "***_LLLLLL_LLLLLLLLLL_LLLL_**"; //$NON-NLS-1$
225: assertParsedTypesEquals(text, type);
226: }
227:
228: public void testParseTypeJAVADOC_LINKSOutliers() throws IOException {
229: String text = "/**@link*/"; //$NON-NLS-1$
230: String type = "***@@@@@**"; //$NON-NLS-1$
231: assertParsedTypesEquals(text, type);
232:
233: text = "/**{@link*/"; //$NON-NLS-1$
234: type = "****@@@@@**"; //$NON-NLS-1$
235: assertParsedTypesEquals(text, type);
236:
237: text = "/**@linka*/"; //$NON-NLS-1$
238: type = "***********"; //Not a valid keyword //$NON-NLS-1$
239: assertParsedTypesEquals(text, type);
240:
241: text = "/**@link a*/"; //$NON-NLS-1$
242: type = "***@@@@@_***"; //$NON-NLS-1$
243: assertParsedTypesEquals(text, type);
244:
245: text = "/**{@link}*/"; //$NON-NLS-1$
246: type = "***LLLLLLL**"; //$NON-NLS-1$
247: assertParsedTypesEquals(text, type);
248:
249: text = "/**{@link }*/"; //$NON-NLS-1$
250: type = "***LLLLLL_L**"; //$NON-NLS-1$
251: assertParsedTypesEquals(text, type);
252: }
253:
254: //Unmatched JavaDoc-Link should be a JavaDoc tag
255: public void testParseTypeJAVADOC_LINKSOutlier1() throws IOException {
256: String text = "/** {@link text */ public String[] texts=new char[]{'t', 'u'};"; //$NON-NLS-1$
257: String type = "***_*@@@@@_****_**_KKKKKK_CCCCCC{{_CCCCCCKKK_TTTT{{{'''C_'''{C"; //$NON-NLS-1$
258: assertParsedTypesEquals(text, type);
259: }
260:
261: public void testEnumKeyword() throws IOException {
262: String text = "public enum Coin {"; //$NON-NLS-1$
263: String type = "KKKKKK_KKKK_CCCC_{"; //$NON-NLS-1$
264: assertParsedTypesEquals(text, type);
265: }
266:
267: public void testAnnotation() throws IOException {
268: String text = "public class Test { @java.lang.Deprecated public void bla() {}}"; //$NON-NLS-1$
269: String type = "KKKKKK_KKKKK_CCCC_{_AAAAAAAAAAAAAAAAAAAAA_KKKKKK_TTTT_CCC{{_{{{"; //$NON-NLS-1$
270: assertParsedTypesEquals(text, type);
271: }
272:
273: public void testAnnotationInterfaceKeyword() throws IOException {
274: String text = "public @interface Coin {"; //$NON-NLS-1$
275: String type = "KKKKKK_KKKKKKKKKK_CCCC_{"; //$NON-NLS-1$
276: assertParsedTypesEquals(text, type);
277: }
278:
279: //TODO Mar 12, 2004 (Markus Gebhard) Generics:
280: // public void testGenerics() throws IOException {
281: // String text = "Vector<String>";
282: // String type = "CCCCCCGGGGGGGG";
283: // assertParsedTypesEquals(text, type);
284: //
285: // text = "Seq<Seq<A>>";
286: // type = "CCCGGGGGGGG";
287: // assertParsedTypesEquals(text, type);
288: //
289: // text = "Seq<String>.Zipper<Integer>";
290: // type = "CCCGGGGGGGGCCCCCCCGGGGGGGGG";
291: // assertParsedTypesEquals(text, type);
292: //
293: // text = "Collection<Integer>";
294: // type = "CCCCCCCCCCGGGGGGGGG";
295: // assertParsedTypesEquals(text, type);
296: //
297: // text = "Pair<String,String>";
298: // type = "CCCCGGGGGGGGGGGGGGG";
299: // assertParsedTypesEquals(text, type);
300: // }
301: }
|