2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
21 package org.sonar.java.squid.check;
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertThat;
25 import static org.sonar.java.ast.SquidTestUtils.getFile;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.sonar.java.ast.JavaAstScanner;
30 import org.sonar.java.squid.JavaSquidConfiguration;
31 import org.sonar.java.squid.SquidScanner;
32 import org.sonar.squid.Squid;
33 import org.sonar.squid.api.CheckMessage;
34 import org.sonar.squid.api.SourceFile;
35 import org.sonar.squid.measures.Metric;
37 public class ClassComplexityCheckTest {
43 squid = new Squid(new JavaSquidConfiguration());
44 ClassComplexityCheck check = new ClassComplexityCheck();
45 check.setThreshold(5);
46 squid.registerVisitor(check);
47 JavaAstScanner scanner = squid.register(JavaAstScanner.class);
48 scanner.scanFile(getFile("/metrics/branches/NoBranches.java"));
49 scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java"));
50 squid.decorateSourceCodeTreeWith(Metric.values());
51 squid.register(SquidScanner.class).scan();
55 public void testComplexityExceedsThreshold() {
56 SourceFile file = (SourceFile) squid.search("ComplexBranches.java");
57 assertThat(file.getCheckMessages().size(), is(1));
58 CheckMessage message = file.getCheckMessages().iterator().next();
59 assertThat(message.getLine(), is(3));
63 public void testComplexityNotExceedsThreshold() {
64 SourceFile file = (SourceFile) squid.search("NoBranches.java");
65 assertThat(file.getCheckMessages().size(), is(0));