|
| 1 | +package test.neo.reducecognitivecomplexity.algorithms; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.ArrayList; |
| 9 | + |
| 10 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Order; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import neo.reducecognitivecomplexity.algorithms.Sequence; |
| 17 | +import neo.reducecognitivecomplexity.algorithms.Pair; |
| 18 | +import neo.reducecognitivecomplexity.jdt.Utils; |
| 19 | + |
| 20 | +class SequenceTest { |
| 21 | + static ArrayList<Sequence> sequence = new ArrayList<>(); |
| 22 | + static ArrayList<String> expectedCode = new ArrayList<>(); |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + static void setUp() throws Exception { |
| 26 | + Path resourceDirectory = Paths.get("src","test","resources"); |
| 27 | + String absolutePath = resourceDirectory.toFile().getAbsolutePath() + File.separatorChar; |
| 28 | + |
| 29 | + ArrayList<String> javaFileName = new ArrayList<>(); |
| 30 | + ArrayList<CompilationUnit> cu = new ArrayList<>(); |
| 31 | + ArrayList<Pair> pair = new ArrayList<>(); |
| 32 | + |
| 33 | + //First test case (comments remove from source code, because they are not in the AST) |
| 34 | + javaFileName.add("EZInjection.java"); |
| 35 | + pair.add(new Pair(4948,5392)); |
| 36 | + expectedCode.add( |
| 37 | + "boolean print = all;\n" |
| 38 | + + "if (!all && debugClasses.length >= 1)\n" |
| 39 | + + "{\n" |
| 40 | + + " for (String s : debugClasses)\n" |
| 41 | + + " {\n" |
| 42 | + + " if (info.split(\"\\\\.\")[0].equals(s.replaceAll(\"\\\\.\", \"/\")))\n" |
| 43 | + + " {\n" |
| 44 | + + " print = true;\n" |
| 45 | + + " break;\n" |
| 46 | + + " }\n" |
| 47 | + + " }\n" |
| 48 | + + "}\n" |
| 49 | + + "if (print)\n" |
| 50 | + + " print(\"Method call: \" + info);"); |
| 51 | + |
| 52 | + //Second test case (comments remove from source code, because they are not in the AST) |
| 53 | + javaFileName.add("EZInjection.java"); |
| 54 | + pair.add(new Pair(11006,11302)); |
| 55 | + expectedCode.add( |
| 56 | + "if (useProxy)\n" |
| 57 | + + "{\n" |
| 58 | + + " try\n" |
| 59 | + + " {\n" |
| 60 | + + " String[] split = proxy.split(\":\");\n" |
| 61 | + + " setProxy(split[0], split[1]);\n" |
| 62 | + + " } catch (Exception e) {\n" |
| 63 | + + " \n" |
| 64 | + + " }\n" |
| 65 | + + "}\n" |
| 66 | + + "print(\"Done setting up.\");\n" |
| 67 | + + "setFinished();"); |
| 68 | + |
| 69 | + //Third test case (comments remove from source code, because they are not in the AST) |
| 70 | + javaFileName.add("FileDrop.java"); |
| 71 | + pair.add(new Pair(22739,23373)); |
| 72 | + expectedCode.add("c.addHierarchyListener(evt -> {\n" |
| 73 | + + " log(out, \"FileDrop: Hierarchy changed.\");\n" |
| 74 | + + " final Component parent = c.getParent();\n" |
| 75 | + + " if (parent == null) {\n" |
| 76 | + + " c.setDropTarget(null);\n" |
| 77 | + + " log(out, \"FileDrop: Drop target cleared from component.\");\n" |
| 78 | + + " } \n" |
| 79 | + + " else {\n" |
| 80 | + + " new DropTarget(c, dropListener);\n" |
| 81 | + + " log(out, \"FileDrop: Drop target added to component.\");\n" |
| 82 | + + " } \n" |
| 83 | + + "}); \n" |
| 84 | + + "if (c.getParent() != null) {\n" |
| 85 | + + " new DropTarget(c, dropListener);\n" |
| 86 | + + "}"); |
| 87 | + |
| 88 | + for (int i = 0; i < expectedCode.size(); i++) |
| 89 | + { |
| 90 | + cu.add(Utils.createCompilationUnitFromFile(absolutePath + javaFileName.get(i))); |
| 91 | + sequence.add(new Sequence(cu.get(i), pair.get(i))); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @Order(1) |
| 97 | + @DisplayName ("Checking Sequence initialization") |
| 98 | + void initialization() { |
| 99 | + //Comparing code after removing spaces, \n and \t |
| 100 | + for (int i = 0; i < sequence.size(); i++) |
| 101 | + { |
| 102 | + String a = sequence.get(i).toString2(); |
| 103 | + a = a.substring(1, a.length()-1); //removing [ and ] from the original string |
| 104 | + a = a.replace(",", ""); // removing the char used to split lines of code |
| 105 | + a = a.replace(" ", ""); |
| 106 | + a = a.replace("\n", ""); |
| 107 | + a = a.replace("\t", ""); |
| 108 | + |
| 109 | + String b = expectedCode.get(i); |
| 110 | + b = b.replace(" ", ""); |
| 111 | + b = b.replace(",", ""); |
| 112 | + b = b.replace("\n", ""); |
| 113 | + b = b.replace("\t", ""); |
| 114 | + |
| 115 | + assertEquals(a, b); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments