Fixed: [ Issue 54 ] Markers from protoc are added to wrong files https://code.google.com/p/protobuf-dt/issues/detail?id=54
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/stubs/resources/FileStub.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/stubs/resources/FileStub.java index 524c744..d5e6136 100644 --- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/stubs/resources/FileStub.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/stubs/resources/FileStub.java
@@ -10,8 +10,7 @@ import static java.util.Collections.*; -import java.io.InputStream; -import java.io.Reader; +import java.io.*; import java.net.URI; import java.util.*; @@ -26,6 +25,7 @@ public class FileStub implements IFile { private final Map<String, List<MarkerStub>> markersByType = new HashMap<String, List<MarkerStub>>(); + private IPath location; /** {@inheritDoc} */ public void accept(IResourceProxyVisitor visitor, int memberFlags) { @@ -225,7 +225,11 @@ /** {@inheritDoc} */ public IPath getLocation() { - throw new UnsupportedOperationException(); + return location; + } + + public void setLocation(IPath location) { + this.location = location; } /** {@inheritDoc} */ @@ -516,8 +520,8 @@ List<MarkerStub> markers = markersByType.get(type); return (markers == null) ? 0 : markers.size(); } - - public List<MarkerStub> markers(String type) { + + public List<MarkerStub> markersOfType(String type) { List<MarkerStub> markers = markersByType.get(type); if (markers == null) return emptyList(); return unmodifiableList(markers);
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory_createErrorIfNecessary_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory_createErrorIfNecessary_Test.java index 481d40d..ea42a3d 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory_createErrorIfNecessary_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory_createErrorIfNecessary_Test.java
@@ -15,14 +15,13 @@ import java.util.List; -import org.eclipse.core.runtime.CoreException; -import org.junit.Before; -import org.junit.Test; +import org.eclipse.core.runtime.*; +import org.junit.*; import com.google.eclipse.protobuf.junit.stubs.resources.*; /** - * Tests for <code>{@link ProtocMarkerFactory#createErrorIfNecessary(String, int)}</code>. + * Tests for <code>{@link ProtocMarkerFactory#createErrorIfNecessary(String, String, int)}</code>. * * @author alruiz@google.com (Alex Ruiz) */ @@ -33,26 +32,33 @@ private FileStub file; private MarkerStub fastValidationMarker; private ProtocMarkerFactory markerFactory; - + @Before public void setUp() throws CoreException { file = new FileStub(); + file.setLocation(new Path("home/alex/protos/test1.proto")); file.createMarker(PROTOC); fastValidationMarker = error(FAST_VALIDATION, "Expected field name.", 68); file.addMarker(fastValidationMarker); markerFactory = new ProtocMarkerFactory(file); } - - @Test public void should_create_marker_if_a_similar_one_does_not_exist() throws CoreException { + + @Test public void should_create_marker_if_file_paths_match_and_a_similar_marker_does_not_exist() throws CoreException { String message = "File not found."; int lineNumber = 8; - markerFactory.createErrorIfNecessary(message, lineNumber); - List<MarkerStub> markers = file.markers(PROTOC); + markerFactory.createErrorIfNecessary("test1.proto", message, lineNumber); + List<MarkerStub> markers = file.markersOfType(PROTOC); assertThat(markers.size(), equalTo(1)); assertThat(markers.get(0), equalTo(error(PROTOC, message, lineNumber))); } - + + @Test public void should_not_create_marker_if_given_path_does_not_match_path_in_file() throws CoreException { + markerFactory.createErrorIfNecessary("test2.proto", "File not found.", 8); + assertThat(file.markerCount(PROTOC), equalTo(0)); + } + @Test public void should_not_create_marker_if_a_similar_one_exists() throws CoreException { - markerFactory.createErrorIfNecessary(fastValidationMarker.message(), fastValidationMarker.lineNumber()); + markerFactory.createErrorIfNecessary("test1.proto", fastValidationMarker.message(), + fastValidationMarker.lineNumber()); assertThat(file.markerCount(PROTOC), equalTo(0)); } }
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser_parseAndAddMarkerIfNecessary_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser_parseAndAddMarkerIfNecessary_Test.java index 58d02ea..544eddf 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser_parseAndAddMarkerIfNecessary_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser_parseAndAddMarkerIfNecessary_Test.java
@@ -11,8 +11,7 @@ import static org.mockito.Mockito.*; import org.eclipse.core.runtime.CoreException; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; /** * Tests for <code>{@link ProtocOutputParser#parseAndAddMarkerIfNecessary(String, ProtocMarkerFactory)}</code>. @@ -23,21 +22,21 @@ private ProtocMarkerFactory markerFactory; private ProtocOutputParser outputParser; - + @Before public void setUp() { markerFactory = mock(ProtocMarkerFactory.class); outputParser = new ProtocOutputParser(); } - + @Test public void should_not_create_IMarker_if_line_does_not_match_error_pattern() throws CoreException { String line = "person.proto: File not found."; outputParser.parseAndAddMarkerIfNecessary(line, markerFactory); verifyZeroInteractions(markerFactory); } - + @Test public void should_attempt_to_create_IMarker_if_line_matches_error_pattern() throws CoreException { String line = "test.proto:23:21: Expected field name."; outputParser.parseAndAddMarkerIfNecessary(line, markerFactory); - verify(markerFactory).createErrorIfNecessary("Expected field name.", 23); + verify(markerFactory).createErrorIfNecessary("test.proto", "Expected field name.", 23); } }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory.java index 1188d40..5a5fe76 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory.java
@@ -12,8 +12,7 @@ import static org.eclipse.core.resources.IResource.DEPTH_INFINITE; import static org.eclipse.xtext.ui.MarkerTypes.FAST_VALIDATION; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.*; import org.eclipse.core.runtime.CoreException; /** @@ -34,8 +33,9 @@ markers = file.findMarkers(FAST_VALIDATION, true, DEPTH_INFINITE); } - void createErrorIfNecessary(String message, int lineNumber) throws CoreException { - if (containsMarker(message, lineNumber)) return; + void createErrorIfNecessary(String fileName, String message, int lineNumber) throws CoreException { + String location = file.getLocation().toOSString(); + if (!location.endsWith(fileName) || containsMarker(message, lineNumber)) return; IMarker marker = file.createMarker(PROTOC); marker.setAttribute(SEVERITY, SEVERITY_ERROR); marker.setAttribute(MESSAGE, message);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser.java index d4822a2..e1bd06c 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtocOutputParser.java
@@ -10,8 +10,7 @@ import static java.lang.Integer.parseInt; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.regex.*; import org.eclipse.core.runtime.CoreException; @@ -37,8 +36,9 @@ void parseAndAddMarkerIfNecessary(String line, ProtocMarkerFactory markerFactory) throws CoreException { Matcher errorMatcher = ERROR_PATTERN.matcher(line); if (!errorMatcher.matches()) return; + String fileName = errorMatcher.group(1); int lineNumber = parseInt(errorMatcher.group(2)); String message = errorMatcher.group(4); - markerFactory.createErrorIfNecessary(message, lineNumber); + markerFactory.createErrorIfNecessary(fileName, message, lineNumber); } }