|
| 1 | +package com.ibm.cldk; |
| 2 | + |
| 3 | +import com.ibm.cldk.entities.Import; |
| 4 | +import com.ibm.cldk.entities.JavaCompilationUnit; |
| 5 | +import java.io.File; |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | + |
| 17 | +public class CodeAnalyzerTest { |
| 18 | + |
| 19 | + @TempDir |
| 20 | + Path tempDir; |
| 21 | + |
| 22 | + @SuppressWarnings("unchecked") |
| 23 | + private Map<String, JavaCompilationUnit> invokeReadSymbolTableFromFile(Path analysisFilePath) throws Exception { |
| 24 | + Method readSymbolTableMethod = CodeAnalyzer.class.getDeclaredMethod("readSymbolTableFromFile", File.class); |
| 25 | + readSymbolTableMethod.setAccessible(true); |
| 26 | + try { |
| 27 | + return (Map<String, JavaCompilationUnit>) readSymbolTableMethod.invoke(null, analysisFilePath.toFile()); |
| 28 | + } catch (InvocationTargetException invocationTargetException) { |
| 29 | + Throwable targetException = invocationTargetException.getTargetException(); |
| 30 | + if (targetException instanceof Exception) { |
| 31 | + throw (Exception) targetException; |
| 32 | + } |
| 33 | + throw new RuntimeException(targetException); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private Path writeAnalysisFile(String jsonContent) throws Exception { |
| 38 | + Path analysisFilePath = tempDir.resolve("analysis.json"); |
| 39 | + Files.writeString(analysisFilePath, jsonContent, StandardCharsets.UTF_8); |
| 40 | + return analysisFilePath; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testReadSymbolTableFromFileRejectsLegacyImportSchema() throws Exception { |
| 45 | + String jsonContent = "{\n" |
| 46 | + + " \"symbol_table\": {\n" |
| 47 | + + " \"/tmp/T.java\": {\n" |
| 48 | + + " \"file_path\": \"/tmp/T.java\",\n" |
| 49 | + + " \"package_name\": \"\",\n" |
| 50 | + + " \"comments\": [],\n" |
| 51 | + + " \"imports\": [\"java.util.List\"],\n" |
| 52 | + + " \"type_declarations\": {},\n" |
| 53 | + + " \"is_modified\": false\n" |
| 54 | + + " }\n" |
| 55 | + + " }\n" |
| 56 | + + "}\n"; |
| 57 | + Path analysisFilePath = writeAnalysisFile(jsonContent); |
| 58 | + |
| 59 | + IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, |
| 60 | + () -> invokeReadSymbolTableFromFile(analysisFilePath)); |
| 61 | + Assertions.assertTrue(exception.getMessage().contains("legacy import schema")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testReadSymbolTableFromFileParsesExplicitImportSchema() throws Exception { |
| 66 | + String jsonContent = "{\n" |
| 67 | + + " \"symbol_table\": {\n" |
| 68 | + + " \"/tmp/T.java\": {\n" |
| 69 | + + " \"file_path\": \"/tmp/T.java\",\n" |
| 70 | + + " \"package_name\": \"\",\n" |
| 71 | + + " \"comments\": [],\n" |
| 72 | + + " \"imports\": [\n" |
| 73 | + + " {\n" |
| 74 | + + " \"path\": \"java.util.List\",\n" |
| 75 | + + " \"is_static\": false,\n" |
| 76 | + + " \"is_wildcard\": false\n" |
| 77 | + + " },\n" |
| 78 | + + " {\n" |
| 79 | + + " \"path\": \"java.util.Collections\",\n" |
| 80 | + + " \"is_static\": true,\n" |
| 81 | + + " \"is_wildcard\": true\n" |
| 82 | + + " }\n" |
| 83 | + + " ],\n" |
| 84 | + + " \"type_declarations\": {},\n" |
| 85 | + + " \"is_modified\": false\n" |
| 86 | + + " }\n" |
| 87 | + + " }\n" |
| 88 | + + "}\n"; |
| 89 | + Path analysisFilePath = writeAnalysisFile(jsonContent); |
| 90 | + |
| 91 | + Map<String, JavaCompilationUnit> symbolTable = invokeReadSymbolTableFromFile(analysisFilePath); |
| 92 | + Assertions.assertNotNull(symbolTable); |
| 93 | + Assertions.assertEquals(1, symbolTable.size()); |
| 94 | + |
| 95 | + JavaCompilationUnit compilationUnit = symbolTable.get("/tmp/T.java"); |
| 96 | + Assertions.assertNotNull(compilationUnit); |
| 97 | + List<Import> imports = compilationUnit.getImports(); |
| 98 | + Assertions.assertNotNull(imports); |
| 99 | + Assertions.assertEquals(2, imports.size()); |
| 100 | + |
| 101 | + Import defaultImport = imports.stream() |
| 102 | + .filter(imp -> "java.util.List".equals(imp.getPath())) |
| 103 | + .findFirst() |
| 104 | + .orElse(null); |
| 105 | + Assertions.assertNotNull(defaultImport); |
| 106 | + Assertions.assertFalse(defaultImport.isStatic()); |
| 107 | + Assertions.assertFalse(defaultImport.isWildcard()); |
| 108 | + |
| 109 | + Import staticWildcardImport = imports.stream() |
| 110 | + .filter(imp -> "java.util.Collections".equals(imp.getPath())) |
| 111 | + .findFirst() |
| 112 | + .orElse(null); |
| 113 | + Assertions.assertNotNull(staticWildcardImport); |
| 114 | + Assertions.assertTrue(staticWildcardImport.isStatic()); |
| 115 | + Assertions.assertTrue(staticWildcardImport.isWildcard()); |
| 116 | + } |
| 117 | +} |
0 commit comments