Search in sources :

Example 1 with JavaLanguageModule

use of net.sourceforge.pmd.lang.java.JavaLanguageModule in project eclipse-pmd by acanda.

the class MarkerUtilTest method addMarker.

/**
 * Verifies that {@link MarkerUtil#addMarker(IFile, String, RuleViolation)} adds a marker to the provided file.
 */
@Test
public void addMarker() throws CoreException {
    final IFile file = mock(IFile.class);
    final IMarker marker = mock(IMarker.class);
    when(file.createMarker(MARKER_TYPE)).thenReturn(marker);
    final RuleViolation violation = mock(RuleViolation.class);
    when(violation.getDescription()).thenReturn("message");
    when(violation.getBeginLine()).thenReturn(1);
    when(violation.getBeginColumn()).thenReturn(17);
    when(violation.getEndLine()).thenReturn(1);
    when(violation.getEndColumn()).thenReturn(22);
    when(violation.getClassName()).thenReturn("ClassName");
    final Rule rule = mock(Rule.class);
    when(rule.getLanguage()).thenReturn(new JavaLanguageModule());
    when(rule.getRuleSetName()).thenReturn("basic");
    when(rule.getName()).thenReturn("ExtendsObject");
    when(violation.getRule()).thenReturn(rule);
    final IMarker actual = MarkerUtil.addMarker(file, "class A extends Object {}", violation);
    assertNotNull("The method must always return a marker", actual);
    verify(file).createMarker(MARKER_TYPE);
    verify(actual).setAttribute(IMarker.MESSAGE, "message");
    verify(actual).setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
    verify(actual).setAttribute(IMarker.LINE_NUMBER, 1);
    verify(actual).setAttribute(IMarker.CHAR_START, 16);
    verify(actual).setAttribute(IMarker.CHAR_END, 22);
    verify(actual).setAttribute("ruleId", "java.basic.ExtendsObject");
    verify(actual).setAttribute("violationClassName", "ClassName");
    verify(actual).setAttribute("markerText", "Object");
}
Also used : IFile(org.eclipse.core.resources.IFile) JavaLanguageModule(net.sourceforge.pmd.lang.java.JavaLanguageModule) IMarker(org.eclipse.core.resources.IMarker) RuleViolation(net.sourceforge.pmd.RuleViolation) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Example 2 with JavaLanguageModule

use of net.sourceforge.pmd.lang.java.JavaLanguageModule in project eclipse-pmd by acanda.

the class MarkerUtilTest method addMarkerWithUnknwonPositionInformation.

/**
 * Verifies that {@link MarkerUtil#addMarker(IFile, String, RuleViolation)} adds a marker to the provided file with
 * position information set to zero if the respective arguments are negative.
 */
@Test
public void addMarkerWithUnknwonPositionInformation() throws CoreException {
    final IFile file = mock(IFile.class);
    final IMarker marker = mock(IMarker.class);
    when(file.createMarker(MARKER_TYPE)).thenReturn(marker);
    final RuleViolation violation = mock(RuleViolation.class);
    when(violation.getDescription()).thenReturn("message");
    when(violation.getBeginLine()).thenReturn(-1);
    when(violation.getBeginColumn()).thenReturn(-18);
    when(violation.getEndLine()).thenReturn(-1);
    when(violation.getEndColumn()).thenReturn(-24);
    when(violation.getClassName()).thenReturn("ClassName");
    final Rule rule = mock(Rule.class);
    when(rule.getLanguage()).thenReturn(new JavaLanguageModule());
    when(rule.getRuleSetName()).thenReturn("basic");
    when(rule.getName()).thenReturn("ExtendsObject");
    when(violation.getRule()).thenReturn(rule);
    final IMarker actual = MarkerUtil.addMarker(file, "class A extends Object {}", violation);
    assertNotNull("The method must always return a marker", actual);
    verify(file).createMarker(MARKER_TYPE);
    verify(actual).setAttribute(IMarker.MESSAGE, "message");
    verify(actual).setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
    verify(actual).setAttribute(IMarker.LINE_NUMBER, 0);
    verify(actual).setAttribute(IMarker.CHAR_START, 0);
    verify(actual).setAttribute(IMarker.CHAR_END, 0);
    verify(actual).setAttribute("ruleId", "java.basic.ExtendsObject");
    verify(actual).setAttribute("violationClassName", "ClassName");
    verify(actual).setAttribute("markerText", "");
}
Also used : IFile(org.eclipse.core.resources.IFile) JavaLanguageModule(net.sourceforge.pmd.lang.java.JavaLanguageModule) IMarker(org.eclipse.core.resources.IMarker) RuleViolation(net.sourceforge.pmd.RuleViolation) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Example 3 with JavaLanguageModule

use of net.sourceforge.pmd.lang.java.JavaLanguageModule in project eclipse-pmd by acanda.

the class MarkerUtilTest method addMarkerWithTab.

/**
 * Verifies that {@link MarkerUtil#addMarker(IFile, String, RuleViolation)} adds a marker to the provided file with
 * the correct position if the content contains tabs and spaces.
 */
@Test
public void addMarkerWithTab() throws CoreException {
    final IFile file = mock(IFile.class);
    final IMarker marker = mock(IMarker.class);
    when(file.createMarker(MARKER_TYPE)).thenReturn(marker);
    final RuleViolation violation = mock(RuleViolation.class);
    when(violation.getDescription()).thenReturn("message");
    when(violation.getBeginLine()).thenReturn(1);
    when(violation.getBeginColumn()).thenReturn(25);
    when(violation.getEndLine()).thenReturn(1);
    when(violation.getEndColumn()).thenReturn(30);
    when(violation.getClassName()).thenReturn("ClassName");
    final Rule rule = mock(Rule.class);
    when(rule.getLanguage()).thenReturn(new JavaLanguageModule());
    when(rule.getRuleSetName()).thenReturn("basic");
    when(rule.getName()).thenReturn("ExtendsObject");
    when(violation.getRule()).thenReturn(rule);
    final IMarker actual = MarkerUtil.addMarker(file, "class ABC extends\tObject {}", violation);
    assertNotNull("The method must always return a marker", actual);
    verify(file).createMarker(MARKER_TYPE);
    verify(actual).setAttribute(IMarker.MESSAGE, "message");
    verify(actual).setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
    verify(actual).setAttribute(IMarker.LINE_NUMBER, 1);
    verify(actual).setAttribute(IMarker.CHAR_START, 18);
    verify(actual).setAttribute(IMarker.CHAR_END, 24);
    verify(actual).setAttribute("ruleId", "java.basic.ExtendsObject");
    verify(actual).setAttribute("violationClassName", "ClassName");
    verify(actual).setAttribute("markerText", "Object");
}
Also used : IFile(org.eclipse.core.resources.IFile) JavaLanguageModule(net.sourceforge.pmd.lang.java.JavaLanguageModule) IMarker(org.eclipse.core.resources.IMarker) RuleViolation(net.sourceforge.pmd.RuleViolation) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Example 4 with JavaLanguageModule

use of net.sourceforge.pmd.lang.java.JavaLanguageModule in project eclipse-pmd by acanda.

the class MarkerUtilTest method addMarkerSpanningMoreThanOneLine.

/**
 * Verifies that {@link MarkerUtil#addMarker(IFile, String, RuleViolation)} adds a marker to the provided file with
 * position information containing the line, the character start and end position but not the marker text if the
 * marker would span more than one line.
 */
@Test
public void addMarkerSpanningMoreThanOneLine() throws CoreException {
    final IFile file = mock(IFile.class);
    final IMarker marker = mock(IMarker.class);
    when(file.createMarker(LONG_MARKER_TYPE)).thenReturn(marker);
    final RuleViolation violation = mock(RuleViolation.class);
    when(violation.getDescription()).thenReturn("message");
    when(violation.getBeginLine()).thenReturn(1);
    when(violation.getBeginColumn()).thenReturn(24);
    when(violation.getEndLine()).thenReturn(2);
    when(violation.getEndColumn()).thenReturn(1);
    when(violation.getClassName()).thenReturn("ClassName");
    final Rule rule = mock(Rule.class);
    when(rule.getLanguage()).thenReturn(new JavaLanguageModule());
    when(rule.getRuleSetName()).thenReturn("basic");
    when(rule.getName()).thenReturn("ExtendsObject");
    when(violation.getRule()).thenReturn(rule);
    final IMarker actual = MarkerUtil.addMarker(file, "class A extends Object {\n}", violation);
    assertNotNull("The method must always return a marker", actual);
    verify(file).createMarker(LONG_MARKER_TYPE);
    verify(actual).setAttribute(IMarker.MESSAGE, "message");
    verify(actual).setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
    verify(actual).setAttribute(IMarker.LINE_NUMBER, 1);
    verify(actual).setAttribute(IMarker.CHAR_START, 23);
    verify(actual).setAttribute(IMarker.CHAR_END, 26);
    verify(actual).setAttribute("ruleId", "java.basic.ExtendsObject");
    verify(actual).setAttribute("violationClassName", "ClassName");
    verify(actual, never()).setAttribute(eq("markerText"), anyString());
}
Also used : IFile(org.eclipse.core.resources.IFile) JavaLanguageModule(net.sourceforge.pmd.lang.java.JavaLanguageModule) IMarker(org.eclipse.core.resources.IMarker) RuleViolation(net.sourceforge.pmd.RuleViolation) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Example 5 with JavaLanguageModule

use of net.sourceforge.pmd.lang.java.JavaLanguageModule in project eclipse-pmd by acanda.

the class MarkerUtilTest method addMarkerEndBeforeStart.

/**
 * Verifies that {@link MarkerUtil#addMarker(IFile, String, RuleViolation)} adds a marker even when the character
 * start and end positions are mixed up, i.e. {@code start > end}. For some rules, PMD creates violations where the
 * start position is greater than the end position.
 */
@Test
public void addMarkerEndBeforeStart() throws CoreException {
    final IFile file = mock(IFile.class);
    final IMarker marker = mock(IMarker.class);
    when(file.createMarker(MARKER_TYPE)).thenReturn(marker);
    final RuleViolation violation = mock(RuleViolation.class);
    when(violation.getDescription()).thenReturn("message");
    when(violation.getBeginLine()).thenReturn(1);
    when(violation.getBeginColumn()).thenReturn(22);
    when(violation.getEndLine()).thenReturn(1);
    when(violation.getEndColumn()).thenReturn(17);
    when(violation.getClassName()).thenReturn("ClassName");
    final Rule rule = mock(Rule.class);
    when(rule.getLanguage()).thenReturn(new JavaLanguageModule());
    when(rule.getRuleSetName()).thenReturn("basic");
    when(rule.getName()).thenReturn("ExtendsObject");
    when(violation.getRule()).thenReturn(rule);
    final IMarker actual = MarkerUtil.addMarker(file, "class A extends Object { }", violation);
    assertNotNull("The method must always return a marker", actual);
    verify(file).createMarker(MARKER_TYPE);
    verify(actual).setAttribute(IMarker.MESSAGE, "message");
    verify(actual).setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
    verify(actual).setAttribute(IMarker.LINE_NUMBER, 1);
    verify(actual).setAttribute(IMarker.CHAR_START, 16);
    verify(actual).setAttribute(IMarker.CHAR_END, 22);
    verify(actual).setAttribute("ruleId", "java.basic.ExtendsObject");
    verify(actual).setAttribute("violationClassName", "ClassName");
    verify(actual).setAttribute("markerText", "Object");
}
Also used : IFile(org.eclipse.core.resources.IFile) JavaLanguageModule(net.sourceforge.pmd.lang.java.JavaLanguageModule) IMarker(org.eclipse.core.resources.IMarker) RuleViolation(net.sourceforge.pmd.RuleViolation) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Aggregations

Rule (net.sourceforge.pmd.Rule)5 RuleViolation (net.sourceforge.pmd.RuleViolation)5 JavaLanguageModule (net.sourceforge.pmd.lang.java.JavaLanguageModule)5 IFile (org.eclipse.core.resources.IFile)5 IMarker (org.eclipse.core.resources.IMarker)5 Test (org.junit.Test)5