Search in sources :

Example 46 with IMarker

use of org.eclipse.core.resources.IMarker 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 47 with IMarker

use of org.eclipse.core.resources.IMarker 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 48 with IMarker

use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.

the class WrappingPMDMarkerTest method setLanguage.

/**
 * Verifies that {@link WrappingPMDMarker#setLanguage(String)} sets the language on the wrapped marker.
 */
@Test
public void setLanguage() throws CoreException {
    final IMarker marker = mock(IMarker.class);
    final WrappingPMDMarker pmdMarker = new WrappingPMDMarker(marker);
    final String expected = "java";
    pmdMarker.setLanguage(expected);
    verify(marker).setAttribute("language", expected);
}
Also used : IMarker(org.eclipse.core.resources.IMarker) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 49 with IMarker

use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.

the class WrappingPMDMarkerTest method setRuleId.

/**
 * Verifies that {@link WrappingPMDMarker#setRuleId(String)} sets the rule id on the wrapped marker.
 */
@Test
public void setRuleId() throws CoreException {
    final IMarker marker = mock(IMarker.class);
    final WrappingPMDMarker pmdMarker = new WrappingPMDMarker(marker);
    final String expected = "Rule D";
    pmdMarker.setRuleId(expected);
    verify(marker).setAttribute(RULE_ID, expected);
}
Also used : IMarker(org.eclipse.core.resources.IMarker) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 50 with IMarker

use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.

the class WrappingPMDMarkerTest method isOtherWithSameRuleId.

/**
 * Verifies that {@link WrappingPMDMarker#isOtherWithSameRuleId(IMarker)} returns true if the argument is not the same
 * instance but has the same rule id.
 */
@Test
public void isOtherWithSameRuleId() {
    final IMarker marker = mock(IMarker.class);
    final String ruleId = "Rule A";
    when(marker.getAttribute(eq(RULE_ID), isA(String.class))).thenReturn(ruleId);
    final WrappingPMDMarker pmdMarker = new WrappingPMDMarker(marker);
    final IMarker other = mock(IMarker.class);
    when(other.getAttribute(eq(RULE_ID), isA(String.class))).thenReturn(ruleId);
    final boolean actual = pmdMarker.isOtherWithSameRuleId(other);
    assertTrue("The marker must not be the same instance as the other marker", actual);
}
Also used : IMarker(org.eclipse.core.resources.IMarker) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

IMarker (org.eclipse.core.resources.IMarker)115 CoreException (org.eclipse.core.runtime.CoreException)31 Test (org.junit.Test)31 IFile (org.eclipse.core.resources.IFile)23 IResource (org.eclipse.core.resources.IResource)16 ArrayList (java.util.ArrayList)15 IProject (org.eclipse.core.resources.IProject)8 IPath (org.eclipse.core.runtime.IPath)8 Position (org.eclipse.jface.text.Position)8 Annotation (org.eclipse.jface.text.source.Annotation)7 Matchers.anyString (org.mockito.Matchers.anyString)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Rule (net.sourceforge.pmd.Rule)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 RuleViolation (net.sourceforge.pmd.RuleViolation)5 JavaLanguageModule (net.sourceforge.pmd.lang.java.JavaLanguageModule)5 IDocument (org.eclipse.jface.text.IDocument)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5