@Test public void testEncodingWithSystemSetting() { SourceProject macRoman; SourceProject cp1252; String currentEncoding = System.getProperty("file.encoding"); // changing the system encoding can have no effects try { System.setProperty("file.encoding", "UTF-16"); Charset defaultEncoding = Charset.defaultCharset(); if (!defaultEncoding.displayName().equals("UTF-16")) { return; } System.setProperty("file.encoding", "MacRoman"); squid = new Squid(new JavaSquidConfiguration(false)); 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(SquidTestUtils.getInputFile("/special_cases/encoding/CP1252Encoding.java")); cp1252 = squid.aggregate(); } finally { System.setProperty("file.encoding", currentEncoding); } assertEquals(4, macRoman.getInt(Metric.METHODS)); assertEquals(4, cp1252.getInt(Metric.METHODS)); }
@Test public void testMacRomanEncoding() { squid = new Squid(new JavaSquidConfiguration(false, Charset.forName("MacRoman"))); squid .register(JavaAstScanner.class) .scanFile(SquidTestUtils.getInputFile("/special_cases/encoding/MacRomanEncoding.java")); SourceProject prj = squid.aggregate(); assertEquals(4, prj.getInt(Metric.METHODS)); }
@Test public void testClassesWithGenerics() { 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 testEmptyFileWithBlankLines() { 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); }
/** SONAR-1836: bug in Checkstyle 5.2 - 5.4 */ @Test public void testLineCommentAtTheEndOfFile() { squid .register(JavaAstScanner.class) .scanFile( SquidTestUtils.getInputFile( "/special_cases/parsingErrors/LineCommentAtTheEndOfFile.java")); SourceProject prj = squid.aggregate(); assertEquals(1, prj.getInt(Metric.CLASSES)); }
@Test public void testCheckstyleParsingBug() { // see // http://sourceforge.net/tracker/?func=detail&atid=397078&aid=1667137&group_id=29721 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 testScanSimpleFile() { AstScanner<CSharpGrammar> scanner = CSharpAstScanner.create(new CSharpConfiguration(Charset.forName("UTF-8"))); scanner.scanFile(readFile("/metric/simpleFile.cs")); SourceProject project = (SourceProject) scanner.getIndex().search(new QueryByType(SourceProject.class)).iterator().next(); assertThat(project.getInt(CSharpMetric.STATEMENTS), is(16)); }
@Test public void testClassWithPackageImportsComment() { 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)); assertEquals(2, prj.getInt(Metric.COMMENT_LINES)); }
@Test public void testPackageInfo() { 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)); assertEquals(29, prj.getInt(Metric.LINES)); }
@Test public void testEmptyClassWithComment() { squid .register(JavaAstScanner.class) .scanFile( SquidTestUtils.getInputFile("/special_cases/emptyFiles/ClassWithOnlyComment.java")); SourceProject prj = squid.aggregate(); assertEquals(0, prj.getInt(Metric.CLASSES)); 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 testInterfaceWithAnnotations() { squid .register(JavaAstScanner.class) .scanFile( SquidTestUtils.getInputFile("/special_cases/annotations/InterfaceWithAnnotation.java")); SourceProject prj = squid.aggregate(); 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)); }