*/
package org.sonar.java.ast;
-import java.util.List;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.io.File;
+import java.util.*;
+import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.resources.InputFile;
import org.sonar.java.ast.visitor.JavaAstVisitor;
import org.sonar.java.recognizer.JavaFootprint;
import org.sonar.java.squid.JavaSquidConfiguration;
private static JavaAstVisitor[] visitors;
private static int[] allTokens;
private static CodeRecognizer codeRecognizer;
+ private static Map<java.io.File,InputFile> inputFilesByPath = Maps.newHashMap();
static void setASTVisitors(List<JavaAstVisitor> visitors) {
CheckstyleSquidBridge.visitors = visitors.toArray(new JavaAstVisitor[visitors.size()]);
return allTokens; //NOSONAR returning directly the array is not a security flaw here
}
+ public static InputFile getInputFile(File path) {
+ return inputFilesByPath.get(path);
+ }
+
+ public static void setInputFiles(Collection<InputFile> inputFiles) {
+ inputFilesByPath.clear();
+ for (InputFile inputFile : inputFiles) {
+ inputFilesByPath.put(inputFile.getFile(), inputFile);
+ }
+ }
+
@Override
public void beginTree(DetailAST ast) {
try {
for (JavaAstVisitor visitor : visitors) {
visitor.setFileContents(getFileContents());
visitor.setSource(source);
+ visitor.setInputFile(getInputFile(new java.io.File(getFileContents().getFilename())));
visitor.visitFile(ast);
}
} catch (RuntimeException e) {
*/
package org.sonar.java.ast;
+import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.InputFileUtils;
import org.sonar.java.ast.visitor.*;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.api.AnalysisException;
this.project = project;
}
- /**
- * Create and execute the Checkstyle engine.
- *
- * @param files
- * collection of files to analyse. This list shouldn't contain and directory.
- * @param charset
- * the default charset to use to read files
- */
- private void launchCheckstyleEngine(Collection<File> files, Charset charset) {
- Checker c = createChecker(charset);
- ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
- Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
- try {
- c.setClassloader(getClass().getClassLoader());
- c.setModuleClassLoader(getClass().getClassLoader());
- c.process(new ArrayList<File>(files));
- c.destroy();
- } finally {
- Thread.currentThread().setContextClassLoader(initialClassLoader);
- }
- }
-
- /**
- * Creates the Checkstyle Checker object.
- *
- * @return a nice new fresh Checkstyle Checker
- */
private Checker createChecker(Charset charset) {
InputStream checkstyleConfig = null;
try {
}
public JavaAstScanner scanDirectory(File javaSourceDirectory) {
- List<File> files = new ArrayList<File>(FileUtils.listFiles(javaSourceDirectory, FileFilterUtils.fileFileFilter(), FileFilterUtils
- .directoryFileFilter()));
- return scanFiles(files);
+ List<InputFile> inputFiles = Lists.newArrayList();
+ Collection<File> files = FileUtils.listFiles(javaSourceDirectory, FileFilterUtils.fileFileFilter(), FileFilterUtils.directoryFileFilter());
+ for (File file : files) {
+ inputFiles.add(InputFileUtils.create(javaSourceDirectory, file));
+ }
+ return scanFiles(inputFiles);
}
- public JavaAstScanner scanFile(File javaFile) {
+ public JavaAstScanner scanFile(InputFile javaFile) {
return scanFiles(Arrays.asList(javaFile));
}
- public JavaAstScanner scanFiles(Collection<File> javaFiles) {
+ public JavaAstScanner scanFiles(Collection<InputFile> inputFiles) {
if (LOG.isDebugEnabled()) {
LOG.debug("----- Java sources analyzed by Squid:");
- for (File javaFile : javaFiles) {
- LOG.debug(javaFile.getAbsolutePath());
+ for (InputFile inputFile : inputFiles) {
+ LOG.debug(inputFile.toString());
}
LOG.debug("-----");
}
}
CheckstyleSquidBridge.setASTVisitors(getVisitors());
CheckstyleSquidBridge.setSquidConfiguration(conf);
- launchCheckstyleEngine(javaFiles, conf.getCharset());
+ CheckstyleSquidBridge.setInputFiles(inputFiles);
+ launchCheckstyle(InputFileUtils.toFiles(inputFiles), conf.getCharset());
return this;
}
+ private void launchCheckstyle(Collection<File> files, Charset charset) {
+ Checker c = createChecker(charset);
+ ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+ try {
+ c.setClassloader(getClass().getClassLoader());
+ c.setModuleClassLoader(getClass().getClassLoader());
+ c.process(Lists.<File>newArrayList(files));
+ c.destroy();
+ } finally {
+ Thread.currentThread().setContextClassLoader(initialClassLoader);
+ }
+ }
+
+
@Override
public Collection<Class<? extends JavaAstVisitor>> getVisitorClasses() {
- List<Class<? extends JavaAstVisitor>> visitorClasses = new ArrayList<Class<? extends JavaAstVisitor>>();
+ List<Class<? extends JavaAstVisitor>> visitorClasses = Lists.newArrayList();
visitorClasses.add(PackageVisitor.class);
visitorClasses.add(FileVisitor.class);
visitorClasses.add(ClassVisitor.class);
*/
package org.sonar.java.ast.visitor;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Stack;
-
-import org.sonar.squid.text.Source;
+import com.google.common.collect.Lists;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.FileContents;
+import org.sonar.api.resources.InputFile;
import org.sonar.squid.api.CodeVisitor;
import org.sonar.squid.api.SourceClass;
import org.sonar.squid.api.SourceCode;
import org.sonar.squid.api.SourcePackage;
+import org.sonar.squid.text.Source;
-import com.puppycrawl.tools.checkstyle.api.DetailAST;
-import com.puppycrawl.tools.checkstyle.api.FileContents;
+import java.util.List;
+import java.util.Stack;
public abstract class JavaAstVisitor implements CodeVisitor {
private FileContents fileContents;
+ private InputFile inputFile;
+
private Source source;
- private static final List<Integer> emptyWantedTokens = new ArrayList<Integer>();
+ private static final List<Integer> emptyWantedTokens = Lists.newArrayList();
public final void setFileContents(FileContents fileContents) {
this.fileContents = fileContents;
this.source = source;
}
+ public final InputFile getInputFile() {
+ return inputFile;
+ }
+
+ public final void setInputFile(InputFile inputFile) {
+ this.inputFile = inputFile;
+ }
+
final Source getSource() {
return source;
}
*/
package org.sonar.java.ast.visitor;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.FullIdent;
+import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.resources.InputFileUtils;
+import org.sonar.squid.api.AnalysisException;
import org.sonar.squid.api.SourceCode;
import org.sonar.squid.api.SourcePackage;
import org.sonar.squid.indexer.SquidIndex;
import org.sonar.squid.measures.Metric;
-import com.puppycrawl.tools.checkstyle.api.DetailAST;
-import com.puppycrawl.tools.checkstyle.api.FullIdent;
-import com.puppycrawl.tools.checkstyle.api.TokenTypes;
-
public class PackageVisitor extends JavaAstVisitor {
+ private static final String ROOT_PACKAGE = "";
+
private SquidIndex indexer;
public PackageVisitor(SquidIndex indexer) {
@Override
public void visitFile(DetailAST ast) {
+ SourceCode packageRes = null;
+
if (ast == null) {
- // ast can be null for empty classes
- return;
+ // ast can be null for empty files (all the file is commented-out)
+ packageRes = guessPackage();
+ } else {
+ packageRes = createSourcePackage(ast);
}
- SourceCode packageRes = createSourcePackage(ast);
if (peekSourceCode().hasChild(packageRes)) {
packageRes = indexer.search(packageRes.getKey());
}
addSourceCode(packageRes);
}
+ private SourcePackage guessPackage() {
+ String directory = InputFileUtils.getRelativeDirectory(getInputFile());
+ return new SourcePackage(directory);
+ }
+
@Override
public void leaveFile(DetailAST ast) {
- if (ast == null) {
- // ast can be null for empty classes
- return;
- }
popSourceCode();
}
private SourcePackage createSourcePackage(DetailAST ast) {
- SourcePackage packageRes;
- if (ast.getType() != TokenTypes.PACKAGE_DEF) {
- packageRes = new SourcePackage("");
- } else {
+ String key = ROOT_PACKAGE;
+ if (ast.getType() == TokenTypes.PACKAGE_DEF) {
String packageName = FullIdent.createFullIdent(ast.getLastChild().getPreviousSibling()).getText();
- packageRes = new SourcePackage(packageName.replace('.', '/'));
+ key = packageName.replace('.', '/');
+ }
+ checkPhysicalDirectory(key);
+ return new SourcePackage(key);
+ }
+
+ /**
+ * Check that package declaration is consistent with the physical location of Java file.
+ * It aims to detect two cases :
+ * - wrong package declaration : "package org.foo" stored in the directory "org/bar"
+ * - source directory badly configured : src/ instead of src/main/java/
+ *
+ * @since 2.8
+ */
+ private void checkPhysicalDirectory(String key) {
+ String relativeDirectory = InputFileUtils.getRelativeDirectory(getInputFile());
+ // both relativeDirectory and key use slash '/' as separator
+ if (!StringUtils.equals(relativeDirectory, key)) {
+ String packageName = StringUtils.replace(key, "/", ".");
+ if (StringUtils.contains(relativeDirectory, key) || StringUtils.contains(key, relativeDirectory)) {
+ throw new AnalysisException(String.format("The source directory does not correspond to the package declaration %s", packageName));
+ }
+ throw new AnalysisException(String.format("The package declaration %s does not correspond to the file path %s",
+ packageName, getInputFile().getRelativePath()));
}
- return packageRes;
}
}
\ No newline at end of file
import org.sonar.api.batch.SensorContext;
import org.sonar.api.checks.CheckFactory;
import org.sonar.api.checks.NoSonarFilter;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.TimeProfiler;
return conf;
}
- protected SquidExecutor(Squid squid) {
- this.squid = squid;
- }
-
- public void scan(Collection<File> sourceFiles, Collection<File> bytecodeFilesOrDirectories) {
+ public void scan(Collection<InputFile> sourceFiles, Collection<File> bytecodeFilesOrDirectories) {
for (Object checker : checkFactory.getChecks()) {
squid.registerVisitor((CodeVisitor) checker);
}
}
- void scanSources(Collection<File> sourceFiles) {
+ void scanSources(Collection<InputFile> sourceFiles) {
if (sourceFiles != null && !sourceFiles.isEmpty()) {
TimeProfiler profiler = new TimeProfiler(getClass()).start("Java AST scan");
JavaAstScanner sourceScanner = squid.register(JavaAstScanner.class);
}
}
- private List<File> getMainSourceFiles(Project project) {
- return project.getFileSystem().getJavaSourceFiles();
+ private List<InputFile> getMainSourceFiles(Project project) {
+ return project.getFileSystem().mainFiles(Java.KEY);
}
private Collection<File> getMainBytecodeFiles(Project project) {
package org.sonar.java.ast;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
@Test
public void testMacRomanEncoding() {
squid = new Squid(new JavaSquidConfiguration(false, Charset.forName("MacRoman")));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/MacRomanEncoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/MacRomanEncoding.java"));
SourceProject prj = squid.aggregate();
assertEquals(4, prj.getInt(Metric.METHODS));
}
@Test(expected = AnalysisException.class)
public void testCP1252EncodingWithWrongDefined() {
squid = new Squid(new JavaSquidConfiguration(true, Charset.forName("MacRoman")));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/CP1252Encoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/CP1252Encoding.java"));
}
@Test
public void testCheckstyleParsingBug() {
// see
// http://sourceforge.net/tracker/?func=detail&atid=397078&aid=1667137&group_id=29721
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/parsingErrors/CheckstyleBug.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/parsingErrors/CheckstyleBug.java"));
SourceProject prj = squid.aggregate();
assertEquals(0, prj.getInt(Metric.CLASSES));
}
@Test
public void testEmptyClassWithComment() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/emptyFiles/ClassWithOnlyComment.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/emptyFiles/ClassWithOnlyComment.java"));
SourceProject prj = squid.aggregate();
assertEquals(0, prj.getInt(Metric.CLASSES));
- assertEquals(0, prj.getInt(Metric.PACKAGES));
+ assertEquals(1, prj.getInt(Metric.PACKAGES));
+ assertEquals(1, prj.getInt(Metric.FILES));
assertEquals(1, prj.getInt(Metric.COMMENT_LINES));
+
+ assertNotNull(squid.search("ClassWithOnlyComment.java"));//file
+ assertNull(squid.search("ClassWithOnlyComment"));//class
}
@Test
public void testEmptyFileWithBlankLines() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/emptyFiles/EmptyFileWithBlankLines.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/emptyFiles/EmptyFileWithBlankLines.java"));
SourceProject prj = squid.aggregate();
assertEquals(0, prj.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01);
}
@Test
public void testClassWithPackageImportsComment() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/emptyFiles/ClassWithPackageImportsComment.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/emptyFiles", "foo/ClassWithPackageImportsComment.java"));
SourceProject prj = squid.aggregate();
assertEquals(0, prj.getInt(Metric.CLASSES));
assertEquals(1, prj.getInt(Metric.PACKAGES));
}
System.setProperty("file.encoding", "MacRoman");
squid = new Squid(new JavaSquidConfiguration(false));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/MacRomanEncoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/MacRomanEncoding.java"));
macRoman = squid.aggregate();
System.setProperty("file.encoding", "CP1252");
squid = new Squid(new JavaSquidConfiguration(false));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/CP1252Encoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/CP1252Encoding.java"));
cp1252 = squid.aggregate();
} finally {
System.setProperty("file.encoding", currentEncoding);
@Test
public void testCP1252Encoding() {
squid = new Squid(new JavaSquidConfiguration(false, Charset.forName("CP1252")));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/CP1252Encoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/CP1252Encoding.java"));
SourceProject prj = squid.aggregate();
assertEquals(4, prj.getInt(Metric.METHODS));
}
@Test
public void testUTF8Encoding() {
squid = new Squid(new JavaSquidConfiguration(false, Charset.forName(CharEncoding.UTF_8)));
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/encoding/Utf8Encoding.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/Utf8Encoding.java"));
SourceProject prj = squid.aggregate();
assertEquals(4, prj.getInt(Metric.METHODS));
}
@Test
public void testInterfaceWithAnnotations() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/annotations/InterfaceWithAnnotation.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/annotations/InterfaceWithAnnotation.java"));
SourceProject prj = squid.aggregate();
- assertEquals(12, prj.getInt(Metric.LINES));
- assertEquals(7, prj.getInt(Metric.LINES_OF_CODE));
+ assertEquals(11, prj.getInt(Metric.LINES));
+ assertEquals(6, prj.getInt(Metric.LINES_OF_CODE));
assertEquals(0, prj.getInt(Metric.STATEMENTS));
assertEquals(2, prj.getInt(Metric.METHODS));
assertEquals(2, prj.getInt(Metric.COMPLEXITY));
@Test
public void testClassesWithGenerics() {
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/special_cases/generics"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/special_cases/generics"));
SourceProject prj = squid.aggregate();
assertEquals(2, prj.getInt(Metric.FILES));
assertEquals(3, prj.getInt(Metric.METHODS));
@Test
public void testPackageInfo() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/package-info.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/packageInfo", "org/apache/cxf/jaxrs/ext/logging/package-info.java"));
SourceProject prj = squid.aggregate();
assertEquals(1, prj.getInt(Metric.FILES));
assertEquals(4, prj.getInt(Metric.LINES_OF_CODE));
*/
package org.sonar.java.ast;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.InputFileUtils;
+
import java.io.File;
public final class SquidTestUtils {
+ /**
+ * Assumes that the Java file has no package. It's declared in the root directory.
+ */
+ public static InputFile getInputFile(String relativePath) {
+ File file = getFile(relativePath);
+ return InputFileUtils.create(file.getParentFile(), file);
+ }
+
+ public static InputFile getInputFile(String basedir, String relativePath) {
+ File file = getFile(basedir + "/" + relativePath);
+ return InputFileUtils.create(getFile(basedir), file);
+ }
+
public static File getFile(String relativePath) {
return new File("test-resources" + relativePath);
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
+import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.resources.InputFileUtils;
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;
public void setUp() {
squid = new Squid(new JavaSquidConfiguration());
squid.registerVisitor(BreakCheck.class);
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/commons-collections-3.2.1/src/org/apache/commons/collections/map"));
+
+ File basedir = SquidTestUtils.getFile("/commons-collections-3.2.1/src");
+ Collection<File> files = FileUtils.listFiles(new File(basedir, "org/apache/commons/collections/map"), new String[]{"java"}, true);
+
+ squid.register(JavaAstScanner.class).scanFiles(InputFileUtils.create(basedir, files));
}
@Test
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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;
squid = new Squid(new JavaSquidConfiguration());
squid.registerVisitor(ContinueCheck.class);
squid.register(JavaAstScanner.class).scanFile(
- getFile("/commons-collections-3.2.1/src/org/apache/commons/collections/ExtendedProperties.java"));
+ SquidTestUtils.getInputFile("/commons-collections-3.2.1/src", "org/apache/commons/collections/ExtendedProperties.java"));
}
@Test
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import java.util.Collections;
import java.util.Comparator;
import org.junit.Before;
import org.junit.Test;
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;
public void setUp() {
squid = new Squid(new JavaSquidConfiguration());
squid.registerVisitor(UndocumentedApiCheck.class);
- squid.register(JavaAstScanner.class).scanFile(getFile("/rules/UndocumentedApi.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/rules/UndocumentedApi.java"));
squid.decorateSourceCodeTreeWith(Metric.values());
squid.register(SquidScanner.class).scan();
}
import org.junit.Test;
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.SourceCode;
import org.sonar.squid.measures.Metric;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AccessorVisitorTest {
@Test
public void analyzePureJavaBean() {
squid = new Squid(new JavaSquidConfiguration(true));
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/accessors/PureJavaBean.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/accessors/PureJavaBean.java"));
SourceCode res = squid.aggregate();
- assertEquals(55, res.getInt(Metric.LINES_OF_CODE));
- assertEquals(95, res.getInt(Metric.LINES));
+ assertEquals(54, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(94, res.getInt(Metric.LINES));
assertEquals(6, res.getInt(Metric.ACCESSORS));
assertEquals(10, res.getInt(Metric.METHODS));
}
@Test
public void considerAccessorAsMethod() {
squid = new Squid(new JavaSquidConfiguration(false));
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/accessors/JavaBeanWithApiDoc.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/accessors/JavaBeanWithApiDoc.java"));
SourceCode res = squid.aggregate();
- assertEquals(11, res.getInt(Metric.LINES_OF_CODE));
- assertEquals(31, res.getInt(Metric.LINES));
+ assertEquals(10, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(30, res.getInt(Metric.LINES));
assertEquals(2, res.getInt(Metric.METHODS));
assertEquals(0, res.getInt(Metric.ACCESSORS));
assertEquals(4, res.getInt(Metric.PUBLIC_API));
@Test
public void analyseVarAccessorsImpactOnOtherMeasures() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/accessors/JavaBeanWithApiDoc.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/accessors/JavaBeanWithApiDoc.java"));
SourceCode res = squid.aggregate();
- assertEquals(11, res.getInt(Metric.LINES_OF_CODE));
- assertEquals(31, res.getInt(Metric.LINES));
+ assertEquals(10, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(30, res.getInt(Metric.LINES));
assertEquals(1, res.getInt(Metric.METHODS));
assertEquals(1, res.getInt(Metric.ACCESSORS));
assertEquals(3, res.getInt(Metric.PUBLIC_API));
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertNotNull;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
@Test
public void testCreateInnerSquidClass() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/AnonymousInnerClass.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/AnonymousInnerClass.java"));
squid.aggregate();
assertNotNull(squid.search("AnonymousInnerClass$1"));
assertNotNull(squid.search("AnonymousInnerClass$2"));
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseTest002() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/loc/Test002.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/loc/Test002.java"));
assertEquals(5, squid.aggregate().getInt(Metric.BLANK_LINES));
}
@Test
public void analyseTest001() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/loc/Test001.java"));
- assertEquals(3, squid.aggregate().getInt(Metric.BLANK_LINES));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/loc/Test001.java"));
+ assertEquals(4, squid.aggregate().getInt(Metric.BLANK_LINES));
- SourceCode classSource = squid.search("test/Something");
+ SourceCode classSource = squid.search("Something");
assertEquals(2, classSource.getInt(Metric.BLANK_LINES));
}
@Test
public void analyseClassWithHeaderAndBlankLines() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithHeaderAndBlankLines.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithHeaderAndBlankLines.java"));
SourceCode sources = squid.aggregate();
- assertEquals(4, sources.getInt(Metric.LINES_OF_CODE));
- assertEquals(3, sources.getInt(Metric.BLANK_LINES));
+ assertEquals(3, sources.getInt(Metric.LINES_OF_CODE));
+ assertEquals(4, sources.getInt(Metric.BLANK_LINES));
assertEquals(6, sources.getInt(Metric.COMMENT_BLANK_LINES));
}
}
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void testNoBranches() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/branches/NoBranches.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/branches/NoBranches.java"));
SourceCode res = squid.aggregate();
assertEquals(0, res.getInt(Metric.BRANCHES));
}
@Test
public void testSimpleBranches() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/branches/SimpleBranches.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/branches/SimpleBranches.java"));
SourceCode res = squid.aggregate();
assertEquals(8, res.getInt(Metric.BRANCHES));
}
@Test
public void testComplexBranches() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/branches/ComplexBranches.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/branches/ComplexBranches.java"));
SourceCode res = squid.aggregate();
assertEquals(6, res.getInt(Metric.BRANCHES));
}
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
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.SourceClass;
@Test
public void analyseTest003() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/Test003.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/Test003.java"));
SourceCode project = squid.aggregate();
SourceCode defaultPackage = project.getFirstChild();
SourceCode file = defaultPackage.getFirstChild();
@Test
public void analyseClassCounterEnum() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/ClassCounterEnum.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/ClassCounterEnum.java"));
SourceCode project = squid.aggregate();
SourceCode defaultPackage = project.getFirstChild();
assertEquals(1, defaultPackage.getInt(Metric.CLASSES));
@Test
public void analyseAnnotationDefinition() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/annotations/AnnotationDefinition.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/annotations/AnnotationDefinition.java"));
SourceCode project = squid.aggregate();
SourceCode annotation = project.getFirstChild();
assertEquals(1, annotation.getInt(Metric.CLASSES));
- assertNotNull(squid.search("org/sonar/plugins/api/AnnotationDefinition"));
+ assertNotNull(squid.search("AnnotationDefinition"));
}
@Test
public void analyseInterface() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/Interface.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/Interface.java"));
SourceCode project = squid.aggregate();
assertEquals(1, project.getInt(Metric.INTERFACES));
}
@Test
public void analyseAbstractClass() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/AbstractClass.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/AbstractClass.java"));
SourceCode project = squid.aggregate();
assertEquals(1, project.getInt(Metric.ABSTRACT_CLASSES));
}
@Test
public void testStartAtLine() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/AbstractClass.java"));
- SourceCode classTest = squid.search("org/sonar/AbstractClass");
- assertEquals(4, classTest.getStartAtLine());
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/AbstractClass.java"));
+ SourceCode classTest = squid.search("AbstractClass");
+ assertEquals(2, classTest.getStartAtLine());
}
@Test
public void analysePrivateInnerClass() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/classes/InnerClassTests.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/classes/InnerClassTests.java"));
SourceCode project = squid.aggregate();
SourceCode defaultPackage = project.getFirstChild();
SourceCode defaultClassFile = defaultPackage.getFirstChild();
@Test
public void detectSuppressWarningsAnnotation() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/rules/ClassWithSuppressWarningsAnnotation.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/rules/ClassWithSuppressWarningsAnnotation.java"));
SourceClass sourceClass = (SourceClass) squid.search("ClassWithSuppressWarningsAnnotation");
assertThat(sourceClass.isSuppressWarnings(), is(true));
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
+import org.sonar.api.resources.InputFile;
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.SourceCode;
assertEquals(2, method.getInt(Metric.COMMENT_BLANK_LINES));
}
+ @Test
+ @Ignore("TODO")
+ public void testCommentedOutFile() {
+ SourceProject res = scan("/metrics/commentedCode", "org/foo/CommentedOutFile.java");
+ }
+
+ @Test
+ @Ignore("TODO")
+ public void shouldGuessPackageOfcommentedOutFile() {
+ SourceProject res = scan("/metrics/commentedCode", "org/foo/CommentedOutFile.java");
+ }
+
@Test
public void testNoSonarTagDetection() {
scan("/rules/FileWithNOSONARTags.java");
assertEquals(0, project.getInt(Metric.COMMENTED_OUT_CODE_LINES));
}
- private SourceProject scan(String path) {
- squid.register(JavaAstScanner.class).scanFile(getFile(path));
+ private SourceProject scan(String filePath) {
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile(filePath));
+ return squid.aggregate();
+ }
+
+ private SourceProject scan(String basedir, String filePath) {
+ InputFile inputFile = SquidTestUtils.getInputFile(basedir, filePath);
+ squid.register(JavaAstScanner.class).scanFile(inputFile);
return squid.aggregate();
}
}
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void testNoBranches() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/branches/NoBranches.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/branches/NoBranches.java"));
SourceCode res = squid.aggregate();
assertEquals(3, res.getInt(Metric.COMPLEXITY));
}
@Test
public void testSimpleBranches() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/branches/SimpleBranches.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/branches/SimpleBranches.java"));
SourceCode res = squid.aggregate();
assertEquals(15, res.getInt(Metric.COMPLEXITY));
- SourceCode simpleSwitch = squid.search("Car#simpleSwitch()V");
+ SourceCode simpleSwitch = squid.search("SimpleBranches#simpleSwitch()V");
assertEquals(3, simpleSwitch.getInt(Metric.COMPLEXITY));
}
@Test
public void testInstanceAndStaticInitBlocks() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/complexity/InstanceAndStaticInitBlocks.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/complexity/InstanceAndStaticInitBlocks.java"));
SourceCode res = squid.aggregate();
assertEquals(2, res.getInt(Metric.COMPLEXITY));
}
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Test;
import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
@Test
public void testEndAtLineForMethod() {
Squid squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithStaticMethods.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithStaticMethods.java"));
assertEquals(17, squid.search("ClassWithStaticMethods#doJob2()V").getEndAtLine());
}
@Test
public void testEndAtLineForClass() {
Squid squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithStaticMethods.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithStaticMethods.java"));
assertEquals(30, squid.search("ClassWithStaticMethods").getEndAtLine());
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
+import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.resources.InputFile;
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.SourceCode;
@Test
public void analyseTest003() {
- List<File> files = new ArrayList<File>();
- files.add(getFile("/metrics/loc/Test002.java"));
- files.add(getFile("/metrics/classes/Test003.java"));
+ List<InputFile> files = Lists.newArrayList();
+ files.add(SquidTestUtils.getInputFile("/metrics/loc/Test002.java"));
+ files.add(SquidTestUtils.getInputFile("/metrics/classes/Test003.java"));
squid.register(JavaAstScanner.class).scanFiles(files);
SourceCode project = squid.aggregate();
assertEquals(2, project.getInt(Metric.FILES));
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseHeaderCommentsStandard() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithHeader.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithHeader.java"));
SourceCode res = squid.aggregate();
assertEquals(7, res.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER));
assertEquals(32, res.getInt(Metric.LINES));
- assertEquals(11, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(10, res.getInt(Metric.LINES_OF_CODE));
assertEquals(2, res.getInt(Metric.HEADER_COMMENT_LINES));
}
@Test
public void analyseHeaderCommentsAndNoPackage() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithHeaderAndNoPackage.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithHeaderAndNoPackage.java"));
SourceCode res = squid.aggregate();
assertEquals(7, res.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER));
assertEquals(30, res.getInt(Metric.LINES));
@Test
public void analyseHeaderCommentsAndNoPackageNoImports() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithHeaderAndNoPackageNoImports.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithHeaderAndNoPackageNoImports.java"));
SourceCode res = squid.aggregate();
assertEquals(3, res.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER));
assertEquals(23, res.getInt(Metric.LINES));
@Test
public void analyseJavadocHeaderAndPackage() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithPackageAndJavadocHeader.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/", "foo/ClassWithPackageAndJavadocHeader.java"));
SourceCode res = squid.aggregate();
assertEquals(2, res.getInt(Metric.HEADER_COMMENT_LINES));
assertEquals(3, res.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER));
@Test
public void analyseCCommentWithoutHeader() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithoutHeaderAndWithCComment.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithoutHeaderAndWithCComment.java"));
SourceCode res = squid.aggregate();
assertEquals(3, res.getInt(Metric.COMMENT_LINES_WITHOUT_HEADER));
assertEquals(0, res.getInt(Metric.HEADER_COMMENT_LINES));
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseTestNcloc() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/ncloc/TestNcloc.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/ncloc/TestNcloc.java"));
SourceCode res = squid.aggregate();
assertEquals(39, res.getInt(Metric.LINES));
assertEquals(9, res.getInt(Metric.BLANK_LINES));
@Test
public void analyseTestNclocWithClassComment() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/ncloc/TestNclocWithClassComment.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/ncloc/TestNclocWithClassComment.java"));
SourceCode res = squid.aggregate();
assertEquals(46, res.getInt(Metric.LINES));
assertEquals(9, res.getInt(Metric.BLANK_LINES));
@Test
public void analyseTestNclocWithHeader() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/ncloc/TestNclocWithHeader.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/ncloc/TestNclocWithHeader.java"));
SourceCode res = squid.aggregate();
assertEquals(59, res.getInt(Metric.LINES));
- assertEquals(11, res.getInt(Metric.BLANK_LINES));
+ assertEquals(12, res.getInt(Metric.BLANK_LINES));
assertEquals(4, res.getInt(Metric.HEADER_COMMENT_LINES));
assertEquals(12, res.getInt(Metric.COMMENT_BLANK_LINES));
- assertEquals(27, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(26, res.getInt(Metric.LINES_OF_CODE));
}
}
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseTest001() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/loc/Test001.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/loc/Test001.java"));
SourceCode res = squid.aggregate();
assertEquals(25, res.getInt(Metric.LINES));
- SourceCode classSource = squid.search("test/Something");
+ SourceCode classSource = squid.search("Something");
assertEquals(14, classSource.getInt(Metric.LINES));
- SourceCode methodSource = squid.search("test/Something#run()V");
+ SourceCode methodSource = squid.search("Something#run()V");
assertEquals(3, methodSource.getInt(Metric.LINES));
}
@Test
public void analyseTest002() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/loc/Test002.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/loc/Test002.java"));
SourceCode res = squid.aggregate();
assertEquals(19, res.getInt(Metric.LINES));
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseClassWithStaticMethods() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithStaticMethods.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithStaticMethods.java"));
SourceCode prj = squid.decorateSourceCodeTreeWith(Metric.values());
assertEquals(3, prj.getInt(Metric.METHODS));
assertEquals(8, prj.getInt(Metric.COMPLEXITY));
@Test
public void methodWithAnonymousInnerClass() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/MethodWithAnonymousInnerClass.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/MethodWithAnonymousInnerClass.java"));
SourceCode prj = squid.decorateSourceCodeTreeWith(Metric.values());
assertEquals(4, prj.getInt(Metric.METHODS));
assertEquals(4, squid.search(new QueryByType(SourceMethod.class)).size());
@Test
public void testStartAtLine() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithStaticMethods.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithStaticMethods.java"));
SourceCode doJob2Method = squid.search("ClassWithStaticMethods#doJob1()V");
assertEquals(3, doJob2Method.getStartAtLine());
}
@Test
public void testMethodSignature() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithDifferentMethodSignatures.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithDifferentMethodSignatures.java"));
assertNotNull(squid.search("ClassWithDifferentMethodSignatures#<init>()V"));
assertNotNull(squid.search("ClassWithDifferentMethodSignatures#<init>(LList;)V"));
assertNotNull(squid.search("ClassWithDifferentMethodSignatures#method()V"));
@Test
public void testConstructorsMetric() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/methods/ClassWithDifferentMethodSignatures.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/methods/ClassWithDifferentMethodSignatures.java"));
SourceCode source = squid.decorateSourceCodeTreeWith(Metric.values());
assertEquals(2, source.getInt(Metric.CONSTRUCTORS));
}
@Test
public void detectSuppressWarningsAnnotation() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/rules/ClassWithSuppressWarningsAnnotation.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/rules/ClassWithSuppressWarningsAnnotation.java"));
assertThat(getMethod("ClassWithSuppressWarningsAnnotation#fullyQualifiedName()V").isSuppressWarnings(), is(true));
assertThat(getMethod("ClassWithSuppressWarningsAnnotation#singleValue()V").isSuppressWarnings(), is(true));
--- /dev/null
+/*
+ * 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.ast.visitor;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+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.AnalysisException;
+import org.sonar.squid.api.SourceProject;
+import org.sonar.squid.measures.Metric;
+
+import static org.junit.Assert.*;
+
+public class PackageVisitorTest {
+
+ private Squid squid;
+
+ @Before
+ public void setup() {
+ squid = new Squid(new JavaSquidConfiguration());
+ }
+
+ @Test(expected = AnalysisException.class)
+ public void shouldFailIfPackageDifferentThanPhysicalDirectory() {
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/wrongPackages/", "org/foo/WrongPackage.java"));
+ squid.aggregate();
+ }
+
+ @Test(expected = AnalysisException.class)
+ public void shouldFailIfBadSourceDirectory() {
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/wrongPackages/org", "foo/GoodPackage.java"));
+ squid.aggregate();
+ }
+
+ @Test
+ public void shouldGuessPackageWhenCommentedOutFile() {
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/wrongPackages", "org/foo/CommentedOutFile.java"));
+ SourceProject project = squid.aggregate();
+
+ assertNull(squid.search("CommentedOutFile.java"));
+ assertNotNull(squid.search("org/foo/CommentedOutFile.java"));
+ assertNotNull(squid.search("org/foo"));
+ assertEquals(1, project.getInt(Metric.PACKAGES));
+ }
+}
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void analyseClassWithCommentsOnLineOfCode() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithCommentsOnLineOfCode.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithCommentsOnLineOfCode.java"));
SourceCode res = squid.aggregate();
- assertEquals(8, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(7, res.getInt(Metric.LINES_OF_CODE));
assertEquals(4, res.getInt(Metric.COMMENT_LINES));
assertEquals(2, res.getInt(Metric.PUBLIC_API));
}
@Test
public void analyseVars() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithVars.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithVars.java"));
SourceCode res = squid.aggregate();
- assertEquals(16, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(15, res.getInt(Metric.LINES_OF_CODE));
assertEquals(59, res.getInt(Metric.LINES));
- assertEquals(9, res.getInt(Metric.BLANK_LINES));
+ assertEquals(10, res.getInt(Metric.BLANK_LINES));
assertEquals(21, res.getInt(Metric.COMMENT_BLANK_LINES));
assertEquals(5, res.getInt(Metric.PUBLIC_API));
assertEquals(1, res.getInt(Metric.HEADER_COMMENT_LINES));
@Test
public void analyseConstants() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/Constants.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/Constants.java"));
SourceCode res = squid.aggregate();
- assertEquals(10, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(9, res.getInt(Metric.LINES_OF_CODE));
assertEquals(76, res.getInt(Metric.LINES));
- assertEquals(10, res.getInt(Metric.BLANK_LINES));
+ assertEquals(11, res.getInt(Metric.BLANK_LINES));
assertEquals(21, res.getInt(Metric.COMMENT_BLANK_LINES));
assertEquals(1, res.getInt(Metric.PUBLIC_API));
assertEquals(15, res.getInt(Metric.HEADER_COMMENT_LINES));
@Test
public void analyseApiDocCounter() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/ClassWithComments.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/ClassWithComments.java"));
SourceCode res = squid.aggregate();
assertEquals(7, res.getInt(Metric.PUBLIC_API));
assertEquals(4, res.getInt(Metric.PUBLIC_DOC_API));
assertEquals(66, res.getInt(Metric.LINES));
- assertEquals(19, res.getInt(Metric.LINES_OF_CODE));
- assertEquals(0.45, res.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01);
+ assertEquals(18, res.getInt(Metric.LINES_OF_CODE));
+ assertEquals(0.47, res.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01);
}
@Test
public void analyseJavaDocCounterOnAnnotation() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/special_cases/annotations/AnnotationDefinition.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/special_cases/annotations/AnnotationDefinition.java"));
SourceCode res = squid.aggregate();
assertEquals(3, res.getInt(Metric.PUBLIC_API));
assertEquals(2, res.getInt(Metric.PUBLIC_DOC_API));
- assertEquals(19, res.getInt(Metric.LINES));
- assertEquals(0.33, res.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01);
+ assertEquals(18, res.getInt(Metric.LINES));
+ assertEquals(0.36, res.getDouble(Metric.COMMENT_LINES_DENSITY), 0.01);
}
@Test
public void analyseInterfaceComments() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/InterfaceWithComments.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/InterfaceWithComments.java"));
SourceCode res = squid.aggregate();
assertEquals(6, res.getInt(Metric.PUBLIC_API));
assertEquals(2, res.getInt(Metric.PUBLIC_DOC_API));
@Test
public void excludeMethodWithOverrideAnnotation() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/MethodsWithOverrideAnnotation.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/MethodsWithOverrideAnnotation.java"));
SourceCode res = squid.aggregate();
assertEquals(2, res.getInt(Metric.PUBLIC_API));
assertEquals(0, res.getInt(Metric.PUBLIC_DOC_API));
@Test
public void excludeEmptyConstructor() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/javadoc/EmptyConstructor.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/javadoc/EmptyConstructor.java"));
SourceCode res = squid.aggregate();
assertEquals(3, res.getInt(Metric.PUBLIC_API));
assertEquals(0, res.getInt(Metric.PUBLIC_DOC_API));
package org.sonar.java.ast.visitor;
import static org.junit.Assert.assertEquals;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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.SourceCode;
@Test
public void testNoStatements() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/statements/NoStatements.java"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/statements/NoStatements.java"));
SourceCode res = squid.aggregate();
assertEquals(12, res.getInt(Metric.STATEMENTS));
SourceCode simpleIf = squid.search("Car#simpleIf(LString;)V");
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.util.TraceClassVisitor;
import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.asm.AsmResource;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
@BeforeClass
public static void setup() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/src"));
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/bin"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/src"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/bin"));
squid.decorateSourceCodeTreeWith(Metric.values());
tag = squid.search("tags/Tag");
tagFile = squid.search("tags/Tag.java");
@Test
@Ignore
- public void testClassWithEnum() throws FileNotFoundException, IOException {
+ public void testClassWithEnum() throws IOException {
SourceFile classWithEnum = (SourceFile) squid.search("specialCases/ClassWithEnum.java");
SourceClass myEnum = (SourceClass) squid.search("specialCases/ClassWithEnum$MyEnum");
- ClassReader asmReader = new ClassReader(new FileInputStream(getFile("/bytecode/bin/specialCases/ClassWithEnum$MyEnum.class")));
+ ClassReader asmReader = new ClassReader(new FileInputStream(SquidTestUtils.getFile("/bytecode/bin/specialCases/ClassWithEnum$MyEnum.class")));
TraceClassVisitor classVisitor = new TraceClassVisitor(new PrintWriter(System.out));
asmReader.accept(classVisitor, 0);
classVisitor.print(new PrintWriter(System.out));
package org.sonar.java.bytecode;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import java.io.File;
import java.util.Collections;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class ClassworldsClassLoaderTest {
@Test
public void createFromDirectory() throws ClassNotFoundException {
- File dir = getFile("/bytecode/bin/");
+ File dir = SquidTestUtils.getFile("/bytecode/bin/");
ClassLoader classloader = ClassworldsClassLoader.create(dir);
assertThat(classloader.loadClass("tags.TagName"), not(nullValue()));
@Test
public void createFromJar() throws ClassNotFoundException {
- File jar = getFile("/bytecode/lib/hello.jar");
+ File jar = SquidTestUtils.getFile("/bytecode/lib/hello.jar");
ClassLoader classloader = ClassworldsClassLoader.create(jar);
assertThat(classloader.loadClass("org.sonar.tests.Hello"), not(nullValue()));
assertThat(classloader.getResource("org/sonar/tests/Hello.class"), not(nullValue()));
@Test
public void unknownJarIsIgnored() throws ClassNotFoundException {
- File jar = getFile("/bytecode/lib/unknown.jar");
+ File jar = SquidTestUtils.getFile("/bytecode/lib/unknown.jar");
ClassLoader classloader = ClassworldsClassLoader.create(jar);
assertThat(classloader.getResource("org/sonar/tests/Hello.class"), nullValue());
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Test;
import org.sonar.java.ast.JavaAstScanner;
-import org.sonar.java.bytecode.BytecodeScanner;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.check.UnusedProtectedMethodCheck;
import org.sonar.java.squid.JavaSquidConfiguration;
import org.sonar.squid.Squid;
@Test
public void testLinkingRunMethodVirtualMethodToItsImplementation() {
Squid squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/virtualMethodsLinker/src"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/virtualMethodsLinker/src"));
squid.registerVisitor(UnusedProtectedMethodCheck.class);
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/virtualMethodsLinker/bin"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/virtualMethodsLinker/bin"));
squid.decorateSourceCodeTreeWith(Metric.values());
SourceFile file = (SourceFile) squid.search("Animal.java");
package org.sonar.java.bytecode.asm;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.ClassworldsClassLoader;
import org.sonar.java.bytecode.asm.AsmClassProvider.DETAIL_LEVEL;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AsmClassProviderImplTest {
@Test
public void testPersonalClassLoader() {
- asmClassProviderImpl = new AsmClassProviderImpl(ClassworldsClassLoader.create(getFile("/bytecode/bin/")));
+ asmClassProviderImpl = new AsmClassProviderImpl(ClassworldsClassLoader.create(SquidTestUtils.getFile("/bytecode/bin/")));
assertEquals(DETAIL_LEVEL.STRUCTURE_AND_CALLS, asmClassProviderImpl.getClass("tags/Line", DETAIL_LEVEL.STRUCTURE_AND_CALLS).getDetailLevel());
}
}
package org.sonar.java.bytecode.asm;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.ClassworldsClassLoader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AsmClassVisitorTest {
- private static AsmClassProvider asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(getFile("/bytecode/bin/")));
+ private static AsmClassProvider asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(SquidTestUtils.getFile("/bytecode/bin/")));
@Test
public void testVisit() {
package org.sonar.java.bytecode.asm;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.ClassworldsClassLoader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AsmFieldVisitorTest {
@Test
public void testVisitStringField() {
- AsmClassProviderImpl classProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(getFile("/bytecode/bin/")));
+ AsmClassProviderImpl classProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(SquidTestUtils.getFile("/bytecode/bin/")));
AsmClass fileClass = classProvider.getClass("tags/SourceFile");
assertEquals(5, fileClass.getFields().size());
AsmField field = fileClass.getField("path");
package org.sonar.java.bytecode.asm;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.ClassworldsClassLoader;
import static org.junit.Assert.*;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AsmMethodVisitorTest {
- private AsmClassProvider asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(getFile("/bytecode/bin/")));
+ private AsmClassProvider asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(SquidTestUtils.getFile("/bytecode/bin/")));
@Test
public void testVisitFieldInsn() {
import org.junit.Test;
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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class ArchitectureCheckTest {
check.setToClasses(toClasses);
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/architecture/src"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/architecture/src"));
squid.registerVisitor(check);
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/architecture/bin"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/architecture/bin"));
}
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.BeforeClass;
import org.junit.Test;
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;
@BeforeClass
public static void setup() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/callToDeprecatedMethod/src"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/callToDeprecatedMethod/src"));
squid.registerVisitor(CallToDeprecatedMethodCheck.class);
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/callToDeprecatedMethod/bin"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/callToDeprecatedMethod/bin"));
}
@Test
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.BeforeClass;
import org.junit.Test;
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;
@BeforeClass
public static void setup() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedPrivateMethod/src"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedPrivateMethod/src"));
squid.registerVisitor(UnusedPrivateMethodCheck.class);
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedPrivateMethod/bin"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedPrivateMethod/bin"));
}
@Test
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.BeforeClass;
import org.junit.Test;
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;
@BeforeClass
public static void setup() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/src"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedProtectedMethod/src"));
squid.registerVisitor(UnusedProtectedMethodCheck.class);
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/bin"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedProtectedMethod/bin"));
}
@Test
import org.junit.BeforeClass;
import org.junit.Test;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.bytecode.ClassworldsClassLoader;
import org.sonar.java.bytecode.asm.AsmClass;
import org.sonar.java.bytecode.asm.AsmClassProvider;
import org.sonar.java.bytecode.asm.AsmMethod;
import static org.junit.Assert.*;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
public class AccessorVisitorTest {
@BeforeClass
public static void init() {
- asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(getFile("/bytecode/bin/")));
+ asmClassProvider = new AsmClassProviderImpl(ClassworldsClassLoader.create(SquidTestUtils.getFile("/bytecode/bin/")));
javaBean = asmClassProvider.getClass("properties/JavaBean");
accessorVisitor.visitClass(javaBean);
for (AsmMethod method : javaBean.getMethods()) {
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.BeforeClass;
import org.junit.Test;
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;
JavaSquidConfiguration conf = new JavaSquidConfiguration();
conf.addFieldToExcludeFromLcom4Calculation("LOG");
squid = new Squid(conf);
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/lcom4/src"));
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/lcom4/bin"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/lcom4/src"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/lcom4/bin"));
squid.decorateSourceCodeTreeWith(Metric.values());
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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;
check.setMax(5);
squid.registerVisitor(check);
JavaAstScanner scanner = squid.register(JavaAstScanner.class);
- scanner.scanFile(getFile("/metrics/branches/NoBranches.java"));
- scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java"));
+ scanner.scanFile(SquidTestUtils.getInputFile("/metrics/branches/NoBranches.java"));
+ scanner.scanFile(SquidTestUtils.getInputFile("/metrics/branches/ComplexBranches.java"));
squid.decorateSourceCodeTreeWith(Metric.values());
squid.register(SquidScanner.class).scan();
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.BeforeClass;
import org.junit.Test;
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;
DITCheck check = new DITCheck();
check.setMax(1);
squid.registerVisitor(check);
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/src"));
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/bin"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedProtectedMethod/src"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/bytecode/unusedProtectedMethod/bin"));
squid.register(SquidScanner.class).scan();
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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;
check.setMax(5);
squid.registerVisitor(check);
JavaAstScanner scanner = squid.register(JavaAstScanner.class);
- scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java"));
+ scanner.scanFile(SquidTestUtils.getInputFile("/metrics/branches/ComplexBranches.java"));
squid.decorateSourceCodeTreeWith(Metric.values());
squid.register(SquidScanner.class).scan();
}
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
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;
NoSonarCheck check = new NoSonarCheck();
squid.registerVisitor(check);
JavaAstScanner scanner = squid.register(JavaAstScanner.class);
- scanner.scanFile(getFile("/rules/FileWithNOSONARTags.java"));
- scanner.scanFile(getFile("/rules/FileWithoutNOSONARTags.java"));
+ scanner.scanFile(SquidTestUtils.getInputFile("/rules/FileWithNOSONARTags.java"));
+ scanner.scanFile(SquidTestUtils.getInputFile("/rules/FileWithoutNOSONARTags.java"));
squid.decorateSourceCodeTreeWith(Metric.values());
squid.register(SquidScanner.class).scan();
}
import org.sonar.api.checks.AnnotationCheckFactory;
import org.sonar.api.checks.CheckFactory;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.squid.Squid;
@Test
public void doNotScanBytecodeIfNoSources() throws IOException, URISyntaxException {
SquidExecutor executor = new SquidExecutor(true, "LOG, logger", createCheckFactory(), Charset.defaultCharset());
- executor.scan(Collections.<File> emptyList(), Arrays.asList(SquidTestUtils.getStrutsCoreJar()));
+ executor.scan(Collections.<InputFile> emptyList(), Arrays.asList(SquidTestUtils.getStrutsCoreJar()));
assertThat(executor.isSourceScanned(), is(false));
assertThat(executor.isBytecodeScanned(), is(false));
package org.sonar.plugins.squid;
import org.apache.commons.io.FileUtils;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.InputFileUtils;
import org.sonar.api.utils.ZipUtils;
import java.io.File;
/**
* See http://svn.apache.org/repos/asf/struts/struts1/tags/STRUTS_1_3_9/core
*/
- public static Collection<File> getStrutsCoreSources() throws IOException, URISyntaxException {
+ public static Collection<InputFile> getStrutsCoreSources() throws IOException, URISyntaxException {
File sourceDir = new File("target/struts-core-1.3.9-sources");
if (!sourceDir.exists() || sourceDir.list().length==0) {
FileUtils.forceMkdir(sourceDir);
ZipUtils.unzip(new File(SquidTestUtils.class.getResource("/struts-core-1.3.9-sources.jar").toURI()), sourceDir);
}
- return FileUtils.listFiles(sourceDir, new String[]{"java"}, true);
+ Collection<File> javaFiles = FileUtils.listFiles(sourceDir, new String[]{"java"}, true);
+
+ return InputFileUtils.create(sourceDir, javaFiles);
}
/**
package org.sonar.squid;
import static org.junit.Assert.assertNotNull;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import org.junit.Before;
import org.junit.Test;
import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.ast.SquidTestUtils;
import org.sonar.java.squid.JavaSquidConfiguration;
public class SquidTest {
@Test
public void searchSingleResult() {
- squid.register(JavaAstScanner.class).scanFile(getFile("/metrics/accessors/JavaBeanWithApiDoc.java"));
- assertNotNull(squid.search("java/bean/test"));
- assertNotNull(squid.search("java/bean/test/JavaBeanWithApiDoc.java"));
- assertNotNull(squid.search("java/bean/test/JavaBeanWithAPIDoc"));
+ squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/packages/", "org/foo/SimpleBean.java"));
+ assertNotNull(squid.search("org/foo"));
+ assertNotNull(squid.search("org/foo/SimpleBean.java"));
+ assertNotNull(squid.search("org/foo/SimpleBean"));
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.sonar.java.ast.SquidTestUtils.getFile;
import java.util.Collection;
import java.util.Map;
import org.junit.Test;
import org.sonar.graph.IncrementalCyclesAndFESSolver;
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.api.SourceCode;
@BeforeClass
public static void setup() {
squid = new Squid(new JavaSquidConfiguration());
- squid.register(JavaAstScanner.class).scanDirectory(getFile("/commons-collections-3.2.1/src"));
- squid.register(BytecodeScanner.class).scanDirectory(getFile("/commons-collections-3.2.1/bin"));
+ squid.register(JavaAstScanner.class).scanDirectory(SquidTestUtils.getFile("/commons-collections-3.2.1/src"));
+ squid.register(BytecodeScanner.class).scanDirectory(SquidTestUtils.getFile("/commons-collections-3.2.1/bin"));
project = squid.aggregate();
}
-package java.bean.test;
public class JavaBeanWithAPIDoc {
-package java.bean.test;
public class PureJavaBean {
-public class Car {
+public class NoBranches {
private int n;
private String s;
- public AClass(int n, String s) {
+ public NoBranches(int n, String s) {
this.n = n;
this.s = s;
}
import java.util.ArrayList;
-public class Car {
+public class SimpleBranches {
private int n;
private String s;
- public AClass(int n, String s) {
+ public SimpleBranches(int n, String s) {
this.n = n;
this.s = s;
}
-package org.sonar;
-
public abstract class AbstractClass {
//comment
--- /dev/null
+//package org.foo;
+//
+//public class CommentedOutFile {
+//
+//}
\ No newline at end of file
/*
* Header
*/
-package test;
+
import nothing;
//single comment 1
-package test;
+
public class ClassWithCommentsOnLineOfCode {
*
* multi lines
*/
-package test;
+
import nothing;
//single comment 1
should no be counted\r
\r
*/\r
-package test;\r
+\r
\r
public interface ClassWithHeaderAndBlankLines {\r
void reset();\r
+++ /dev/null
-/**
- * File header as javadoc
- * should be detected as a header
- *
- */
-package foo;
-
-/**
- * This is a class comment
- */
-public class ClassWithPackageAndJavadocHeader {
-
- public static void doJob1() {
- int i = 3;
- if (i == 4) {
- i++;
- }
- }
-
- /**
- *
- * qsdfqsdf
- */
- public static void doJob2() {
- int i = 3;
- if (i == 4) {
- if (i == 3) {
- i--;
- //mlkqjdsf
- }
- }
- }
- public class Toto{
- public static void doJob2() {
- int i = 3;
- if (i == 4) {
- if (i == 3) {
- i--;
- }
- }
- }
-
- }
-
-
-
-
-
-
-}
/*
* Header
*/
-package test;
+
import nothing;
//single comment 1
-package test;
+
import nothing;
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.struts.chain;
+
/**
/*
* Header
*/
-package test;
+
public class EmptyConstructor {
/*
* Header
*/
-package test;
+
public interface Something {
/*
* Header
*/
-package test;
+
public class MethodsWithOverrideAnnotation {
--- /dev/null
+/**
+ * File header as javadoc
+ * should be detected as a header
+ *
+ */
+package foo;
+
+/**
+ * This is a class comment
+ */
+public class ClassWithPackageAndJavadocHeader {
+
+ public static void doJob1() {
+ int i = 3;
+ if (i == 4) {
+ i++;
+ }
+ }
+
+ /**
+ *
+ * qsdfqsdf
+ */
+ public static void doJob2() {
+ int i = 3;
+ if (i == 4) {
+ if (i == 3) {
+ i--;
+ //mlkqjdsf
+ }
+ }
+ }
+ public class Toto{
+ public static void doJob2() {
+ int i = 3;
+ if (i == 4) {
+ if (i == 3) {
+ i--;
+ }
+ }
+ }
+
+ }
+
+
+
+
+
+
+}
/*
* Header
*/
-package test;
+
import nothing;
//single comment 1
* lines
*/
-package test;
+
/**
* This is a nice comments header
--- /dev/null
+package org.foo;
+
+public class SimpleBean {
+
+ /**
+ * Not to count
+ */
+ private String testVar;
+
+ /**
+ * To count
+ */
+ public String testVar2;
+
+ /**
+ * This api doc should not be counted not its complexity
+ * @return
+ */
+ public String getTest() {
+ return testVar;
+ }
+
+ /**
+ * This api doc should be counted
+ * @return
+ */
+ public String getTest2() {
+ return "test";
+ }
+
+}
\ No newline at end of file
-package org.sonar.plugins.api;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
-package com.toto;
public interface InterfaceWithAnnotation {
+++ /dev/null
-// some comment
-package test;
-
-import java.lang.Runtime;
-import org.apache.commons.*;
-
-// another comment
\ No newline at end of file
--- /dev/null
+// some comment
+package foo;
+
+import java.lang.Runtime;
+import org.apache.commons.*;
+
+// another comment
\ No newline at end of file
+++ /dev/null
-/**\r
- * Licensed to the Apache Software Foundation (ASF) under one\r
- * or more contributor license agreements. See the NOTICE file\r
- * distributed with this work for additional information\r
- * regarding copyright ownership. The ASF licenses this file\r
- * to you under the Apache License, Version 2.0 (the\r
- * "License"); you may not use this file except in compliance\r
- * with the License. You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing,\r
- * software distributed under the License is distributed on an\r
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
- * KIND, either express or implied. See the License for the\r
- * specific language governing permissions and limitations\r
- * under the License.\r
- */\r
-\r
-/**\r
- * JAX-RS specific logging support. Based on <tt>java.util.logging</tt> (JUL)\r
- * with use of different logging frameworks factored out; assumes that client \r
- * with source code logging to other systems, like Log4J, can bridge \r
- * to this implementation applying <a href="www.slf4j.org">SLF4J</a> \r
- * that JAXRS already depends on.\r
- */\r
-@javax.xml.bind.annotation.XmlSchema(xmlns = {\r
- @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://cxf.apache.org/jaxrs/log", prefix = "log")\r
- })\r
-package org.apache.cxf.jaxrs.ext.logging;\r
-\r
--- /dev/null
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * JAX-RS specific logging support. Based on <tt>java.util.logging</tt> (JUL)
+ * with use of different logging frameworks factored out; assumes that client
+ * with source code logging to other systems, like Log4J, can bridge
+ * to this implementation applying <a href="www.slf4j.org">SLF4J</a>
+ * that JAXRS already depends on.
+ */
+@javax.xml.bind.annotation.XmlSchema(xmlns = {
+ @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://cxf.apache.org/jaxrs/log", prefix = "log")
+ })
+package org.apache.cxf.jaxrs.ext.logging;
+
--- /dev/null
+//package org.foo;
+//
+//public class GoodPackage {
+//
+// private int i;
+//
+//}
\ No newline at end of file
--- /dev/null
+package org.foo;
+
+public class GoodPackage {
+
+ private int i;
+
+}
\ No newline at end of file
--- /dev/null
+package com.somewhere;
+
+public class WrongPackage {
+
+ private int i;
+
+}
\ No newline at end of file