@Test public void should_store_nosonar_lines_on_inputfile() { DefaultInputFile f = TestInputFileBuilder.create("module1", "myfile.java").build(); new NoSonarFilter().noSonarInFile(f, new HashSet<>(Arrays.asList(1,4))); assertThat(f.hasNoSonarAt(1)).isTrue(); assertThat(f.hasNoSonarAt(2)).isFalse(); assertThat(f.hasNoSonarAt(4)).isTrue(); } }
@Test public void shouldExcludeFileCoverageBasedOnPattern() { DefaultInputFile file = TestInputFileBuilder.create("foo", new File(baseDir, "moduleA"), new File(baseDir, "moduleA/src/org/polop/File.php")) .setProjectBaseDir(baseDir.toPath()) .build(); underTest = new ProjectCoverageAndDuplicationExclusions(mockConfig("moduleA/src/org/polop/*", "")); assertThat(underTest.isExcludedForCoverage(file)).isTrue(); assertThat(underTest.isExcludedForDuplication(file)).isFalse(); }
@Test public void shouldNotExcludeFileDuplicationBasedOnPattern() { DefaultInputFile file = TestInputFileBuilder.create("foo", new File(baseDir, "moduleA"), new File(baseDir, "moduleA/src/org/polop/File.php")) .setProjectBaseDir(baseDir.toPath()) .build(); underTest = new ProjectCoverageAndDuplicationExclusions(mockConfig("", "moduleA/src/org/other/*")); assertThat(underTest.isExcludedForCoverage(file)).isFalse(); assertThat(underTest.isExcludedForDuplication(file)).isFalse(); }
@Before public void setUp() { inputFile = new TestInputFileBuilder("module1", "src/File.java").build(); textPointer = new DefaultTextPointer(5, 2); storage = mock(SensorStorage.class); }
@Test public void shouldNotExcludeFileCoverageBasedOnPattern() { DefaultInputFile file = TestInputFileBuilder.create("foo", new File(baseDir, "moduleA"), new File(baseDir, "moduleA/src/org/polop/File.php")) .setProjectBaseDir(baseDir.toPath()) .build(); underTest = new ProjectCoverageAndDuplicationExclusions(mockConfig("moduleA/src/org/other/*", "")); assertThat(underTest.isExcludedForCoverage(file)).isFalse(); assertThat(underTest.isExcludedForDuplication(file)).isFalse(); }
@Test public void shouldNotExcludeFileBasedOnPattern() { DefaultInputFile file = TestInputFileBuilder.create("foo", new File(baseDir, "moduleA"), new File(baseDir, "moduleA/src/org/polop/File.php")) .setProjectBaseDir(baseDir.toPath()) .build(); coverageExclusions = new ModuleCoverageAndDuplicationExclusions(mockConfig("src/org/other/*", "")); assertThat(coverageExclusions.isExcludedForCoverage(file)).isFalse(); }
@Test public void setContent() throws IOException { DefaultInputFile file = TestInputFileBuilder.create("module", "invalidPath") .setContents("my content") .setCharset(StandardCharsets.UTF_8) .build(); assertThat(file.contents()).isEqualTo("my content"); assertThat(IOUtils.toString(file.inputStream())).isEqualTo("my content"); }
@Test public void should_save_issue() { InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build(); DefaultIssue issue = new DefaultIssue(project).at(new DefaultIssueLocation().on(file)); underTest.store(issue); ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class); verify(moduleIssues).initAndAddIssue(argumentCaptor.capture()); assertThat(argumentCaptor.getValue()).isEqualTo(issue); }
@Test public void testLineHitAndConditions() throws IOException { File coverage = new File(baseDir, "src/foo.xoo.itcoverage"); FileUtils.write(coverage, "1:3:4:2"); InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setModuleBaseDir(baseDir.toPath()).setLanguage("xoo").setLines(10).build(); context.fileSystem().add(inputFile); sensor.execute(context); assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3); assertThat(context.conditions("foo:src/foo.xoo", 1)).isEqualTo(4); assertThat(context.coveredConditions("foo:src/foo.xoo", 1)).isEqualTo(2); } }
InputFile addFile(String moduleKey, String relpath, String language) { DefaultInputFile file = new TestInputFileBuilder(moduleKey, relpath) .setLanguage(language) .build(); put(moduleKey, file); return file; } }
@Test public void should_optimize_on_language() { DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() .onlyOnLanguages("java", "php"); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); fs.add(new TestInputFileBuilder("foo", "src/Foo.java").setLanguage("java").build()); assertThat(optimizer.shouldExecute(descriptor)).isTrue(); }
@Test public void testHighlighting() { assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).isEmpty(); tester.newHighlighting() .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build()) .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION) .highlight(8, 10, TypeOfText.CONSTANT) .highlight(9, 10, TypeOfText.COMMENT) .save(); assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).containsExactly(TypeOfText.ANNOTATION); assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT); }
@Test public void shouldValidateMaxLine() throws Exception { InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build(); Map<Integer, Integer> map = ImmutableMap.of(11, 3); String data = KeyValueFormat.format(map); thrown.expect(IllegalStateException.class); underTest.validateCoverageMeasure(data, file); }
@Test public void shouldFailIfNotExpectedFile() { InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").build(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("It was not expected to blame file " + file); new DefaultBlameOutput(null, Arrays.<InputFile>asList(new TestInputFileBuilder("foo", "src/main/java/Foo2.java").build())) .blameResult(file, Arrays.asList(new BlameLine().revision("1").author("guy"))); }
@Test public void testLineHits() { assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull(); assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull(); tester.newCoverage() .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build()) .lineHits(1, 2) .lineHits(2, 3) .save(); assertThat(tester.lineHits("foo:src/Foo.java", 1)).isEqualTo(2); assertThat(tester.lineHits("foo:src/Foo.java", 2)).isEqualTo(3); }
@Before public void before() throws IOException { predicates = new DefaultFilePredicates(temp.newFolder().toPath()); javaFile = new TestInputFileBuilder("foo", "src/main/java/struts/Action.java") .setModuleBaseDir(moduleBasePath) .setLanguage("java") .setStatus(Status.SAME) .build(); }
private DefaultInputFile setupFile(String path) { return new TestInputFileBuilder(context.module().key(), path) .setLanguage("bla") .setType(InputFile.Type.TEST) .initMetadata("1\n2\n3\n4\n5\n6") .build(); } }
@Test public void input_file_fails_if_too_many_results() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("expected one element"); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); fs.inputFile(fs.predicates().all()); }
@Test public void should_fail_if_default_profile_not_used() { store.put("foo", new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); settings.setProperty("sonar.profile", "Unknown"); QProfileVerifier profileLogger = new QProfileVerifier(settings.asConfig(), store, profiles); thrown.expect(MessageException.class); thrown.expectMessage("sonar.profile was set to 'Unknown' but didn't match any profile for any language. Please check your configuration."); profileLogger.execute(); }
@Test public void not_allowed_to_call_on_twice() throws IOException { thrown.expect(IllegalStateException.class); thrown.expectMessage("on() already called"); new DefaultMeasure<Integer>() .on(new DefaultInputProject(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()))) .on(new TestInputFileBuilder("foo", "src/Foo.php").build()) .withValue(3) .save(); }