summaryrefslogtreecommitdiffstats
path: root/plugins/sonar-squid-java-plugin
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-12-13 17:27:40 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-12-13 17:28:36 +0400
commit2ad595f71d67e104768d34d5ee0118820681cb25 (patch)
treebb16d67b50728ece319c07154fcb30058b3c9245 /plugins/sonar-squid-java-plugin
parentc8eb33221e59566dfe6ad0a690f453794db98359 (diff)
downloadsonarqube-2ad595f71d67e104768d34d5ee0118820681cb25.tar.gz
sonarqube-2ad595f71d67e104768d34d5ee0118820681cb25.zip
Add utility class to simplify assertions in tests for checks
And update tests to use this new class.
Diffstat (limited to 'plugins/sonar-squid-java-plugin')
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/CheckMessages.java96
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/BreakCheckTest.java20
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ContinueCheckTest.java12
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/UndocumentedApiCheckTest.java24
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/ArchitectureCheckTest.java30
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/CallToDeprecatedMethodCheckTest.java12
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedPrivateMethodCheckTest.java31
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedProtectedMethodCheckTest.java18
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java17
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java12
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java17
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java18
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java13
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/NoSonarCheckTest.java14
14 files changed, 183 insertions, 151 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/CheckMessages.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/CheckMessages.java
new file mode 100644
index 00000000000..326d0690301
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/CheckMessages.java
@@ -0,0 +1,96 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * 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.java;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.commons.lang.ObjectUtils;
+import org.sonar.squid.api.CheckMessage;
+import org.sonar.squid.api.SourceFile;
+
+import com.google.common.collect.Ordering;
+
+/**
+ * Utility class to simplify assertions in tests for checks.
+ * It can be used as following:
+ * <pre>
+ * CheckMessages = new CheckMessages(SourceFile);
+ * checkMessages.assertNext().atLine(1).withMessage("Message").withCost(1.0);
+ * checkMessages.assertNoMore();
+ * </pre>
+ * TODO Godin: I think that this class can be reused by consumers of sonar-squid, thus can be moved to test-library for sonar-squid.
+ */
+public class CheckMessages {
+
+ private final List<CheckMessage> findings;
+ private final Iterator<CheckMessage> findingsIterator;
+
+ public CheckMessages(SourceFile file) {
+ findings = CHECK_MESSAGE_ORDERING.immutableSortedCopy(file.getCheckMessages());
+ findingsIterator = findings.iterator();
+ }
+
+ private static final Ordering<CheckMessage> CHECK_MESSAGE_ORDERING = new Ordering<CheckMessage>() {
+ public int compare(CheckMessage o1, CheckMessage o2) {
+ return ObjectUtils.compare(o1.getLine(), o2.getLine());
+ }
+ };
+
+ public Next assertNext() {
+ assertTrue("There is no more violations", findingsIterator.hasNext());
+ return new Next(findingsIterator.next());
+ }
+
+ public void assertNoMore() {
+ assertFalse(findingsIterator.hasNext());
+ }
+
+ public static final class Next {
+ private final CheckMessage checkMessage;
+
+ private Next(CheckMessage checkMessage) {
+ this.checkMessage = checkMessage;
+ }
+
+ public Next atLine(Integer expected) {
+ assertThat(checkMessage.getLine(), is(expected));
+ return this;
+ }
+
+ public Next withMessage(String expected) {
+ assertThat(checkMessage.getText(Locale.getDefault()), equalTo(expected));
+ return this;
+ }
+
+ public Next withCost(double expected) {
+ assertThat(checkMessage.getCost(), is(expected));
+ return this;
+ }
+ }
+
+}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/BreakCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/BreakCheckTest.java
index 9ee98d5530e..8fed774a2cf 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/BreakCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/BreakCheckTest.java
@@ -20,23 +20,20 @@
package org.sonar.java.ast.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import java.io.File;
+import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.InputFileUtils;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
-import java.io.File;
-import java.util.Collection;
-
public class BreakCheckTest {
private Squid squid;
@@ -54,16 +51,15 @@ public class BreakCheckTest {
@Test
public void testAvoidUsageOfBreakOutsideSwitch() {
- SourceFile file = (SourceFile) squid.search("org/apache/commons/collections/map/LRUMap.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(244));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("org/apache/commons/collections/map/LRUMap.java"));
+ checkMessages.assertNext().atLine(244);
+ checkMessages.assertNoMore();
}
@Test
public void testAlowUsageOfBreakInsideSwitch() {
- SourceFile file = (SourceFile) squid.search("org/apache/commons/collections/map/Flat3Map.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("org/apache/commons/collections/map/Flat3Map.java"));
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ContinueCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ContinueCheckTest.java
index 09bd0b5afac..c4f4d543e8a 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ContinueCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/ContinueCheckTest.java
@@ -20,16 +20,13 @@
package org.sonar.java.ast.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
public class ContinueCheckTest {
@@ -46,10 +43,9 @@ public class ContinueCheckTest {
@Test
public void testAvoidUsageOfContinue() {
- SourceFile file = (SourceFile) squid.search("org/apache/commons/collections/ExtendedProperties.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(566));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("org/apache/commons/collections/ExtendedProperties.java"));
+ checkMessages.assertNext().atLine(566);
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/UndocumentedApiCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/UndocumentedApiCheckTest.java
index e83b3ce77c3..a680aa7ac74 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/UndocumentedApiCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/UndocumentedApiCheckTest.java
@@ -23,23 +23,17 @@ package org.sonar.java.ast.check;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.java.squid.SquidScanner;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
-import com.google.common.collect.Lists;
-
public class UndocumentedApiCheckTest {
private Squid squid;
@@ -57,18 +51,12 @@ public class UndocumentedApiCheckTest {
public void testUndocumentedApi() {
SourceFile file = (SourceFile) squid.search("UndocumentedApi.java");
- List<CheckMessage> messages = Lists.newArrayList(file.getCheckMessages());
- Collections.sort(messages, new Comparator<CheckMessage>() {
- public int compare(CheckMessage o1, CheckMessage o2) {
- return o1.getLine() - o2.getLine();
- }
- });
-
assertThat(file.getInt(Metric.PUBLIC_API) - file.getInt(Metric.PUBLIC_DOC_API), is(3));
- assertThat(file.getCheckMessages().size(), is(3));
- assertThat(messages.get(0).getLine(), is(10));
- assertThat(messages.get(1).getLine(), is(14));
- assertThat(messages.get(2).getLine(), is(17));
+ CheckMessages checkMessages = new CheckMessages(file);
+ checkMessages.assertNext().atLine(10);
+ checkMessages.assertNext().atLine(14);
+ checkMessages.assertNext().atLine(17);
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/ArchitectureCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/ArchitectureCheckTest.java
index 4233312cb48..686ef815ec7 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/ArchitectureCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/ArchitectureCheckTest.java
@@ -20,17 +20,14 @@
package org.sonar.java.bytecode.check;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
public class ArchitectureCheckTest {
private Squid squid;
@@ -39,37 +36,36 @@ public class ArchitectureCheckTest {
public void testDependencyCheckOneErrorMessage() {
check("", "java.**.Pattern");
- SourceFile file = (SourceFile) squid.search("ArchitectureCheckOneErrorMessage.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getDefaultMessage(), is("ArchitectureCheckOneErrorMessage must not use java/util/regex/Pattern"));
- assertThat(message.getLine(), is(6));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ArchitectureCheckOneErrorMessage.java"));
+ checkMessages.assertNext().atLine(6).withMessage("ArchitectureCheckOneErrorMessage must not use java/util/regex/Pattern");
+ checkMessages.assertNoMore();
}
@Test
public void testDependencyCheckDateForbidden() {
check("", "**.Date");
- SourceFile file = (SourceFile) squid.search("ArchitectureCheckDateForbidden.java");
- assertThat(file.getCheckMessages().size(), is(2));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ArchitectureCheckDateForbidden.java"));
+ checkMessages.assertNext().atLine(7);
+ checkMessages.assertNext().atLine(9);
+ checkMessages.assertNoMore();
}
@Test
public void testDependencyCheckToSqlFromUI() {
check("*UI", "java.sql.*");
- SourceFile file = (SourceFile) squid.search("ArchitectureCheckToSqlFromUI.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(4));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ArchitectureCheckToSqlFromUI.java"));
+ checkMessages.assertNext().atLine(4);
+ checkMessages.assertNoMore();
}
@Test
public void testDependencyCheckOKFromClassesToClasses() {
check("*SA", "java.sql.*");
- SourceFile file = (SourceFile) squid.search("ArchitectureCheckToSqlFromUI.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ArchitectureCheckToSqlFromUI.java"));
+ checkMessages.assertNoMore();
}
private void check(String fromClasses, String toClasses) {
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/CallToDeprecatedMethodCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/CallToDeprecatedMethodCheckTest.java
index e6ed3f42bb9..8680d3a0e13 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/CallToDeprecatedMethodCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/CallToDeprecatedMethodCheckTest.java
@@ -19,17 +19,14 @@
*/
package org.sonar.java.bytecode.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.BeforeClass;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
public class CallToDeprecatedMethodCheckTest {
@@ -46,9 +43,8 @@ public class CallToDeprecatedMethodCheckTest {
@Test
public void testCallToDeprecatedMethod() {
- SourceFile file = (SourceFile) squid.search("CallToDeprecatedMethod.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(6));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CallToDeprecatedMethod.java"));
+ checkMessages.assertNext().atLine(6);
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedPrivateMethodCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedPrivateMethodCheckTest.java
index e1da2df5c0d..0ce6d70b5da 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedPrivateMethodCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedPrivateMethodCheckTest.java
@@ -19,17 +19,14 @@
*/
package org.sonar.java.bytecode.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.BeforeClass;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
public class UnusedPrivateMethodCheckTest {
@@ -46,30 +43,22 @@ public class UnusedPrivateMethodCheckTest {
@Test
public void testDetectUnusedPrivateMethod() {
- SourceFile file = (SourceFile) squid.search("UnusedPrivateMethod.java");
-
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(33));
- assertThat(message.getDefaultMessage(), is("Private method 'unusedPrivateMethod(...)' is never used."));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedPrivateMethod.java"));
+ checkMessages.assertNext().atLine(33).withMessage("Private method 'unusedPrivateMethod(...)' is never used.");
+ checkMessages.assertNoMore();
}
@Test
public void testDetectUnusedGenericPrivateMethod() {
- SourceFile file = (SourceFile) squid.search("UnusedGenericPrivateMethod.java");
-
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(7));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedGenericPrivateMethod.java"));
+ checkMessages.assertNext().atLine(7);
+ checkMessages.assertNoMore();
}
@Test
public void testDetectUnusedPrivateConstructor() {
- SourceFile file = (SourceFile) squid.search("UnusedPrivateConstructor.java");
-
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(10));
- assertThat(message.getDefaultMessage(), is("Private method '<init>(...)' is never used."));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedPrivateConstructor.java"));
+ checkMessages.assertNext().atLine(10).withMessage("Private method '<init>(...)' is never used.");
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedProtectedMethodCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedProtectedMethodCheckTest.java
index b4573367a2f..58d5b60b753 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedProtectedMethodCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/UnusedProtectedMethodCheckTest.java
@@ -19,17 +19,14 @@
*/
package org.sonar.java.bytecode.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.BeforeClass;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
public class UnusedProtectedMethodCheckTest {
@@ -46,17 +43,14 @@ public class UnusedProtectedMethodCheckTest {
@Test
public void testDetectUnusedProtectedMethod() {
- SourceFile file = (SourceFile) squid.search("UnusedProtectedMethod.java");
-
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(17));
- assertThat(message.getDefaultMessage(), is("Protected method 'unusedProtectedMethod(...)' is never used."));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedProtectedMethod.java"));
+ checkMessages.assertNext().atLine(17).withMessage("Protected method 'unusedProtectedMethod(...)' is never used.");
+ checkMessages.assertNoMore();
}
@Test
public void testIgnoreUnusedProtectedMethodFromAbstractClass() {
- SourceFile file = (SourceFile) squid.search("UnusedProtectedMethodFromAbstractClass.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedProtectedMethodFromAbstractClass.java"));
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
index 65cbb288f8d..c8c90c8efbd 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
@@ -20,17 +20,14 @@
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.java.squid.SquidScanner;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
@@ -53,17 +50,15 @@ public class ClassComplexityCheckTest {
@Test
public void testComplexityExceedsThreshold() {
- SourceFile file = (SourceFile) squid.search("ComplexBranches.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(3));
- assertThat(message.getCost(), is(3.0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ComplexBranches.java"));
+ checkMessages.assertNext().atLine(3).withCost(3.0);
+ checkMessages.assertNoMore();
}
@Test
public void testComplexityNotExceedsThreshold() {
- SourceFile file = (SourceFile) squid.search("NoBranches.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("NoBranches.java"));
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java
index aee6c74b360..a479d3a409c 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java
@@ -19,11 +19,9 @@
*/
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.*;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
@@ -49,8 +47,12 @@ public class CommentedOutCodeLineCheckTest {
@Test
public void testDetection() {
- SourceFile file = (SourceFile) squid.search("CommentedCode.java");
- assertThat(file.getCheckMessages().size(), is(4));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CommentedCode.java"));
+ checkMessages.assertNext().atLine(4);
+ checkMessages.assertNext().atLine(17);
+ checkMessages.assertNext().atLine(18);
+ checkMessages.assertNext().atLine(19);
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java
index b74ffd8f49a..3cdbc37ea64 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/DITCheckTest.java
@@ -20,18 +20,15 @@
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.BeforeClass;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.java.squid.SquidScanner;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
public class DITCheckTest {
@@ -51,17 +48,15 @@ public class DITCheckTest {
@Test
public void testDepthOfInheritanceGreaterThanMaximum() {
- SourceFile file = (SourceFile) squid.search("UnusedProtectedMethod.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(7));
- assertThat(message.getCost(), is(1.0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("UnusedProtectedMethod.java"));
+ checkMessages.assertNext().atLine(7).withCost(1.0);
+ checkMessages.assertNoMore();
}
@Test
public void testDepthOfInheritanceLowerThanMaximum() {
- SourceFile file = (SourceFile) squid.search("Job.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("Job.java"));
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java
index 358dc657b29..7820119d245 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/EmptyFileCheckTest.java
@@ -20,18 +20,14 @@
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.java.squid.SquidScanner;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
@@ -51,16 +47,14 @@ public class EmptyFileCheckTest {
@Test
public void shouldDetectEmptyFiles() {
- SourceFile file = (SourceFile) squid.search("org/foo/CommentedOutFile.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), nullValue());
- assertThat(message.getDefaultMessage(), is("This Java file is empty"));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("org/foo/CommentedOutFile.java"));
+ checkMessages.assertNext().atLine(null).withMessage("This Java file is empty");
+ checkMessages.assertNoMore();
}
@Test
public void shouldNotLogOnCorrectFiles() {
- SourceFile file = (SourceFile) squid.search("CommentedCode.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("CommentedCode.java"));
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java
index f44ec36ef5f..65c736232d1 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java
@@ -20,17 +20,14 @@
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.java.squid.SquidScanner;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
@@ -51,10 +48,8 @@ public class MethodComplexityCheckTest {
@Test
public void testMethodComplexityExceedsThreshold() {
- SourceFile file = (SourceFile) squid.search("ComplexBranches.java");
- assertThat(file.getCheckMessages().size(), is(1));
- CheckMessage message = file.getCheckMessages().iterator().next();
- assertThat(message.getLine(), is(10));
- assertThat(message.getCost(), is(2.0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("ComplexBranches.java"));
+ checkMessages.assertNext().atLine(10).withCost(2.0);
+ checkMessages.assertNoMore();
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/NoSonarCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/NoSonarCheckTest.java
index 0b36b69ba55..3bef8f23249 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/NoSonarCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/NoSonarCheckTest.java
@@ -19,11 +19,9 @@
*/
package org.sonar.java.squid.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.java.CheckMessages;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
@@ -50,14 +48,16 @@ public class NoSonarCheckTest {
@Test
public void testNoSonarTagDetection() {
- SourceFile file = (SourceFile) squid.search("FileWithNOSONARTags.java");
- assertThat(file.getCheckMessages().size(), is(2));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("FileWithNOSONARTags.java"));
+ checkMessages.assertNext().atLine(5);
+ checkMessages.assertNext().atLine(10);
+ checkMessages.assertNoMore();
}
@Test
public void testNoSonarTagDetectionWhenNoTag() {
- SourceFile file = (SourceFile) squid.search("FileWithoutNOSONARTags.java");
- assertThat(file.getCheckMessages().size(), is(0));
+ CheckMessages checkMessages = new CheckMessages((SourceFile) squid.search("FileWithoutNOSONARTags.java"));
+ checkMessages.assertNoMore();
}
}