diff options
Diffstat (limited to 'sonar-graph/src/test/java/org/sonar/graph')
13 files changed, 0 insertions, 1039 deletions
diff --git a/sonar-graph/src/test/java/org/sonar/graph/CycleDetectorTest.java b/sonar-graph/src/test/java/org/sonar/graph/CycleDetectorTest.java deleted file mode 100644 index 244ae9f6ada..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/CycleDetectorTest.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CycleDetectorTest { - - @Test - public void testIsAcyclicGraph() { - DirectedGraph<String, StringEdge> dag = DirectedGraph.createStringDirectedGraph(); - dag.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D"); - dag.addEdge("B", "D"); - dag.addEdge("A", "D"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dag); - cycleDetector.detectCycles(); - assertThat(cycleDetector.isAcyclicGraph()).isTrue(); - } - - @Test - public void testIsNotAcyclicGraph() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "A"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - assertThat(cycleDetector.isAcyclicGraph()).isFalse(); - } - - @Test - public void testGetCyclesWithMultipleCycles() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("C", "A"); - dcg.addEdge("B", "A"); - dcg.addEdge("A", "E").addEdge("E", "C"); - dcg.addEdge("E", "D"); - dcg.addEdge("E", "F"); - dcg.addEdge("F", "C"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - assertThat(cycleDetector.getCycles()).hasSize(8); - assertThat(cycleDetector.getSearchCyclesCalls()).isEqualTo(11); - } - - @Test - public void testMaxSearchDepthOption() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("C", "A"); - dcg.addEdge("B", "A"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCyclesWithMaxSearchDepth(3); - assertThat(cycleDetector.getCycles()).hasSize(2); - - cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCyclesWithMaxSearchDepth(2); - assertThat(cycleDetector.getCycles()).hasSize(1); - } - - @Test - public void testExcludeEdgesFromSearch() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("C", "A"); - dcg.addEdge("B", "A"); - - Set<Edge> excludedEdges = new HashSet<>(); - excludedEdges.add(dcg.getEdge("C", "A")); - excludedEdges.add(dcg.getEdge("B", "A")); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg, excludedEdges); - cycleDetector.detectCycles(); - assertThat(cycleDetector.getCycles()).hasSize(1); - } - - @Test - public void testGetCyclesWithOneCycle() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "E"); - dcg.addEdge("B", "A"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - assertThat(cycleDetector.getCycles()).hasSize(1); - Cycle cycle = cycleDetector.getCycles().iterator().next(); - assertThat(cycle.size()).isEqualTo(2); - assertThat(cycle.contains(new StringEdge("A", "B"))).isTrue(); - assertThat(cycle.contains(new StringEdge("B", "A"))).isTrue(); - } - - @Test - public void getCyclesInLimitedSetOfVertices() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "A"); - - // C must not be used to find cycles - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg, Arrays.asList("A", "B")); - cycleDetector.detectCycles(); - assertThat(cycleDetector.getCycles()).isEmpty(); - - // C is used to find cycles - cycleDetector = new CycleDetector<>(dcg, Arrays.asList("A", "B", "C")); - cycleDetector.detectCycles(); - assertThat(cycleDetector.getCycles().size()).isEqualTo(1); - } - - @Test(expected = IllegalStateException.class) - public void testExecutingTwoCycleDetectionsOnSameObject() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "A"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - cycleDetector.detectCycles(); - } - - @Test - public void testDetectCyclesWithUpperLimit() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("B", "A"); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - assertThat(cycleDetector.detectCyclesWithUpperLimit(1)).hasSize(1); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java b/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java deleted file mode 100644 index b90e0cc32e0..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CycleTest { - static List<Edge> AB_BA = list(new StringEdge("A", "B"), new StringEdge("B", "A")); - static List<Edge> BA_AB = list(new StringEdge("B", "A"), new StringEdge("A", "B")); - static List<Edge> AB_BC_CA = list(new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "A")); - static List<Edge> HI_IJ_JH = list(new StringEdge("H", "I"), new StringEdge("I", "J"), new StringEdge("J", "H")); - static List<Edge> AB_BC_CD_DA = list(new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A")); - static List<Edge> BC_CD_DA_AB = list(new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A"), new StringEdge("A", "B")); - - @Test - public void testHashCode() { - assertThat(new Cycle(AB_BA).hashCode()).isEqualTo(new Cycle(BA_AB).hashCode()); - assertThat(new Cycle(BC_CD_DA_AB).hashCode()).isEqualTo(new Cycle(AB_BC_CD_DA).hashCode()); - assertThat(new Cycle(AB_BA).hashCode()).isNotEqualTo(new Cycle(AB_BC_CA).hashCode()); - } - - @Test - public void testContains() { - assertThat(new Cycle(AB_BC_CD_DA).contains(new StringEdge("B", "C"))).isTrue(); - } - - @Test - public void testEqualsObject() { - assertThat(new Cycle(AB_BA)).isEqualTo(new Cycle(BA_AB)); - assertThat(new Cycle(BC_CD_DA_AB)).isEqualTo(new Cycle(AB_BC_CD_DA)); - } - - @Test - public void testNotEqualsObject() { - assertThat(new Cycle(BC_CD_DA_AB)).isNotEqualTo(new Cycle(AB_BA)); - assertThat(new Cycle(AB_BC_CA)).isNotEqualTo(new Cycle(HI_IJ_JH)); - } - - static List<Edge> list(StringEdge... edges) { - return Arrays.<Edge> asList(edges); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DirectedGraphTest.java b/sonar-graph/src/test/java/org/sonar/graph/DirectedGraphTest.java deleted file mode 100644 index fac53692e1a..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DirectedGraphTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.graph.DirectedGraph; -import org.sonar.graph.StringEdge; - -import java.util.Arrays; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -public class DirectedGraphTest { - - private DirectedGraph<String, StringEdge> graph; - - @Before - public void init() { - graph = DirectedGraph.createStringDirectedGraph(); - graph.addEdge("A", "B"); - graph.addEdge("A", "C"); - graph.addEdge("B", "C"); - } - - @Test - public void testGetVertices() { - assertThat(graph.getVertices(), hasItems("A", "B")); - assertThat(graph.getVertices().size(), is(3)); - } - - @Test - public void testGetEdge() { - assertNull(graph.getEdge("A", "T")); - graph.addEdge("A", "T", 5); - assertThat(graph.getEdge("A", "T").getWeight(), is(5)); - } - - @Test(expected = IllegalStateException.class) - public void testAddEdgeThrowsException() { - graph.addEdge("B", "C"); - } - - @Test - public void testAddEdgeWithWeight() { - graph.addEdge("D", "B", 4); - assertThat(graph.getOutgoingEdges("D").iterator().next().getWeight(), is(4)); - } - - @Test - public void testGetOutgoingEdges() { - assertThat(graph.getOutgoingEdges("A"), hasItem(new StringEdge("A", "B"))); - assertThat(graph.getOutgoingEdges("A").size(), is(2)); - assertThat(graph.getOutgoingEdges("B"), hasItem(new StringEdge("B", "C"))); - } - - @Test - public void testGetIncomingEdges() { - assertThat(graph.getIncomingEdges("C"), hasItem(new StringEdge("A", "C"))); - assertThat(graph.getIncomingEdges("A").size(), is(0)); - } - - @Test - public void testGetEdges() { - assertTrue(graph.getEdges(Arrays.asList("A")).containsAll(Arrays.asList(new StringEdge("A", "B"), new StringEdge("A", "C")))); - assertTrue(graph.getEdges(Arrays.asList("A", "B")).containsAll(Arrays.asList(new StringEdge("A", "B"), new StringEdge("A", "C"), new StringEdge("B", "C")))); - } - - @Test - public void testHasEdge() { - assertTrue(graph.hasEdge("A", "B")); - assertFalse(graph.hasEdge("C", "A")); - } - - @Test - public void testAddVertex() { - graph.addVertex("X"); - assertThat(graph.getVertices(), hasItem("X")); - assertThat(graph.getOutgoingEdges("X").size(), is(0)); - } - - @Test - public void testAddVertices() { - String[] vertices = { "X", "Y", "Z" }; - graph.addVertices(Arrays.asList(vertices)); - assertThat(graph.getVertices(), hasItems("X", "Y", "Z")); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DsmManualSorterTest.java b/sonar-graph/src/test/java/org/sonar/graph/DsmManualSorterTest.java deleted file mode 100644 index 7735dd510c5..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DsmManualSorterTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class DsmManualSorterTest { - - @Test - public void testSort() { - DirectedGraph<String, StringEdge> graph = DirectedGraph.createStringDirectedGraph(); - graph.addEdge("A", "B", 2); - graph.addEdge("A", "C", 3); - graph.addEdge("C", "B", 1); - Dsm<String> dsm = new Dsm<>(graph); - DsmManualSorter.sort(dsm, "B", "A", "C"); - - StringPrintWriter expectedDsm = new StringPrintWriter(); - expectedDsm.println(" | B | A | C |"); - expectedDsm.println("B | | 2 | 1 |"); - expectedDsm.println("A | | | |"); - expectedDsm.println("C | | 3 | |"); - - assertEquals(expectedDsm.toString(), DsmPrinter.print(dsm)); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DsmPrinterTest.java b/sonar-graph/src/test/java/org/sonar/graph/DsmPrinterTest.java deleted file mode 100644 index 063e1955b89..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DsmPrinterTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import com.google.common.collect.Sets; -import org.junit.Before; -import org.junit.Test; - -import java.util.HashSet; - -import static org.junit.Assert.assertEquals; - -public class DsmPrinterTest { - - private Dsm<String> dsm; - - @Before - public void init() { - DirectedGraph<String, StringEdge> graph = DirectedGraph.createStringDirectedGraph(); - graph.addEdge("A", "B", 3).addEdge("A", "C", 2); - graph.addEdge("C", "B", 4).addEdge("C", "A", 1); - HashSet<Edge> feedbackEdges = Sets.<Edge>newHashSet(graph.getEdge("C", "A")); - dsm = new Dsm<>(graph, feedbackEdges); - DsmManualSorter.sort(dsm, "A", "C", "B"); - } - - @Test - public void testPrintDsm() { - StringPrintWriter expectedResult = new StringPrintWriter(); - expectedResult.println(" | A | C | B |"); - expectedResult.println("A | | 1*| |"); - expectedResult.println("C | 2 | | |"); - expectedResult.println("B | 3 | 4 | |"); - - assertEquals(expectedResult.toString(), DsmPrinter.print(dsm)); - } - - @Test - public void testPrintDsmWithoutColumnHeaders() { - StringPrintWriter expectedResult = new StringPrintWriter(); - expectedResult.println("A | | 1*| |"); - expectedResult.println("C | 2 | | |"); - expectedResult.println("B | 3 | 4 | |"); - - assertEquals(expectedResult.toString(), DsmPrinter.print(dsm, false)); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DsmScannerTest.java b/sonar-graph/src/test/java/org/sonar/graph/DsmScannerTest.java deleted file mode 100644 index 676a812796e..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DsmScannerTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; -import org.sonar.graph.Dsm; -import org.sonar.graph.DsmCell; -import org.sonar.graph.DsmScanner; - -import static org.junit.Assert.*; - -public class DsmScannerTest { - - @Test - public void testScanString() { - StringPrintWriter builder = new StringPrintWriter(); - builder.println(" | A | B |"); - builder.println("A | | 1*|"); - builder.println("B | 3 | |"); - Dsm dsm = DsmScanner.scan(builder.toString()); - - assertEquals("A", dsm.getVertex(0).toString()); - assertEquals("B", dsm.getVertex(1).toString()); - - assertEquals(2, dsm.getDimension()); - - DsmCell ba = dsm.getCell(1, 0); - assertEquals(1, ba.getWeight()); - assertTrue(ba.isFeedbackEdge()); - - DsmCell ab = dsm.getCell(0, 1); - assertEquals(3, ab.getWeight()); - assertFalse(ab.isFeedbackEdge()); - - assertEquals(0, dsm.getCell(1, 1).getWeight()); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DsmTest.java b/sonar-graph/src/test/java/org/sonar/graph/DsmTest.java deleted file mode 100644 index 9d01eaf1467..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DsmTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.graph.Dsm; -import org.sonar.graph.DsmScanner; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -public class DsmTest { - - private Dsm<String> dsm; - - @Before - public void init() { - StringPrintWriter textDsm = new StringPrintWriter(); - textDsm.println(" | A | B | C | D | E |"); - textDsm.println("A | | 1 | | | |"); - textDsm.println("B | 2 | | | | |"); - textDsm.println("C | 4 | | | | |"); - textDsm.println("D | | 7 | | | 5 |"); - textDsm.println("E | | | | | |"); - dsm = DsmScanner.scan(textDsm.toString()); - } - - @Test - public void testGetVertex() { - assertEquals("A", dsm.getVertex(0)); - assertEquals("B", dsm.getVertex(1)); - assertEquals("C", dsm.getVertex(2)); - assertEquals("D", dsm.getVertex(3)); - assertEquals("E", dsm.getVertex(4)); - } - - @Test - public void testPermute() { - assertEquals(2, dsm.getCell(0, 1).getWeight()); - assertEquals(4, dsm.getCell(0, 2).getWeight()); - - dsm.permute(0, 1); - assertEquals(1, dsm.getCell(0, 1).getWeight()); - assertEquals(0, dsm.getCell(0, 2).getWeight()); - } - - @Test - public void testGetNumberOfOutgoingEdges() { - assertEquals(0, dsm.getNumberOfOutgoingEdges(3, 0, 4)); - assertEquals(2, dsm.getNumberOfOutgoingEdges(0, 0, 4)); - } - - @Test - public void testGetNumberOfIncomingEdges() { - assertThat(dsm.getNumberOfIncomingEdges(0, 0, 4), equalTo(1)); - assertThat(dsm.getNumberOfIncomingEdges(4, 0, 4), equalTo(0)); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/DsmTopologicalSorterTest.java b/sonar-graph/src/test/java/org/sonar/graph/DsmTopologicalSorterTest.java deleted file mode 100644 index 9a22ea4555b..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/DsmTopologicalSorterTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class DsmTopologicalSorterTest { - - @Test - public void sortAcyclicGraph() { - StringPrintWriter textDsm = new StringPrintWriter(); - textDsm.println(" | A | B | C | D | E |"); - textDsm.println("A | | | | | |"); - textDsm.println("B | 3 | | 4 | | |"); - textDsm.println("C | 1 | | | | |"); - textDsm.println("D | | 2 | | | 1 |"); - textDsm.println("E | | 5 | | | |"); - - Dsm<String> dsm = DsmScanner.scan(textDsm.toString()); - DsmTopologicalSorter.sort(dsm); - - StringPrintWriter expectedTextDsm = new StringPrintWriter(); - expectedTextDsm.println(" | A | C | B | E | D |"); - expectedTextDsm.println("A | | | | | |"); - expectedTextDsm.println("C | 1 | | | | |"); - expectedTextDsm.println("B | 3 | 4 | | | |"); - expectedTextDsm.println("E | | | 5 | | |"); - expectedTextDsm.println("D | | | 2 | 1 | |"); - - Dsm<String> expectedDsm = DsmScanner.scan(expectedTextDsm.toString()); - DsmTopologicalSorter.sort(expectedDsm); - - assertEquals(DsmPrinter.print(dsm), DsmPrinter.print(expectedDsm)); - } - - @Test(expected = IllegalStateException.class) - public void sortCyclicGraph() { - StringPrintWriter textDsm = new StringPrintWriter(); - textDsm.println(" | A | B | C | D |"); - textDsm.println("A | | | | |"); - textDsm.println("B | 3 | | 4 | |"); - textDsm.println("C | 1 | 1 | | |"); - textDsm.println("D | | 2 | | |"); - - Dsm<String> dsm = DsmScanner.scan(textDsm.toString()); - DsmTopologicalSorter.sort(dsm); - } - - @Test - public void sortCyclicGraphWithManuallyFlaggedFeedbackEdges() { - StringPrintWriter textDsm = new StringPrintWriter(); - textDsm.println(" | A | B | C | D |"); - textDsm.println("A | | | | |"); - textDsm.println("B | 3 | | 4 | |"); - textDsm.println("C | 1 | 1*| | |"); - textDsm.println("D | | 2 | | |"); - - Dsm<String> dsm = DsmScanner.scan(textDsm.toString()); - DsmTopologicalSorter.sort(dsm); - - StringPrintWriter expectedDsm = new StringPrintWriter(); - expectedDsm.println(" | A | C | B | D |"); - expectedDsm.println("A | | | | |"); - expectedDsm.println("C | 1 | | 1*| |"); - expectedDsm.println("B | 3 | 4 | | |"); - expectedDsm.println("D | | | 2 | |"); - - assertEquals(expectedDsm.toString(), DsmPrinter.print(dsm)); - } - - @Test - public void sortCyclicGraphWithFlaggedFeedbackEdges() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 3).addEdge("B", "A", 1); - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - - Dsm<String> dsm = new Dsm<>(dcg, solver.getEdges()); - DsmTopologicalSorter.sort(dsm); - - StringPrintWriter expectedDsm = new StringPrintWriter(); - expectedDsm.println(" | A | B |"); - expectedDsm.println("A | | 1*|"); - expectedDsm.println("B | 3 | |"); - - assertEquals(expectedDsm.toString(), DsmPrinter.print(dsm)); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/FeedbackCycleTest.java b/sonar-graph/src/test/java/org/sonar/graph/FeedbackCycleTest.java deleted file mode 100644 index e14a97e691a..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/FeedbackCycleTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class FeedbackCycleTest { - - private Edge[] AB_Edges = { new StringEdge("A", "B"), new StringEdge("B", "A") }; - private Edge[] ABC_Edges = { new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "A") }; - private Edge[] BCDA_Edges = { new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A"), new StringEdge("A", "B"), }; - private Edge[] EF_Edges = { new StringEdge("E", "F"), new StringEdge("F", "E") }; - private Edge[] GHIJ_Edges = { new StringEdge("G", "H"), new StringEdge("H", "I"), new StringEdge("I", "J"), new StringEdge("J", "G") }; - private Edge[] XYZW_Edges = { new StringEdge("X", "Y"), new StringEdge("Y", "Z"), new StringEdge("Z", "W"), new StringEdge("W", "X") }; - - private Cycle AB_Cycle = new Cycle(Arrays.asList(AB_Edges)); - private Cycle ABC_Cycle = new Cycle(Arrays.asList(ABC_Edges)); - private Cycle BCDA_Cycle = new Cycle(Arrays.asList(BCDA_Edges)); - private Cycle EF_Cycle = new Cycle(Arrays.asList(EF_Edges)); - private Cycle GHIJ_Cycle = new Cycle(Arrays.asList(GHIJ_Edges)); - private Cycle XYZW_Cycle = new Cycle(Arrays.asList(XYZW_Edges)); - - @Test - public void testBuildFeedbackCycles() { - Set<Cycle> cycles = new HashSet<>(); - cycles.add(AB_Cycle); - cycles.add(ABC_Cycle); - cycles.add(BCDA_Cycle); - cycles.add(EF_Cycle); - cycles.add(GHIJ_Cycle); - cycles.add(XYZW_Cycle); - - List<FeedbackCycle> feedbackCycles = FeedbackCycle.buildFeedbackCycles(cycles); - assertEquals(6, feedbackCycles.size()); - assertEquals(EF_Cycle, feedbackCycles.get(0).getCycle()); - assertEquals(AB_Cycle, feedbackCycles.get(1).getCycle()); - assertEquals(GHIJ_Cycle, feedbackCycles.get(2).getCycle()); - assertEquals(XYZW_Cycle, feedbackCycles.get(3).getCycle()); - assertEquals(ABC_Cycle, feedbackCycles.get(4).getCycle()); - assertEquals(BCDA_Cycle, feedbackCycles.get(5).getCycle()); - } -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/FeedbackEdgeTest.java b/sonar-graph/src/test/java/org/sonar/graph/FeedbackEdgeTest.java deleted file mode 100644 index 366b5d2a231..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/FeedbackEdgeTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; - -public class FeedbackEdgeTest { - - @Test - public void testWeights() { - FeedbackEdge fEdge = mockFeedbackEdge(14, 2); - assertThat(fEdge.getWeight(), is(14)); - assertThat(fEdge.getRelativeWeight(), is(7.0)); - assertThat(fEdge.getOccurences(), is(2)); - } - - @Test - public void testCompareTo() { - FeedbackEdge feedbackEdge1; - FeedbackEdge feedbackEdge2; - FeedbackEdge feedbackEdge3; - - feedbackEdge1 = mockFeedbackEdge(14, 2); - feedbackEdge2 = mockFeedbackEdge(10, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(1)); - - feedbackEdge1 = mockFeedbackEdge(10, 2); - feedbackEdge2 = mockFeedbackEdge(14, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(-1)); - - feedbackEdge1 = mockFeedbackEdge(14, 2); - feedbackEdge2 = mockFeedbackEdge(14, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(0)); - - feedbackEdge1 = mockFeedbackEdge(14, 2); - feedbackEdge2 = mockFeedbackEdge(13, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(1)); - - feedbackEdge1 = mockFeedbackEdge(13, 2); - feedbackEdge2 = mockFeedbackEdge(14, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(-1)); - - feedbackEdge1 = mockFeedbackEdge("A", "B", 14, 2); - feedbackEdge2 = mockFeedbackEdge("B", "C", 14, 2); - feedbackEdge3 = mockFeedbackEdge("C", "A", 14, 2); - assertThat(feedbackEdge1.compareTo(feedbackEdge2), is(-1)); - assertThat(feedbackEdge2.compareTo(feedbackEdge3), is(-1)); - assertThat(feedbackEdge3.compareTo(feedbackEdge1), greaterThan(1)); - } - - private FeedbackEdge mockFeedbackEdge(int weight, int occurrences) { - return mockFeedbackEdge("from", "to", weight, occurrences); - } - - private FeedbackEdge mockFeedbackEdge(String from, String to, int weight, int occurrences) { - Edge edge = new StringEdge(from, to, weight); - return new FeedbackEdge(edge, occurrences); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/IncrementalCyclesAndFESSolverTest.java b/sonar-graph/src/test/java/org/sonar/graph/IncrementalCyclesAndFESSolverTest.java deleted file mode 100644 index 104c11044a9..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/IncrementalCyclesAndFESSolverTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class IncrementalCyclesAndFESSolverTest { - - @Test - public void testSimpleCase() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("C", "A"); - dcg.addEdge("B", "A"); - dcg.addEdge("A", "E").addEdge("E", "C"); - dcg.addEdge("E", "D"); - dcg.addEdge("E", "F"); - dcg.addEdge("F", "C"); - - IncrementalCyclesAndFESSolver<String> cyclesAndFESSolver = new IncrementalCyclesAndFESSolver<>(dcg, dcg.getVertices(), 3, - Integer.MAX_VALUE); - assertThat(cyclesAndFESSolver.getCycles().size(), is(4)); - assertThat(cyclesAndFESSolver.getFeedbackEdgeSet().size(), is(2)); - assertThat(cyclesAndFESSolver.getWeightOfFeedbackEdgeSet(), is(2)); - } - - @Test - public void testWithNoCycleUnderTheThreshold() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - - IncrementalCyclesAndFESSolver<String> cyclesAndFESSolver = new IncrementalCyclesAndFESSolver<>(dcg, dcg.getVertices(), 2, - Integer.MAX_VALUE); - assertThat(cyclesAndFESSolver.getCycles().size(), is(1)); - assertThat(cyclesAndFESSolver.getFeedbackEdgeSet().size(), is(1)); - assertThat(cyclesAndFESSolver.getWeightOfFeedbackEdgeSet(), is(1)); - } - - @Test - public void testBothMaxSearchDepthAtFirstAndMaxCyclesToFoundByIteration() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B").addEdge("B", "C").addEdge("C", "D").addEdge("D", "A"); - dcg.addEdge("E", "F").addEdge("F", "G").addEdge("G", "E"); - dcg.addEdge("H", "I").addEdge("I", "H"); - - IncrementalCyclesAndFESSolver<String> cyclesAndFESSolver = new IncrementalCyclesAndFESSolver<>(dcg, dcg.getVertices(), 2, 1); - assertThat(cyclesAndFESSolver.getCycles().size(), is(3)); - assertThat(cyclesAndFESSolver.getIterations(), is(4)); - cyclesAndFESSolver.getFeedbackEdgeSet(); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/MinimumFeedbackEdgeSetSolverTest.java b/sonar-graph/src/test/java/org/sonar/graph/MinimumFeedbackEdgeSetSolverTest.java deleted file mode 100644 index 580cc64f37f..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/MinimumFeedbackEdgeSetSolverTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -import java.util.Set; - -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -public class MinimumFeedbackEdgeSetSolverTest { - - @Test - public void testGetFeedbackEdgesOnSimpleLoop() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 3).addEdge("B", "A", 1); - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - Set<Edge> feedbackEdges = solver.getEdges(); - assertThat(feedbackEdges.size(), is(1)); - } - - @Test - public void testFlagFeedbackEdgesOnSimpleLoop() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 3).addEdge("B", "A", 1); - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - Set<Edge> feedbackEdges = solver.getEdges(); - assertTrue(feedbackEdges.contains(dcg.getEdge("B", "A"))); - } - - @Test - public void testGetFeedbackEdgesOnComplexGraph() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 7).addEdge("B", "C", 3).addEdge("C", "D", 1).addEdge("D", "A", 3); - dcg.addEdge("B", "A", 12); - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - Set<Edge> feedbackEdges = solver.getEdges(); - assertThat(feedbackEdges.size(), is(1)); - - assertTrue(feedbackEdges.contains(dcg.getEdge("A", "B"))); - } - - @Test - public void testFlagFeedbackEdgesOnUnrelatedCycles() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 7).addEdge("B", "C", 3).addEdge("C", "A", 2); - dcg.addEdge("D", "E", 3).addEdge("E", "D", 5); - dcg.addEdge("F", "G", 1).addEdge("G", "H", 4).addEdge("H", "F", 7); - - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - Set<Edge> feedbackEdges = solver.getEdges(); - - assertThat(feedbackEdges.size(), is(3)); - - assertTrue(feedbackEdges.contains(dcg.getEdge("C", "A"))); - assertTrue(feedbackEdges.contains(dcg.getEdge("D", "E"))); - assertTrue(feedbackEdges.contains(dcg.getEdge("F", "G"))); - } - - @Test - public void testApproximateMinimumFeedbackEdges() { - DirectedGraph<String, StringEdge> dcg = DirectedGraph.createStringDirectedGraph(); - dcg.addEdge("A", "B", 5).addEdge("B", "C", 9).addEdge("C", "A", 1); - dcg.addEdge("D", "B", 5).addEdge("C", "D", 7); - dcg.addEdge("F", "B", 5).addEdge("C", "F", 4); - CycleDetector<String> cycleDetector = new CycleDetector<>(dcg); - cycleDetector.detectCycles(); - - MinimumFeedbackEdgeSetSolver minimumSolver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles()); - assertThat(minimumSolver.getEdges().size(), is(1)); - assertTrue(minimumSolver.getEdges().contains(dcg.getEdge("B", "C"))); - - MinimumFeedbackEdgeSetSolver approximateSolver = new MinimumFeedbackEdgeSetSolver(cycleDetector.getCycles(), 2); - assertThat(approximateSolver.getEdges().size(), is(2)); - assertTrue(approximateSolver.getEdges().contains(dcg.getEdge("B", "C"))); - assertTrue(approximateSolver.getEdges().contains(dcg.getEdge("C", "A"))); - } - -} diff --git a/sonar-graph/src/test/java/org/sonar/graph/StringPrintWriter.java b/sonar-graph/src/test/java/org/sonar/graph/StringPrintWriter.java deleted file mode 100644 index 4ac77c0675d..00000000000 --- a/sonar-graph/src/test/java/org/sonar/graph/StringPrintWriter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.graph; - -public class StringPrintWriter { - - private StringBuilder builder = new StringBuilder(); - - public void println(String line) { - builder.append(line + " \r"); - } - - @Override - public String toString() { - return builder.toString(); - } - -} |