ParserTest.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package de.jplag.java17;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import org.antlr.v4.runtime.CharStream;
  8. import org.antlr.v4.runtime.CharStreams;
  9. import org.antlr.v4.runtime.CommonTokenStream;
  10. import org.junit.Assert;
  11. import org.junit.BeforeClass;
  12. import org.junit.Ignore;
  13. import org.junit.Test;
  14. import de.jplag.StrippedProgram;
  15. import de.jplag.java17.grammar.Java7Lexer;
  16. import de.jplag.java17.grammar.Java7Parser;
  17. public class ParserTest {
  18. private static File srcTestResources;
  19. @BeforeClass
  20. public static void getPaths() {
  21. srcTestResources = new File(System.getProperty("user.dir"), "src/test/resources");
  22. }
  23. /**
  24. * Parses a (useless) class that uses all the fancy Java 7 syntax features
  25. * that I was able to find so far. The output is not compared to anything.
  26. * This test should only assure that we have covered all new features with
  27. * the grammar.
  28. *
  29. * @throws IOException
  30. */
  31. @Test
  32. public void testTheNewFancyJava7Features() throws IOException {
  33. File file = new File(srcTestResources, "Java7FeatureTest.java");
  34. FileInputStream fis = new FileInputStream(file);
  35. CharStream input = CharStreams.fromStream(fis);
  36. Java7Lexer lexer = new Java7Lexer(input);
  37. CommonTokenStream tokens = new CommonTokenStream(lexer);
  38. Java7Parser parser = new Java7Parser(tokens);
  39. parser.compilationUnit();
  40. }
  41. @Ignore
  42. public void j7Template() throws IOException {
  43. File file = new File(srcTestResources, "J7StringSwitch.java");
  44. String expected = ""// @formatter:off
  45. + "********\n"; // @formatter:on
  46. String tokens = parseWithJ7Parser(file, false);
  47. assertEquals(expected, tokens);
  48. }
  49. /**
  50. * added for ticket #58
  51. */
  52. @Test
  53. public void emptyFileOnlyComments() throws IOException {
  54. compareWithParser15(new File(srcTestResources, "EmptyFileOnlyComments.java"));
  55. }
  56. @Test
  57. public void emptyFile() throws IOException {
  58. compareWithParser15(new File(srcTestResources, "EmptyFile.java"));
  59. }
  60. @Test
  61. public void j7StringSwitchTest() throws IOException {
  62. File file = new File(srcTestResources, "J7StringSwitch.java");
  63. String expected ="" // @formatter:off
  64. +"CLASS{ \n"
  65. +"VOID \n"
  66. +"METHOD{ \n"
  67. +"SWITCH{ \n"
  68. +"CASE \n"
  69. +"BREAK \n"
  70. +"CASE \n"
  71. +"CASE \n"
  72. +"BREAK \n"
  73. +"CASE \n"
  74. +"BREAK \n"
  75. +"}SWITCH \n"
  76. +"}METHOD \n"
  77. +"}CLASS \n"
  78. + "********\n"; // @formatter:on
  79. String tokens = parseWithJ7Parser(file, false);
  80. assertEquals(expected, tokens);
  81. }
  82. @Test
  83. public void j7TryCatchTest() throws IOException {
  84. File file = new File(srcTestResources, "J7TryCatch.java");
  85. String expected ="IMPORT \n" // @formatter:off
  86. +"IMPORT \n"
  87. +"IMPORT \n"
  88. +"CLASS{ \n"
  89. +"VOID \n"
  90. +"METHOD{ \n"
  91. +"TRY{ \n"
  92. +"CATCH{ \n"
  93. +"}CATCH \n"
  94. +"FINALLY \n"
  95. +"}METHOD \n"
  96. +"VOID \n"
  97. +"METHOD{ \n"
  98. +"TRY{ \n"
  99. +"TRY_RES \n"
  100. +"NEWCLASS\n"
  101. +"APPLY \n"
  102. +"CATCH{ \n"
  103. +"}CATCH \n"
  104. +"}METHOD \n"
  105. +"VOID \n"
  106. +"METHOD{ \n"
  107. +"TRY{ \n"
  108. +"TRY_RES \n"
  109. +"NEWCLASS\n"
  110. +"TRY_RES \n"
  111. +"NEWCLASS\n"
  112. +"APPLY \n"
  113. +"CATCH{ \n"
  114. +"}CATCH \n"
  115. +"}METHOD \n"
  116. +"VOID \n"
  117. +"METHOD{ \n"
  118. +"TRY{ \n"
  119. +"APPLY \n"
  120. +"CATCH{ \n"
  121. +"}CATCH \n"
  122. +"}METHOD \n"
  123. +"VOID \n"
  124. +"METHOD{ \n"
  125. +"TRY{ \n"
  126. +"APPLY \n"
  127. +"CATCH{ \n"
  128. +"}CATCH \n"
  129. +"CATCH{ \n"
  130. +"}CATCH \n"
  131. +"}METHOD \n"
  132. +"}CLASS \n"
  133. +"********\n";// @formatter:on
  134. String tokens = parseWithJ7Parser(file, false);
  135. assertEquals(expected, tokens);
  136. }
  137. @Test
  138. public void compArray() throws IOException {
  139. compareWithParser15(new File(srcTestResources, "ArrayTest.java"));
  140. }
  141. @Test
  142. public void compSimpleClass() throws IOException {
  143. compareWithParser15(new File(srcTestResources, "SimpleClass.java"));
  144. }
  145. @Test
  146. public void compEnum() {
  147. compareWithParser15(new File(srcTestResources, "EnumTest.java"));
  148. }
  149. @Test
  150. public void compGeneric() {
  151. compareWithParser15(new File(srcTestResources, "GenericPocket.java"));
  152. }
  153. @Test
  154. public void compInnerClass() {
  155. compareWithParser15(new File(srcTestResources, "InnerClass.java"));
  156. }
  157. @Test
  158. public void compAnonymousClass() {
  159. compareWithParser15(new File(srcTestResources, "AnonymousClass.java"));
  160. }
  161. @Test
  162. public void compAnnotations_Usage() {
  163. compareWithParser15(new File(srcTestResources, "AnnotationText.java"));
  164. }
  165. @Test
  166. public void compAnnotations_PackageUsage() {
  167. compareWithParser15(new File(srcTestResources, "package-info.java"));
  168. }
  169. @Test
  170. public void compAnnotations_Definition() {
  171. compareWithParser15(new File(srcTestResources, "OptimizeAnnotation.java"));
  172. }
  173. @Test
  174. public void j7Generics() throws IOException {
  175. File file = new File(srcTestResources, "J7Generics.java");
  176. String expected = ""// @formatter:off
  177. + "CLASS{ \n"
  178. + "VOID \n"
  179. + "METHOD{ \n"
  180. + "GENERIC \n"
  181. + "VARDEF \n"
  182. + "ASSIGN \n"
  183. + "GENERIC \n"
  184. + "NEWCLASS\n"
  185. + "}METHOD \n"
  186. + "}CLASS \n"
  187. + "********\n"; // @formatter:on
  188. String tokens = parseWithJ7Parser(file, false);
  189. assertEquals(expected, tokens);
  190. }
  191. @Test
  192. public void assureBackwardsCompatibility2() throws IOException {
  193. compareWithParser15(new File(srcTestResources, "ExceptionTwo.java"));
  194. }
  195. @Test
  196. public void assureBackwardsCompatibility3() throws IOException {
  197. compareWithParser15(new File(srcTestResources, "ExceptionThree.java"));
  198. }
  199. @Test
  200. public void assureBackwardsCompatibility4() throws IOException {
  201. compareWithParser15(new File(srcTestResources, "Calendar.java"));
  202. }
  203. /**
  204. * Compare results (token sequence) with Java 1.5 parser (without method
  205. * separators ; if you know why they exist, PLEASE tell us or document at
  206. * https://svn.ipd.kit.edu/trac/de/jplag/wiki/Server/Frontends/Java-1.5)
  207. *
  208. * @param javaFile
  209. * @throws IOException
  210. */
  211. private void compareWithParser15(File javaFile) {
  212. if (!javaFile.exists()) {
  213. fail("Test not implemented - cannot find file >" + javaFile + "<");
  214. }
  215. String newTokens = parseWithJ7Parser(javaFile, true);
  216. String oldTokens = parseWithJ5Parser(javaFile);
  217. // compare token sequence?
  218. Assert.assertEquals(oldTokens, newTokens);
  219. }
  220. private String parseWithJ5Parser(File javaFile) {
  221. // parse with old parser
  222. de.jplag.java15.Parser oldParser = new de.jplag.java15.Parser(false);
  223. oldParser.setProgram(new StrippedProgram());
  224. de.jplag.TokenList oldStruct = oldParser.parse(javaFile.getParentFile(), new String[] { javaFile.getName() });
  225. String oldTokens = buildTokenString(oldStruct, true);
  226. return oldTokens;
  227. }
  228. /**
  229. * assumes that the token types have not changed from java 1.5 to Java 1.7
  230. * (meaning the integer values are the same but Java 1.7 has more than Java
  231. * 1.5)
  232. *
  233. * @see de.jplag.java17.JavaToken
  234. *
  235. * @param oldStruct
  236. * @param withDetails
  237. * @return
  238. */
  239. private String buildTokenString(de.jplag.TokenList oldStruct, boolean withDetails) {
  240. StringBuilder sb = new StringBuilder();
  241. for (int i = 0; i < oldStruct.size(); i++) {
  242. sb.append(de.jplag.java17.JavaToken.type2string(oldStruct.getToken(i).type));
  243. if (withDetails) {
  244. sb.append(" L:" + oldStruct.getToken(i).getLine()
  245. + " C:" + oldStruct.getToken(i).getLine()
  246. + " l:" + oldStruct.getToken(i).getLine());
  247. }
  248. sb.append("\n");
  249. }
  250. String oldTokens = sb.toString();
  251. return oldTokens;
  252. }
  253. private String parseWithJ7Parser(File javaFile, boolean withDetails) {
  254. // parse with new parser
  255. de.jplag.java17.Parser newParser = new Parser();
  256. newParser.setProgram(new StrippedProgram());
  257. de.jplag.TokenList newStruct = newParser.parse(javaFile.getParentFile(), new String[] { javaFile.getName() });
  258. String newTokens = buildTokenString(newStruct, withDetails);
  259. return newTokens;
  260. }
  261. }