diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-06 14:08:06 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-06 14:08:06 +0000 |
commit | aeadc1f9129274949daaa57738c7c4550bdfbc7b (patch) | |
tree | 08dadf5ef7474fc41d1d48f74648f1ba8b55f34d /sonar-squid/src/test | |
download | sonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.tar.gz sonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.zip |
SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console
Diffstat (limited to 'sonar-squid/src/test')
29 files changed, 1922 insertions, 0 deletions
diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/CheckMessageTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/CheckMessageTest.java new file mode 100644 index 00000000000..d2f59c2ef79 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/api/CheckMessageTest.java @@ -0,0 +1,35 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.api; + +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class CheckMessageTest { + + @Test + public void testFormatDefaultMessage() { + CheckMessage message = new CheckMessage(null, "Value is {0,number,integer}, expected value is {1,number,integer}.", 3, 7); + assertThat(message.formatDefaultMessage(), is("Value is 3, expected value is 7.")); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java new file mode 100644 index 00000000000..471a6b4d82a --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java @@ -0,0 +1,135 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.api; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.squid.measures.Measurable; +import org.sonar.squid.measures.Metric; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class SourceCodeTest { + + private SourceProject prj; + private SourcePackage pac; + private SourcePackage pac2; + private SourceCode cla; + private SourceCode cla2; + + @Before + public void before() { + prj = new SourceProject("dummy project"); + pac = new SourcePackage("org.sonar"); + pac2 = new SourcePackage("org.sonar2"); + pac2 = new SourcePackage("org.sonar2"); + cla = new SourceClass("org.sonar.Toto", "Toto"); + cla2 = new SourceClass("org.sonar2.Tata", "Tata"); + prj.addChild(pac); + prj.addChild(pac2); + pac.addChild(cla); + pac.addChild(cla2); + } + + @Test + public void testAddChild() { + prj.addChild(pac); + assertEquals(pac.getParent(), prj); + assertTrue(prj.getChildren().contains(pac)); + } + + @Test + public void testEqualsAndHashCode() { + assertFalse((prj.equals(pac))); + assertFalse(prj.hashCode() == pac.hashCode()); + assertFalse(prj.equals(new Object())); + SourceCode samePac = new SourcePackage("org.sonar"); + assertEquals(pac, samePac); + assertEquals(pac.hashCode(), samePac.hashCode()); + } + + @Test + public void testContains() { + assertThat(prj.hasChild(pac), is(true)); + assertThat(prj.hasChild(cla), is(true)); + } + + @Test + public void testIsType() { + SourcePackage pacFrom = new SourcePackage("org.from"); + assertFalse(pacFrom.isType(SourceCode.class)); + assertFalse(pacFrom.isType(SourceClass.class)); + assertTrue(pacFrom.isType(SourcePackage.class)); + } + + @Test + public void testGetParentByType() { + SourcePackage pacFrom = new SourcePackage("org.from"); + SourceFile fileFrom = new SourceFile("org.from.From.java", "From.java"); + SourceClass classFrom = new SourceClass("org.from.From", "From"); + pacFrom.addChild(fileFrom); + fileFrom.addChild(classFrom); + assertEquals(pacFrom, classFrom.getParent(SourcePackage.class)); + } + + @Test + public void testHasAmongParents() { + assertTrue(cla.hasAmongParents(prj)); + assertTrue(cla.hasAmongParents(pac)); + assertFalse(prj.hasAmongParents(cla)); + } + + @Test(expected = IllegalStateException.class) + public void setMeasureOnCalculatedMetric() { + Measurable measurable = new SourceFile("org.Toto.java"); + measurable.setMeasure(Metric.INSTABILITY, 0); + } + + @Test + public void testConmputeMeasures() { + cla2.setMeasure(Metric.COMPLEXITY, 4); + cla.setMeasure(Metric.COMPLEXITY, 2); + cla.setMeasure(Metric.CA, 2); + SourceCodeTreeDecorator decorator = new SourceCodeTreeDecorator(prj); + decorator.decorateWith(Metric.values()); + assertEquals(6, prj.getInt(Metric.COMPLEXITY)); + assertEquals(0, prj.getInt(Metric.CA)); + } + + @Test + public void testAddData() { + SourceCode myFile = new SourceFile("org.sonar2.Tata.java"); + myFile.addData(Metric.LCOM4_BLOCKS, "blocks detail"); + assertEquals("blocks detail", myFile.getData(Metric.LCOM4_BLOCKS)); + } + + @Test + public void getCheckMessages() { + SourceCode foo = new SourceFile("Foo.java"); + assertThat(foo.getCheckMessages().size(), is(0)); + + foo.log(new CheckMessage(null, "message")); + assertThat(foo.getCheckMessages().size(), is(1)); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTreeDecoratorTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTreeDecoratorTest.java new file mode 100644 index 00000000000..71f9c7073c8 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTreeDecoratorTest.java @@ -0,0 +1,120 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.api; + +import org.junit.Test; +import org.sonar.squid.measures.Metric; + +import static org.junit.Assert.assertEquals; + +public class SourceCodeTreeDecoratorTest { + + private int idCounter = 0; + + @Test + public void addMethodMeasures() { + SourceCode method1 = new SourceMethod("method1"); + method1.setMeasure(Metric.COMPLEXITY, 4); + method1.setMeasure(Metric.STATEMENTS, 8); + method1.setMeasure(Metric.METHODS, 1); + SourceCode method2 = new SourceMethod("method2"); + method2.setMeasure(Metric.COMPLEXITY, 2); + method2.setMeasure(Metric.STATEMENTS, 3); + method2.setMeasure(Metric.METHODS, 1); + SourceCode method3 = new SourceMethod("method3"); + method3.setMeasure(Metric.COMPLEXITY, 1); + method3.setMeasure(Metric.STATEMENTS, 3); + method3.setMeasure(Metric.METHODS, 1); + SourceCode class1 = new SourceClass("class1"); + class1.addChild(method1); + class1.addChild(method2); + class1.addChild(method3); + + SourceProject project = new SourceProject("project"); + project.addChild(class1); + decorate(project); + + assertEquals(3, class1.getInt(Metric.METHODS)); + assertEquals(7, class1.getInt(Metric.COMPLEXITY)); + } + + private SourceCode createTestMethod(SourceCode classResource) { + SourceCode method = new SourceMethod("test" + idCounter++); + classResource.addChild(method); + return method; + } + + private SourceCode createTestClass() { + return new SourceClass("class" + idCounter++); + } + + private SourceCode createTestComplexityMethod(SourceCode classResource, int complexity) { + SourceCode method = createTestMethod(classResource); + method.setMeasure(Metric.COMPLEXITY, complexity); + return method; + } + + @Test + public void classMethodComplexityDistribution() { + SourceCode testClass = createTestClass(); + createTestComplexityMethod(testClass, 4); + createTestComplexityMethod(testClass, 2); + createTestComplexityMethod(testClass, 3); + SourceProject project = new SourceProject("project"); + project.addChild(testClass); + decorate(project); + } + + @Test + public void addPackageMeasures() { + SourceCode package1 = new SourcePackage("pack1"); + package1.setMeasure(Metric.CLASSES, 12); + package1.setMeasure(Metric.METHODS, 87); + package1.setMeasure(Metric.COMPLEXITY, 834); + package1.setMeasure(Metric.LINES, 1450); + package1.setMeasure(Metric.PACKAGES, 1); + SourceCode package2 = new SourcePackage("pack2"); + package2.setMeasure(Metric.CLASSES, 9); + package2.setMeasure(Metric.METHODS, 73); + package2.setMeasure(Metric.COMPLEXITY, 287); + package2.setMeasure(Metric.LINES, 893); + package2.setMeasure(Metric.PACKAGES, 1); + SourceCode package3 = new SourcePackage("pack3"); + package3.setMeasure(Metric.CLASSES, 9); + package3.setMeasure(Metric.METHODS, 73); + package3.setMeasure(Metric.COMPLEXITY, 287); + package3.setMeasure(Metric.LINES, 938); + package3.setMeasure(Metric.PACKAGES, 1); + SourceProject prj1 = new SourceProject("prj1"); + prj1.addChild(package1); + prj1.addChild(package2); + prj1.addChild(package3); + decorate(prj1); + assertEquals(3, prj1.getInt(Metric.PACKAGES)); + assertEquals(30, prj1.getInt(Metric.CLASSES)); + assertEquals(233, prj1.getInt(Metric.METHODS)); + assertEquals(3281, prj1.getInt(Metric.LINES)); + } + + private void decorate(SourceProject project) { + SourceCodeTreeDecorator decorator = new SourceCodeTreeDecorator(project); + decorator.decorateWith(Metric.values()); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/SourceFileTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/SourceFileTest.java new file mode 100644 index 00000000000..bae9c06c464 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/api/SourceFileTest.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.api; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class SourceFileTest { + + @Test + public void testGetStartAtLine() { + SourceFile file = new SourceFile("com/sonarsource/Toto.java"); + assertEquals(1, file.getStartAtLine()); + file = new SourceFile("com/sonarsource/Toto.java", "Toto.java"); + assertEquals(1, file.getStartAtLine()); + } + + @Test + public void testHasNoSon() { + SourceFile file = new SourceFile("com/sonarsource/Toto.java"); + Set<Integer> noSonarTagLines = new HashSet<Integer>(); + noSonarTagLines.add(23); + noSonarTagLines.add(10); + file.addNoSonarTagLines(noSonarTagLines); + assertTrue(file.hasNoSonarTagAtLine(23)); + assertTrue(file.hasNoSonarTagAtLine(10)); + assertFalse(file.hasNoSonarTagAtLine(11)); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/SourceMethodTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/SourceMethodTest.java new file mode 100644 index 00000000000..bb8183cbccb --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/api/SourceMethodTest.java @@ -0,0 +1,34 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.api; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SourceMethodTest { + + @Test + public void testSquidMethodSquidClassString() { + SourceMethod squidMethod = new SourceMethod(new SourceClass("org.sonar.Squid"), "scan:23", 23); + assertEquals("org.sonar.Squid#scan:23", squidMethod.getKey()); + assertEquals(23, squidMethod.getStartAtLine()); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/indexer/SquidIndexTest.java b/sonar-squid/src/test/java/org/sonar/squid/indexer/SquidIndexTest.java new file mode 100644 index 00000000000..1d19632c9eb --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/indexer/SquidIndexTest.java @@ -0,0 +1,111 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.indexer; + +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.squid.api.SourceClass; +import org.sonar.squid.api.SourceCode; +import org.sonar.squid.api.SourceFile; +import org.sonar.squid.api.SourcePackage; +import org.sonar.squid.api.SourceProject; +import org.sonar.squid.indexer.QueryByMeasure.Operator; +import org.sonar.squid.measures.Metric; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class SquidIndexTest { + + private SquidIndex indexer; + private SourceProject project; + private SourcePackage packSquid; + private SourceFile fileSquid; + private SourceFile file2Squid; + private SourceCode classSquid; + + @Before + public void setup() { + indexer = new SquidIndex(); + project = new SourceProject("Squid Project"); + indexer.index(project); + packSquid = new SourcePackage("org.sonar.squid"); + project.addChild(packSquid); + fileSquid = new SourceFile("org.sonar.squid.Squid.java", "Squid.java"); + packSquid.addChild(fileSquid); + file2Squid = new SourceFile("org.sonar.squid.SquidConfiguration.java", "SquidConfiguration.java"); + packSquid.addChild(file2Squid); + classSquid = new SourceClass("org.sonar.squid.Squid", "Squid"); + fileSquid.addChild(classSquid); + } + + @Test + public void searchSingleResource() { + SourceCode squidClass = indexer.search("org.sonar.squid.Squid"); + assertEquals(new SourceClass("org.sonar.squid.Squid", "Squid"), squidClass); + SourceCode javaNCSSClass = indexer.search("org.sonar.squid.JavaNCSS"); + assertNull(javaNCSSClass); + } + + @Test + public void searchByType() { + Collection<SourceCode> resources = indexer.search(new QueryByType(SourceFile.class)); + assertEquals(2, resources.size()); + resources = indexer.search(new QueryByType(SourceClass.class)); + assertEquals(1, resources.size()); + assertTrue(resources.contains(classSquid)); + } + + @Test + public void searchByName() { + Collection<SourceCode> resources = indexer.search(new QueryByName("Squid.java")); + assertEquals(1, resources.size()); + assertTrue(resources.contains(fileSquid)); + } + + @Test + public void searchByParent() { + Collection<SourceCode> resources = indexer.search(new QueryByParent(packSquid)); + assertEquals(3, resources.size()); + } + + @Test + public void searchByParentAndByType() { + Collection<SourceCode> resources = indexer.search(new QueryByParent(packSquid), new QueryByType(SourceClass.class)); + assertEquals(1, resources.size()); + assertTrue(resources.contains(classSquid)); + } + + @Test + public void searchByMeasure() { + fileSquid.add(Metric.COMPLEXITY, 2); + assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN, 1)).size()); + assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN_EQUALS, 2)).size()); + assertEquals(0, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.GREATER_THAN, 3)).size()); + assertEquals(4, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN, 1)).size()); + assertEquals(5, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN, 3)).size()); + assertEquals(5, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.LESS_THAN_EQUALS, 2)).size()); + assertEquals(0, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.EQUALS, 6)).size()); + assertEquals(1, indexer.search(new QueryByMeasure(Metric.COMPLEXITY, Operator.EQUALS, 2)).size()); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/math/MeasuresDistributionTest.java b/sonar-squid/src/test/java/org/sonar/squid/math/MeasuresDistributionTest.java new file mode 100644 index 00000000000..b18003ee3f7 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/math/MeasuresDistributionTest.java @@ -0,0 +1,65 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.math; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.squid.api.SourceCode; +import org.sonar.squid.api.SourceFile; +import org.sonar.squid.measures.Metric; + +import java.util.Arrays; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class MeasuresDistributionTest { + + private MeasuresDistribution distribution; + + @Before + public void setup() { + SourceFile file0 = newFile("File0.java", 0); + SourceFile file1 = newFile("File1.java", 1); + SourceFile file8 = newFile("File8.java", 8); + SourceFile file10 = newFile("File10.java", 10); + SourceFile file20 = newFile("File20.java", 20); + SourceFile file21 = newFile("File21.java", 21); + SourceFile file30 = newFile("File3.java", 30); + distribution = new MeasuresDistribution(Arrays.<SourceCode>asList(file0, file1, file8, file10, file20, file21, file30)); + } + + private SourceFile newFile(String filename, int complexity) { + SourceFile file0 = new SourceFile(filename); + file0.setMeasure(Metric.COMPLEXITY, complexity); + return file0; + } + + @Test + public void testComplexityDistribution() { + Map<Integer, Integer> intervals = distribution.distributeAccordingTo(Metric.COMPLEXITY, 1, 10, 18, 25); + assertEquals(4, intervals.size()); + assertEquals(2, (int) intervals.get(1)); // between 1 included and 10 excluded + assertEquals(1, (int) intervals.get(10));// between 10 included and 18 excluded + assertEquals(2, (int) intervals.get(18)); + assertEquals(1, (int) intervals.get(25)); // >= 25 + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/AbstractnessFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/AbstractnessFormulaTest.java new file mode 100644 index 00000000000..11515a56d3d --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/AbstractnessFormulaTest.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourcePackage; +import org.sonar.squid.measures.AbstractnessFormula; +import org.sonar.squid.measures.Metric; + +import static org.junit.Assert.assertEquals; + +public class AbstractnessFormulaTest { + + AbstractnessFormula abstractness = new AbstractnessFormula(); + Measurable measurable = new SourcePackage("pac1"); + + @Test + public void testCalculate() { + measurable.setMeasure(Metric.CLASSES, 10); + measurable.setMeasure(Metric.INTERFACES, 1); + measurable.setMeasure(Metric.ABSTRACT_CLASSES, 1); + + assertEquals(0.2, abstractness.calculate(measurable), 0); + } + + @Test + public void testCalculateOnEmptyProject() { + measurable.setMeasure(Metric.CLASSES, 0); + measurable.setMeasure(Metric.INTERFACES, 0); + measurable.setMeasure(Metric.ABSTRACT_CLASSES, 0); + + assertEquals(0, abstractness.calculate(measurable), 0); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesDensityFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesDensityFormulaTest.java new file mode 100644 index 00000000000..fb6129ee27f --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesDensityFormulaTest.java @@ -0,0 +1,47 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourceClass; + +import static org.junit.Assert.*; + + +public class CommentLinesDensityFormulaTest { + + CommentLinesDensityFormula formula = new CommentLinesDensityFormula(); + Measurable measurable = new SourceClass("com.Toto"); + + @Test + public void calculateDensityOnEmptyFile() { + measurable.setMeasure(Metric.LINES_OF_CODE, 0); + measurable.setMeasure(Metric.COMMENT_LINES, 0); + assertEquals(0, measurable.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01); + } + + @Test + public void calculate() { + measurable.setMeasure(Metric.LINES_OF_CODE, 10); + measurable.setMeasure(Metric.COMMENT_LINES, 10); + assertEquals(0.5, measurable.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesWithoutHeaderFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesWithoutHeaderFormulaTest.java new file mode 100644 index 00000000000..2d505d04255 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/CommentLinesWithoutHeaderFormulaTest.java @@ -0,0 +1,38 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourceClass; + +import static org.junit.Assert.assertEquals; + +public class CommentLinesWithoutHeaderFormulaTest { + + CommentLinesWithoutHeaderFormula formula = new CommentLinesWithoutHeaderFormula(); + Measurable measurable = new SourceClass("com.Toto"); + + @Test + public void calculateDensityOnEmptyFile() { + measurable.setMeasure(Metric.COMMENT_LINES, 10); + measurable.setMeasure(Metric.HEADER_COMMENT_LINES, 5); + assertEquals(5, measurable.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER)); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/DistanceFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/DistanceFormulaTest.java new file mode 100644 index 00000000000..fb4bd38893a --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/DistanceFormulaTest.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourcePackage; + +import static org.junit.Assert.assertEquals; + +public class DistanceFormulaTest { + + DistanceFormula distance = new DistanceFormula(); + Measurable measurable = new SourcePackage("pac1"); + + @Test + public void calculateBestDistance() { + measurable.setMeasure(Metric.CLASSES, 5); + measurable.setMeasure(Metric.INTERFACES, 5); + measurable.setMeasure(Metric.CA, 10); + measurable.setMeasure(Metric.CE, 10); + assertEquals(0.5, measurable.getDouble(Metric.DISTANCE), 0.01); + } + + @Test + public void calculateWorstDistance() { + measurable.setMeasure(Metric.CLASSES, 5); + measurable.setMeasure(Metric.CA, 10); + measurable.setMeasure(Metric.CE, 0); + assertEquals(1, measurable.getDouble(Metric.DISTANCE), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/InstabilityFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/InstabilityFormulaTest.java new file mode 100644 index 00000000000..110cb641399 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/InstabilityFormulaTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourcePackage; + +import static org.junit.Assert.assertEquals; + +public class InstabilityFormulaTest { + + InstabilityFormula distance = new InstabilityFormula(); + Measurable measurable = new SourcePackage("pac1"); + + @Test + public void calculateBestStability() { + measurable.setMeasure(Metric.CA, 50); + measurable.setMeasure(Metric.CE, 0); + assertEquals(0, measurable.getDouble(Metric.INSTABILITY), 0.01); + } + + @Test + public void calculateWorstStability() { + measurable.setMeasure(Metric.CA, 0); + measurable.setMeasure(Metric.CE, 10); + assertEquals(1, measurable.getDouble(Metric.INSTABILITY), 0.01); + } + + @Test + public void calculateOnIsolatedProject() { + measurable.setMeasure(Metric.CA, 0); + measurable.setMeasure(Metric.CE, 0); + assertEquals(0, measurable.getDouble(Metric.INSTABILITY), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/MeanAggregationFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/MeanAggregationFormulaTest.java new file mode 100644 index 00000000000..498b91bb272 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/MeanAggregationFormulaTest.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import static org.junit.Assert.*; + +import org.sonar.squid.api.SourceClass; + + +public class MeanAggregationFormulaTest { + + MeanAggregationFormula formula = new MeanAggregationFormula(); + + @Test + public void testAggregate() { + List<Measurable> measurables = new ArrayList<Measurable>(); + SourceClass class1 = new SourceClass("com.My"); + class1.setMeasure(Metric.COMPLEXITY, 2); + measurables.add(class1); + SourceClass class2 = new SourceClass("com.My"); + class2.setMeasure(Metric.COMPLEXITY, 3); + measurables.add(class2); + + assertEquals(2.5, formula.aggregate(Metric.COMPLEXITY, measurables), 0.01); + } + + @Test + public void testAggregateEmptyCollections() { + List<Measurable> measurables = new ArrayList<Measurable>(); + assertEquals(0, formula.aggregate(Metric.COMPLEXITY, measurables), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/MeasuresTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/MeasuresTest.java new file mode 100644 index 00000000000..ab5b477a493 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/MeasuresTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class MeasuresTest { + + private Measures measures = new Measures(); + + @Test + public void testGetValue() { + assertEquals(0, measures.getValue(Metric.ACCESSORS), 0.1); + } + + @Test + public void testGetData() { + assertNull(measures.getData(Metric.LCOM4_BLOCKS)); + } + + @Test + public void testSetValue() { + measures.setValue(Metric.ACCESSORS, 3); + assertEquals(3, measures.getValue(Metric.ACCESSORS), 0.1); + } + + @Test + public void testSetData() { + measures.setData(Metric.LCOM4_BLOCKS, "blocks detail"); + assertEquals("blocks detail", measures.getData(Metric.LCOM4_BLOCKS)); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/PublicDocumentedApiDensityFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/PublicDocumentedApiDensityFormulaTest.java new file mode 100644 index 00000000000..4e2af0ad764 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/PublicDocumentedApiDensityFormulaTest.java @@ -0,0 +1,47 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import org.junit.Test; +import org.sonar.squid.api.SourceClass; + +import static org.junit.Assert.*; + + +public class PublicDocumentedApiDensityFormulaTest { + + PublicDocumentedApiDensityFormula formula = new PublicDocumentedApiDensityFormula(); + Measurable measurable = new SourceClass("com.Toto"); + + @Test + public void calculateWhenNoPublicApi() { + measurable.setMeasure(Metric.PUBLIC_API, 0); + measurable.setMeasure(Metric.PUBLIC_DOC_API, 0); + assertEquals(1, measurable.getDouble(Metric.PUBLIC_DOCUMENTED_API_DENSITY), 0.01); + } + + @Test + public void calculate() { + measurable.setMeasure(Metric.PUBLIC_API, 10); + measurable.setMeasure(Metric.PUBLIC_DOC_API, 5); + assertEquals(0.5, measurable.getDouble(Metric.PUBLIC_DOCUMENTED_API_DENSITY), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/measures/SumAggregationFormulaTest.java b/sonar-squid/src/test/java/org/sonar/squid/measures/SumAggregationFormulaTest.java new file mode 100644 index 00000000000..2c74c53ce2e --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/measures/SumAggregationFormulaTest.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.measures; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import static org.junit.Assert.*; + +import org.sonar.squid.api.SourceClass; + + +public class SumAggregationFormulaTest { + + SumAggregationFormula formula = new SumAggregationFormula(); + + @Test + public void testAggregate() { + List<Measurable> measurables = new ArrayList<Measurable>(); + SourceClass class1 = new SourceClass("com.My"); + class1.setMeasure(Metric.COMPLEXITY, 2); + measurables.add(class1); + SourceClass class2 = new SourceClass("com.My"); + class2.setMeasure(Metric.COMPLEXITY, 3); + measurables.add(class2); + + assertEquals(5, formula.aggregate(Metric.COMPLEXITY, measurables), 0.01); + } + + @Test + public void testAggregateEmptyCollections() { + List<Measurable> measurables = new ArrayList<Measurable>(); + assertEquals(0, formula.aggregate(Metric.COMPLEXITY, measurables), 0.01); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/recognizer/CamelCaseDetectorTest.java b/sonar-squid/src/test/java/org/sonar/squid/recognizer/CamelCaseDetectorTest.java new file mode 100644 index 00000000000..1655aa49005 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/recognizer/CamelCaseDetectorTest.java @@ -0,0 +1,15 @@ +package org.sonar.squid.recognizer; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CamelCaseDetectorTest { + + @Test + public void scan() { + CamelCaseDetector detector = new CamelCaseDetector(0.3); + assertEquals(1, detector.scan("isDog() or isCat()")); + assertEquals(0, detector.scan("String name;")); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/recognizer/ContainsDetectorTest.java b/sonar-squid/src/test/java/org/sonar/squid/recognizer/ContainsDetectorTest.java new file mode 100644 index 00000000000..f2e25e26a35 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/recognizer/ContainsDetectorTest.java @@ -0,0 +1,15 @@ +package org.sonar.squid.recognizer; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ContainsDetectorTest { + + @Test + public void scan() { + ContainsDetector detector = new ContainsDetector(0.3, "++", "for("); + assertEquals(2, detector.scan("for (int i =0; i++; i<4) {")); + assertEquals(0, detector.scan("String name;")); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/recognizer/EndWithDetectorTest.java b/sonar-squid/src/test/java/org/sonar/squid/recognizer/EndWithDetectorTest.java new file mode 100644 index 00000000000..2df5831f1db --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/recognizer/EndWithDetectorTest.java @@ -0,0 +1,16 @@ +package org.sonar.squid.recognizer; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class EndWithDetectorTest { + + @Test + public void scan() { + EndWithDetector detector = new EndWithDetector(0.3, '}'); + assertEquals(1, detector.scan(" return true; }")); + assertEquals(0, detector.scan("} catch(NullPointerException e) {")); + assertEquals(1, detector.scan("} ")); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/recognizer/KeywordsDetectorTest.java b/sonar-squid/src/test/java/org/sonar/squid/recognizer/KeywordsDetectorTest.java new file mode 100644 index 00000000000..fde4f343b6e --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/recognizer/KeywordsDetectorTest.java @@ -0,0 +1,19 @@ +package org.sonar.squid.recognizer; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class KeywordsDetectorTest { + + @Test + public void scan() { + KeywordsDetector detector = new KeywordsDetector(0.3, "public", "static"); + assertEquals(2, detector.scan("public static void main")); + assertEquals(1, detector.scan("private(static} String name;")); + assertEquals(0, detector.scan("publicstatic")); + assertEquals(0, detector.scan("i++;")); + detector = new KeywordsDetector(0.3, true, "PUBLIC"); + assertEquals(2, detector.scan("Public static pubLIC")); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/recognizer/RegexDetectorTest.java b/sonar-squid/src/test/java/org/sonar/squid/recognizer/RegexDetectorTest.java new file mode 100644 index 00000000000..a032916b1be --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/recognizer/RegexDetectorTest.java @@ -0,0 +1,35 @@ +package org.sonar.squid.recognizer; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class RegexDetectorTest { + + @Test(expected = IllegalArgumentException.class) + public void testNegativeProbability() { + new RegexDetector("toto", -1); + } + + @Test(expected = IllegalArgumentException.class) + public void testProbabilityHigherThan1() { + new RegexDetector("toto", 1.2); + } + + @Test + public void testProbability() { + RegexDetector pattern = new RegexDetector("toto", 0.3); + assertEquals(0.3, pattern.recognition(" toto "), 0.01); + assertEquals(0, pattern.recognition("sql"), 0.01); + assertEquals(1 - Math.pow(0.7, 3), pattern.recognition(" toto toto toto "), 0.01); + } + + @Test + public void testSeveralMatches() { + RegexDetector pattern = new RegexDetector("(\\S\\.\\S)", 0.3); // \S is non-whitespace character + assertEquals(0.0, pattern.recognition(" toto "), 0.001); + assertEquals(0.3, pattern.recognition("abc.def ghi jkl"), 0.001); + assertEquals(0.51, pattern.recognition("abc.def.ghi"), 0.001); + assertEquals(0.51, pattern.recognition("abc.def ghi.jkl"), 0.001); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/JavaFootprint.java b/sonar-squid/src/test/java/org/sonar/squid/text/JavaFootprint.java new file mode 100644 index 00000000000..dd50cdd3a4d --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/JavaFootprint.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.sonar.squid.recognizer.*; + +import java.util.HashSet; +import java.util.Set; + +public class JavaFootprint implements LanguageFootprint { + + private final Set<Detector> detectors = new HashSet<Detector>(); + + public JavaFootprint() { + detectors.add(new EndWithDetector(0.95, '}', ';', '{')); // NOSONAR Magic number is suitable in that case + detectors.add(new KeywordsDetector(0.7, "||", "&&")); // NOSONAR + detectors.add(new KeywordsDetector(0.3, "public", "abstract", "class", "implements", "extends", "return","throw",// NOSONAR + "private", "protected", "enum", "continue", "assert", "package", "synchronized", "boolean", "this", "double", "instanceof", + "final", "interface", "static", "void", "long", "int", "float", "super", "true", "case:")); + detectors.add(new ContainsDetector(0.95, "++", "for(", "if(", "while(", "catch(", "switch(", "try{", "else{"));// NOSONAR + detectors.add(new CamelCaseDetector(0.5));// NOSONAR + } + + public Set<Detector> getDetectors() { + return detectors; + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/LineTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/LineTest.java new file mode 100644 index 00000000000..e385d50298d --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/LineTest.java @@ -0,0 +1,90 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; +import org.sonar.squid.measures.Metric; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LineTest { + + @Test + public void testIsThereCode() { + Line line = new Line("//comment"); + line.setComment("//comment"); + assertFalse(line.isThereCode()); + line.setComment(null, false); + assertTrue(line.isThereCode()); + } + + @Test + public void testIsThereComment() { + Line line = new Line(" //comment"); + line.setComment("//comment"); + assertTrue(line.isThereComment()); + } + + @Test + public void testIsThereBlankComment() { + Line line = new Line("//"); + line.setComment("//"); + assertTrue(line.isThereBlankComment()); + } + + @Test(expected = IllegalStateException.class) + public void testUnexpectedMetric() { + Line line = new Line(" //comment"); + line.getInt(Metric.CA); + } + + @Test + public void testIsBlank() { + Line line = new Line(" "); + assertTrue(line.isBlank()); + line.setComment(""); + assertFalse(line.isBlank()); + } + + @Test + public void testIsThereCodeWithBlankLinesBeforeComment() { + Line line = new Line(" //comment"); + line.setComment("//comment"); + assertFalse(line.isThereCode()); + } + + @Test + public void testIsThereCodeWithBlankLinesAfterComment() { + Line line = new Line(" //comment"); + line.setComment("//comment"); + assertFalse(line.isThereCode()); + } + + @Test + public void testIsThereNoSonarTag() { + Line line = new Line(" //NOSONAR"); + line.setComment("//NOSONAR"); + assertTrue(line.isThereNoSonarTag()); + line.setComment(null); + assertFalse(line.isThereNoSonarTag()); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/LinesFactoryTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/LinesFactoryTest.java new file mode 100644 index 00000000000..130d7990862 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/LinesFactoryTest.java @@ -0,0 +1,89 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; + +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class LinesFactoryTest { + + @Test + public void getLines() { + LinesFactory factory = new LinesFactory(new StringReader("import java.util.*;\n\rimport java.io.*;")); + assertEquals(2, factory.getLines().size()); + assertEquals("import java.util.*;", factory.getLines().get(0).getString()); + assertEquals("import java.io.*;", factory.getLines().get(1).getString()); + } + + @Test + public void getLinesWithSingleLineComment() { + LinesFactory factory = new LinesFactory(new StringReader("import java.util.*;\n\rint a = 4; //comments\nimport java.io.*;")); + Line commentLine = factory.getLines().get(1); + assertEquals("int a = 4; //comments", commentLine.getString()); + assertEquals("//comments", commentLine.getComment()); + } + + @Test + public void getLinesWithMultiLineComment() { + LinesFactory factory = new LinesFactory(new StringReader("import java.util.*;\n\rint a = 4; /*comments\nimport java.io.*;*/")); + assertEquals("/*comments", factory.getLines().get(1).getComment()); + assertEquals("import java.io.*;*/", factory.getLines().get(2).getComment()); + } + + @Test + public void testEndOfLineWithLFAndCR() { + LinesFactory factory = new LinesFactory(new StringReader("/*\n\r\n\r\n\r*/")); + assertEquals("/*", factory.getLines().get(0).getComment()); + assertEquals("*/", factory.getLines().get(3).getComment()); + } + + @Test + public void testEndOfLineWithLF() { + LinesFactory factory = new LinesFactory(new StringReader("/*\n\n\n*/")); + assertEquals("/*", factory.getLines().get(0).getComment()); + assertEquals("*/", factory.getLines().get(3).getComment()); + } + + @Test + public void testEndOfLineWithCR() { + LinesFactory factory = new LinesFactory(new StringReader("/*\r\r\r*/")); + assertEquals("/*", factory.getLines().get(0).getComment()); + assertEquals("*/", factory.getLines().get(3).getComment()); + } + + @Test + public void getLinesWithCommentInsideDoubleQuotesString() { + LinesFactory factory = new LinesFactory(new StringReader("String toto = \"//NOSONAR\"")); + Line commentLine = factory.getLines().get(0); + assertNull(commentLine.getComment()); + } + + @Test + public void getLinesWithCommentInsideSingleQuoteString() { + LinesFactory factory = new LinesFactory(new StringReader("String toto = \'//NOSONAR\'")); + Line commentLine = factory.getLines().get(0); + assertNull(commentLine.getComment()); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/LiteralValueHandlerTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/LiteralValueHandlerTest.java new file mode 100644 index 00000000000..b91af09540b --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/LiteralValueHandlerTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LiteralValueHandlerTest { + + @Test + public void matchToBegin() { + LiteralValueHandler handler = new LiteralValueHandler('"'); + assertTrue(handler.matchToBegin(new Line(), new StringBuilder("toto = \""))); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder("toto = \'"))); + } + + @Test + public void matchToEnd() { + LiteralValueHandler handler = new LiteralValueHandler('"'); + assertTrue(handler.matchToEnd(new Line(), new StringBuilder("toto = \"lklj\""))); + assertFalse(handler.matchToEnd(new Line(), new StringBuilder("\\\""))); + assertTrue(handler.matchToEnd(new Line(), new StringBuilder("\\\\\""))); + assertFalse(handler.matchToEnd(new Line(), new StringBuilder("\\\\\\\""))); + assertTrue(handler.matchToEnd(new Line(), new StringBuilder("\\\\\\\\\""))); + assertFalse(handler.matchToEnd(new Line(), new StringBuilder("toto = \'"))); + } + + @Test + public void matchToEndOfLine() { + LiteralValueHandler handler = new LiteralValueHandler('"'); + assertTrue(handler.matchWithEndOfLine(new Line(), new StringBuilder())); + } + +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/MultiLineCommentHandlerTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/MultiLineCommentHandlerTest.java new file mode 100644 index 00000000000..493b9654d6c --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/MultiLineCommentHandlerTest.java @@ -0,0 +1,116 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class MultiLineCommentHandlerTest { + + @Test(expected = IllegalStateException.class) + public void illegalCallToMatchToEnd() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + Line line = new Line(); + StringBuilder builder = new StringBuilder("d"); + handler.matchToEnd(line, builder); + } + + @Test + public void matchWithEndOfLine() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + Line line = new Line(); + StringBuilder builder = new StringBuilder("import java.util.*; /*"); + assertTrue(handler.matchToBegin(line, builder)); + builder.append('N'); + assertFalse(handler.matchToEnd(line, builder)); + builder.append('O'); + assertFalse(handler.matchToEnd(line, builder)); + assertFalse(handler.matchWithEndOfLine(line, builder)); + assertEquals("/*NO", line.getComment()); + builder.append('*'); + assertFalse(handler.matchToEnd(line, builder)); + builder.append('/'); + assertTrue(handler.matchToEnd(line, builder)); + assertEquals("*/", line.getComment()); + } + + @Test + public void testHeaderLicenseComment() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + Line line = new Line(1); + StringBuilder builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + assertFalse(handler.matchWithEndOfLine(line, builder)); + assertTrue(line.isThereLicenseHeaderComment()); + + line = new Line(2); + builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + assertFalse(handler.matchWithEndOfLine(line, builder)); + assertFalse(line.isThereLicenseHeaderComment()); + } + + @Test + public void testJavaDocComment() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + Line line = new Line(1); + StringBuilder builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + builder.append('*'); + assertFalse(handler.matchToEnd(line, builder)); + assertFalse(handler.matchWithEndOfLine(line, builder)); + assertTrue(line.isThereJavadoc()); + + handler = new MultiLinesCommentHandler(); + line = new Line(1); + builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + assertFalse(handler.matchWithEndOfLine(line, builder)); + assertFalse(line.isThereJavadoc()); + } + + @Test + public void matchToBegin() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder("import java.util.*;"))); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder(""))); + assertTrue(handler.matchToBegin(new Line(), new StringBuilder("import java.util.*; /*"))); + } + + @Test + public void testBeginEndCommentWithOnly3Chars() { + MultiLinesCommentHandler handler = new MultiLinesCommentHandler(); + Line line = new Line(1); + StringBuilder builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + builder = new StringBuilder("/*/"); + assertFalse(handler.matchToEnd(line, builder)); + + handler = new MultiLinesCommentHandler(); + line = new Line(1); + builder = new StringBuilder("/*"); + assertTrue(handler.matchToBegin(line, builder)); + builder = new StringBuilder("/**/"); + assertTrue(handler.matchToEnd(line, builder)); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/SingleLineCommentHandlerTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/SingleLineCommentHandlerTest.java new file mode 100644 index 00000000000..70961a3e625 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/SingleLineCommentHandlerTest.java @@ -0,0 +1,67 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class SingleLineCommentHandlerTest { + + @Test(expected = IllegalStateException.class) + public void illegalCallToMatchToEnd() { + SingleLineCommentHandler handler = new SingleLineCommentHandler("//"); + Line line = new Line(); + StringBuilder builder = new StringBuilder("d"); + handler.matchToEnd(line, builder); + } + + @Test + public void matchWithEndOfLine() { + SingleLineCommentHandler handler = new SingleLineCommentHandler("//"); + Line line = new Line(); + StringBuilder builder = new StringBuilder("import java.util.*; //"); + assertTrue(handler.matchToBegin(line, builder)); + builder.append('N'); + assertFalse(handler.matchToEnd(line, builder)); + builder.append('O'); + assertFalse(handler.matchToEnd(line, builder)); + assertTrue(handler.matchWithEndOfLine(line, builder)); + assertEquals("//NO", line.getComment()); + } + + @Test + public void matchToBegin() { + SingleLineCommentHandler handler = new SingleLineCommentHandler("//", "*//"); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder("import java.util.*;"))); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder(""))); + assertTrue(handler.matchToBegin(new Line(), new StringBuilder("import java.util.*; //"))); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder("/*import java.util.*; *//"))); + } + + @Test + public void matchToBeginWithDoubleDash() { + SingleLineCommentHandler handler = new SingleLineCommentHandler("--"); + assertFalse(handler.matchToBegin(new Line(), new StringBuilder("//"))); + assertTrue(handler.matchToBegin(new Line(), new StringBuilder("--"))); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/SourceTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/SourceTest.java new file mode 100644 index 00000000000..73d96313f37 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/SourceTest.java @@ -0,0 +1,180 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; +import org.sonar.squid.measures.Metric; +import org.sonar.squid.recognizer.CodeRecognizer; + +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; + +public class SourceTest { + + private CodeRecognizer codeRecognizer = new CodeRecognizer(0.91, new JavaFootprint()); + + @Test + public void testGetLines() { + String[] lines = { "", "int i = 0;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(2, source.getMeasure(Metric.LINES)); + } + + @Test(expected = IllegalStateException.class) + public void testGetIllegalMetric() { + String[] lines = { "", "int i = 0;" }; + Source source = new Source(lines, codeRecognizer); + source.getMeasure(Metric.COMPLEXITY); + } + + @Test + public void testGetBlankLines() { + String[] lines = { "package toto;", " " }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.BLANK_LINES)); + } + + @Test + public void testGetCppCommentLines() { + String[] lines = { "package toto;", "//this is a comment", "int i = 4; //new comment" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(2, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES, 2, 2)); + assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE)); + } + + @Test + public void testGetCCommentLines() { + String[] lines = { "package toto;", " int a = 4; /*this is a comment", "new line of comment", "end of comment */ int b = 4;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(3, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES, 2, 2)); + assertEquals(3, source.getMeasure(Metric.LINES_OF_CODE)); + } + + @Test + public void testGetAdjacentCCommentBlocks() { + String[] lines = { "/*first comment*//*second ", " * + \"Ver.", "comment*/" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(3, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(3, source.getMeasure(Metric.LINES)); + } + + @Test + public void testGetLinesOfCode() { + String[] lines = { "package toto;", " ", "import java.util.*;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(0, source.getMeasure(Metric.LINES_OF_CODE, 2, 2)); + } + + @Test + public void testGetCommentedCodeOutLines() { + String[] lines = { "", "/*package toto;", "}*/", " ", "import java.util.*;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(0, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(2, source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES)); + } + + @Test + public void testBlankLinesAfterEndOfComment() { + String[] lines = { "/*Comment*/ " }; + Source source = new Source(lines, codeRecognizer); + assertEquals(0, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES)); + } + + @Test + public void testGetCommentedCodeOutLinesIntoJavadoc() { + String[] lines = { "/**package toto;", "}*/", " ", "import java.util.*;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(2, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(0, source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES)); + } + + @Test + public void testGetBlankCommentLines() { + String[] lines = { "/**", "*/", "import java.util.*;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(3, source.getMeasure(Metric.LINES)); + assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(0, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(2, source.getMeasure(Metric.COMMENT_BLANK_LINES)); + } + + @Test + public void testGetNoSonarTagLines() { + String[] lines = { "import java.util.*;", "//NOSONAR comment", }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(1, source.getNoSonarTagLines().size()); + } + + @Test + public void testGetBlankLinesFromTo() { + String[] lines = { "package toto;", "", "import java.util.*", " " }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 1, 3)); + assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 3, 4)); + assertEquals(2, source.getMeasure(Metric.BLANK_LINES, 1, 4)); + } + + @Test + public void endWithEmptyLine() { + String[] lines = { "package toto;", "" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.BLANK_LINES)); + assertEquals(2, source.getMeasure(Metric.LINES)); + } + + @Test(expected = IllegalStateException.class) + public void testGetBlankLinesFromToWithOutOfBoundIndex() { + String[] lines = { "package toto;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 1, 3)); + } + + @Test + public void testConstructorWithReader() { + Source source = new Source(new StringReader("package toto; \nimport java.util.*;"), codeRecognizer); + assertEquals(2, source.getMeasure(Metric.LINES)); + assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE)); + } + + @Test + public void nativeGWTCodeRecognition() { + String[] lines = { "/*-{", "// JavaScript code", "return this.nextSibling;", "}-*/;" }; + Source source = new Source(lines, codeRecognizer); + assertEquals(4, source.getMeasure(Metric.LINES)); + assertEquals(3, source.getMeasure(Metric.LINES_OF_CODE)); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES)); + } + + @Test + public void testSingleLineCommentWithDoubleDash() { + String[] lines = { "import java.util.*;", "--NOSONAR", }; + Source source = new Source(new StringArrayReader(lines), codeRecognizer, "--"); + assertEquals(1, source.getMeasure(Metric.COMMENT_LINES)); + assertEquals(1, source.getNoSonarTagLines().size()); + } +} diff --git a/sonar-squid/src/test/java/org/sonar/squid/text/StringArrayReaderTest.java b/sonar-squid/src/test/java/org/sonar/squid/text/StringArrayReaderTest.java new file mode 100644 index 00000000000..b07d05accf6 --- /dev/null +++ b/sonar-squid/src/test/java/org/sonar/squid/text/StringArrayReaderTest.java @@ -0,0 +1,184 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.squid.text; + +import org.junit.Test; +import org.sonar.squid.text.StringArrayReader.EndOfLineDelimiter; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class StringArrayReaderTest { + + @Test + public void read() throws IOException { + String[] lines = { "import java.util.*;", "//NOSONAR comment", }; + StringArrayReader reader = new StringArrayReader(lines); + assertEquals('i', reader.read()); + assertEquals('m', reader.read()); + } + + @Test + public void testLFEndOfLineDelimiter() throws IOException { + String[] lines = { ";", ";", }; + StringArrayReader reader = new StringArrayReader(lines, EndOfLineDelimiter.LF); + assertEquals(';', reader.read()); + assertEquals('\n', reader.read()); + assertEquals(';', reader.read()); + } + + @Test + public void testCREndOfLineDelimiter() throws IOException { + String[] lines = { ";", ";", }; + StringArrayReader reader = new StringArrayReader(lines, EndOfLineDelimiter.CR); + assertEquals(';', reader.read()); + assertEquals('\r', reader.read()); + assertEquals(';', reader.read()); + } + + @Test + public void testCRPlusLFEndOfLineDelimiter() throws IOException { + String[] lines = { ";", ";", }; + StringArrayReader reader = new StringArrayReader(lines, EndOfLineDelimiter.CR_PLUS_LF); + assertEquals(';', reader.read()); + assertEquals('\r', reader.read()); + assertEquals('\n', reader.read()); + assertEquals(';', reader.read()); + } + + @Test + public void ready() throws IOException { + String[] lines = { ";", "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + assertTrue(reader.ready()); + } + + @Test + public void markSupported() throws IOException { + String[] lines = {}; + StringArrayReader reader = new StringArrayReader(lines); + assertTrue(reader.markSupported()); + } + + @Test + public void mark() throws IOException { + String[] lines = { ";", "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + reader.read(new char[4], 0, 4); + reader.mark(4); + reader.read(new char[2], 0, 2); + reader.reset(); + assertEquals('N', reader.read()); + assertEquals('O', reader.read()); + } + + @Test(expected = IOException.class) + public void close() throws IOException { + String[] lines = { ";", "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + assertTrue(reader.ready()); + reader.close(); + reader.ready(); + } + + @Test + public void readEndOfArray() throws IOException { + String[] lines = { ";" }; + StringArrayReader reader = new StringArrayReader(lines); + assertEquals(';', reader.read()); + assertEquals(-1, reader.read()); + } + + @Test + public void readMultipleCharacters() throws IOException { + String[] lines = { ";", "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + char[] chars = new char[4]; + assertEquals(4, reader.read(chars, 0, 4)); + assertEquals(";\n//", new String(chars)); + } + + @Test + public void readMultipleCharactersTillEndOfArray() throws IOException { + String[] lines = { ";", "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + char[] chars = new char[11]; + assertEquals(11, reader.read(chars, 0, 11)); + assertEquals(";\n//NOSONAR", new String(chars)); + } + + @Test + public void readEmptyArray() throws IOException { + String[] lines = {}; + StringArrayReader reader = new StringArrayReader(lines); + char[] cbuf = new char[10000]; + assertEquals(-1, reader.read(cbuf, 0, 10000)); + } + + @Test + public void readMultipleCharactersWithEmptyLineAtEnd() throws IOException { + String[] lines = { ";", "//NOSONAR", "", "" }; + StringArrayReader reader = new StringArrayReader(lines); + char[] cbuf = new char[10000]; + assertEquals(13, reader.read(cbuf, 0, 10000)); + assertEquals(";\n//NOSONAR\n\n", new String(cbuf, 0, 13)); + } + + @Test + public void readOneCharacter() throws IOException { + String[] lines = { ";", "//NOSONAR" }; + StringArrayReader reader = new StringArrayReader(lines); + char[] chars = new char[1]; + assertEquals(1, reader.read(chars, 0, 1)); + assertEquals(";", new String(chars)); + } + + @Test + public void readBlankLines() throws IOException { + String[] lines = { "", "", "" }; + StringArrayReader reader = new StringArrayReader(lines); + assertEquals('\n', reader.read()); + assertEquals('\n', reader.read()); + assertEquals(-1, reader.read()); + } + + @Test + public void skip() throws IOException { + String[] lines = { "//NOSONAR", }; + StringArrayReader reader = new StringArrayReader(lines); + reader.skip(2); + assertEquals('N', reader.read()); + } + + @Test + public void readEOF() throws IOException { + String[] emptyLines = {}; + StringArrayReader reader = new StringArrayReader(emptyLines); + assertEquals(-1, reader.read()); + + String[] lines = { "a" }; + reader = new StringArrayReader(lines); + assertEquals('a', reader.read()); + assertEquals(-1, reader.read()); + } + +} |