1 package de.java2html.javasource.test; 2 3 import java.io.IOException ; 4 import java.io.Reader ; 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 14 public class JavaSourceParserTest extends JavaSourceParserTestCase { 15 public void testParseNull() throws IOException { 16 JavaSourceParser parser = new JavaSourceParser(); 17 try { 18 parser.parse((Reader ) null); 19 fail(); 20 } 21 catch (IllegalArgumentException expected) { 22 } 24 } 25 26 public void testParseEmpty() throws IOException { 27 JavaSource source = doParse(""); assertNotNull(source); 29 assertEquals("", source.getCode()); 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 { 38 JavaSource source = doParse("public String text =\"test\";"); assertNotNull(source); 40 assertEquals("public String text =\"test\";", source.getCode()); 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 { 49 int tabSize = JavaSourceConversionOptions.getDefault().getTabSize(); 50 JavaSource source = doParse("\t"); assertNotNull(source); 52 assertEquals(tabSize, source.getCode().length()); 53 } 54 55 public void testParseTabs() throws IOException { 56 int tabSize = JavaSourceConversionOptions.getDefault().getTabSize(); 57 58 JavaSource source = doParse("\t\tpublic String text =\"test\";"); assertNotNull(source); 60 assertEquals(tabSize * 2 + "public String text =\"test\";".length(), source.getCode().length()); assertEquals("public String text =\"test\";", source.getCode().trim()); } 63 64 public void testParse() throws IOException { 65 JavaSource source = doParse("package test.it; public class Main{ public static void main(String [] " + "args){System.out.println(}}\"Hello World!\");}}"); assertNotNull(source); 68 } 69 70 public void testSimplePackage() throws Exception { 71 JavaSource source = doParse("package de.java2html;"); assertEquals("de.java2html", source.getStatistic().getPackageName()); } 74 75 public void testPackageWithPrefix() throws Exception { 76 JavaSource source = doParse("/* foo.. */ package de.java2html;"); assertEquals("de.java2html", source.getStatistic().getPackageName()); } 79 80 public void testPackageWithEmptySpace() throws Exception { 81 JavaSource source = doParse("package de.java2html ;"); assertEquals("de.java2html", source.getStatistic().getPackageName()); } 84 85 92 99 public void testParseTypeCHAR_CONSTANT() throws IOException { 100 String text = "'c'"; String type = "'''"; assertParsedTypesEquals(text, type); 103 } 104 105 public void testParseTypeCODE() throws IOException { 106 String text = "doThis()"; String type = "CCCCCC{{"; assertParsedTypesEquals(text, type); 109 } 110 111 public void testParseTypeCODE_TYPE() throws IOException { 112 String text = "int doThis()"; String type = "TTT_CCCCCC{{"; assertParsedTypesEquals(text, type); 115 } 116 117 public void testParseTypeCOMMENT_BLOCK() throws IOException { 118 String text = "/* int doThis() */"; String type = "##_###_########_##"; assertParsedTypesEquals(text, type); 121 } 122 123 public void testParseTypeCOMMENT_LINE() throws IOException { 124 String text = "// /** int doThis()"; String type = "//_///_///_////////"; assertParsedTypesEquals(text, type); 127 } 128 129 public void testParseTypeJAVADOC() throws IOException { 130 String text = "/** int doThis() */"; String type = "***_***_********_**"; assertParsedTypesEquals(text, type); 133 } 134 135 public void testParseTypeJAVADOC_HTML_TAG() throws IOException { 136 String text = "/** <code>int</code> doThis() */"; String type = "***_<<<<<<***<<<<<<<_********_**"; assertParsedTypesEquals(text, type); 139 } 140 141 public void testParseTypeJAVADOC_KEYWORD() throws IOException { 142 String text = "/** @deprecated doThis() */"; String type = "***_@@@@@@@@@@@_********_**"; assertParsedTypesEquals(text, type); 145 } 146 147 public void testParseTypeJAVADOC_KEYWORDWithoutSpace() throws IOException { 148 String text = "/**@see*/"; String type = "***@@@@**"; assertParsedTypesEquals(text, type); 151 } 152 153 public void testParseTypeKEYWORD() throws IOException { 154 String text = "public void"; String type = "KKKKKK_TTTT"; assertParsedTypesEquals(text, type); 157 } 158 159 public void testParseTypeNUM_CONSTANT() throws IOException { 160 String text = "int i = 1;"; String type = "TTT_C_C_1C"; assertParsedTypesEquals(text, type); 163 } 164 165 public void testParseTypePARENTHESIS() throws IOException { 166 String text = "{ }"; String type = "{_{"; assertParsedTypesEquals(text, type); 169 } 170 171 public void testParseTypeSTRING() throws IOException { 172 String TEXT = "text = \"\\\"\";"; 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 { 179 String TEXT = "this\nand that"; JavaSource source = doParse(TEXT); 181 assertEquals(2, source.getLineCount()); 182 } 184 185 public void testLineBreaks2() throws IOException { 186 String TEXT = "this\r\nand that"; JavaSource source = doParse(TEXT); 188 assertEquals(2, source.getLineCount()); 189 } 190 191 200 public void testParseTypeJAVADOC_LINKAsOrdinaryTag() throws IOException { 201 String text = "/** @link this... */"; String type = "***_@@@@@_*******_**"; assertParsedTypesEquals(text, type); 204 } 205 206 public void testParseTypeJAVADOC_LINKS() throws IOException { 207 String text = "/** {@link this...} */"; String type = "***_LLLLLL_LLLLLLLL_**"; assertParsedTypesEquals(text, type); 210 } 211 212 public void testParseTypeJAVADOC_LINKSDouble() throws IOException { 213 String text = "/** {@link abc}{@link def} */"; String type = "***_LLLLLL_LLLLLLLLLL_LLLL_**"; assertParsedTypesEquals(text, type); 216 } 217 218 public void testParseTypeJAVADOC_LINKSOutliers() throws IOException { 219 String text = "/**@link*/"; String type = "***@@@@@**"; assertParsedTypesEquals(text, type); 222 223 text = "/**{@link*/"; type = "****@@@@@**"; assertParsedTypesEquals(text, type); 226 227 text = "/**@linka*/"; type = "***********"; assertParsedTypesEquals(text, type); 230 231 text = "/**@link a*/"; type = "***@@@@@_***"; assertParsedTypesEquals(text, type); 234 235 text = "/**{@link}*/"; type = "***LLLLLLL**"; assertParsedTypesEquals(text, type); 238 239 text = "/**{@link }*/"; type = "***LLLLLL_L**"; assertParsedTypesEquals(text, type); 242 } 243 244 public void testParseTypeJAVADOC_LINKSOutlier1() throws IOException { 246 String text = "/** {@link text */ public String[] texts=new char[]{'t', 'u'};"; String type = "***_*@@@@@_****_**_KKKKKK_CCCCCC{{_CCCCCCKKK_TTTT{{{'''C_'''{C"; assertParsedTypesEquals(text, type); 249 } 250 251 public void testEnumKeyword() throws IOException { 252 String text = "public enum Coin {"; String type = "KKKKKK_KKKK_CCCC_{"; assertParsedTypesEquals(text, type); 255 } 256 257 public void testAnnotation() throws IOException { 258 String text = "public class Test { @java.lang.Deprecated public void bla() {}}"; String type = "KKKKKK_KKKKK_CCCC_{_AAAAAAAAAAAAAAAAAAAAA_KKKKKK_TTTT_CCC{{_{{{"; assertParsedTypesEquals(text, type); 261 } 262 263 public void testAnnotationInterfaceKeyword() throws IOException { 264 String text = "public @interface Coin {"; String type = "KKKKKK_KKKKKKKKKK_CCCC_{"; assertParsedTypesEquals(text, type); 267 } 268 269 } | Popular Tags |