Adding unit tests.
diff --git a/com.google.eclipse.protobuf.junit/META-INF/MANIFEST.MF b/com.google.eclipse.protobuf.junit/META-INF/MANIFEST.MF index a1521b5..a2160b9 100644 --- a/com.google.eclipse.protobuf.junit/META-INF/MANIFEST.MF +++ b/com.google.eclipse.protobuf.junit/META-INF/MANIFEST.MF
@@ -21,7 +21,9 @@ com.google.inject;bundle-version="2.0.0", org.eclipse.core.resources;bundle-version="3.7.100", org.eclipse.core.jobs;bundle-version="3.5.100", - org.eclipse.equinox.common;bundle-version="3.6.0" + org.eclipse.equinox.common;bundle-version="3.6.0", + org.eclipse.core.contenttype;bundle-version="3.4.100" Export-Package: com.google.eclipse.protobuf.junit.core, com.google.eclipse.protobuf.junit.matchers, + com.google.eclipse.protobuf.junit.stubs, com.google.eclipse.protobuf.junit.util
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/FileStub.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/FileStub.java index 22491f4..e6dcec0 100644 --- a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/FileStub.java +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/FileStub.java
@@ -8,6 +8,8 @@ */ package com.google.eclipse.protobuf.junit.stubs; +import static java.util.Collections.*; + import java.io.InputStream; import java.io.Reader; import java.net.URI; @@ -15,14 +17,15 @@ import org.eclipse.core.resources.*; import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.content.IContentDescription; import org.eclipse.core.runtime.jobs.ISchedulingRule; /** * @author alruiz@google.com (Alex Ruiz) */ -public abstract class FileStub implements IFile { +public class FileStub implements IFile { - private final Map<String, List<IMarker>> markersByType = new HashMap<String, List<IMarker>>(); + private final Map<String, List<MarkerStub>> markersByType = new HashMap<String, List<MarkerStub>>(); /** {@inheritDoc} */ public void accept(IResourceProxyVisitor visitor, int memberFlags) throws CoreException { @@ -45,6 +48,17 @@ } /** {@inheritDoc} */ + public void appendContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) + throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void appendContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public void clearHistory(IProgressMonitor monitor) throws CoreException { throw new UnsupportedOperationException(); } @@ -75,14 +89,29 @@ } /** {@inheritDoc} */ + public void create(InputStream source, boolean force, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void create(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void createLink(IPath localLocation, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void createLink(URI location, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public IMarker createMarker(String type) throws CoreException { MarkerStub marker = new MarkerStub(type); - List<IMarker> markers = markersByType.get(type); - if (markers == null) { - markers = new ArrayList<IMarker>(); - markersByType.put(type, markers); - } - markers.add(marker); + addMarker(marker); return marker; } @@ -92,6 +121,11 @@ } /** {@inheritDoc} */ + public void delete(boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public void delete(boolean force, IProgressMonitor monitor) throws CoreException { throw new UnsupportedOperationException(); } @@ -103,7 +137,7 @@ /** {@inheritDoc} */ public void deleteMarkers(String type, boolean includeSubtypes, int depth) throws CoreException { - List<IMarker> markers = markersByType.get(type); + List<MarkerStub> markers = markersByType.get(type); if (markers != null) markers.clear(); } @@ -119,7 +153,7 @@ /** {@inheritDoc} */ public IMarker[] findMarkers(String type, boolean includeSubtypes, int depth) throws CoreException { - List<IMarker> markers = markersByType.get(type); + List<MarkerStub> markers = markersByType.get(type); if (markers == null) return new IMarker[0]; return markers.toArray(new IMarker[markers.size()]); } @@ -135,11 +169,56 @@ } /** {@inheritDoc} */ + public String getCharset() throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public String getCharset(boolean checkImplicit) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public String getCharsetFor(Reader reader) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public IContentDescription getContentDescription() throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public InputStream getContents() throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public InputStream getContents(boolean force) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public int getEncoding() throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public String getFileExtension() { throw new UnsupportedOperationException(); } /** {@inheritDoc} */ + public IPath getFullPath() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public IFileState[] getHistory(IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public long getLocalTimeStamp() { throw new UnsupportedOperationException(); } @@ -165,7 +244,7 @@ } /** {@inheritDoc} */ - public IPathVariableManager getPathVariableManager() { + public String getName() { throw new UnsupportedOperationException(); } @@ -175,6 +254,11 @@ } /** {@inheritDoc} */ + public IPathVariableManager getPathVariableManager() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public Map<QualifiedName, String> getPersistentProperties() throws CoreException { throw new UnsupportedOperationException(); } @@ -265,11 +349,6 @@ } /** {@inheritDoc} */ - public boolean isVirtual() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ public boolean isLinked(int options) { throw new UnsupportedOperationException(); } @@ -285,6 +364,11 @@ } /** {@inheritDoc} */ + public boolean isReadOnly() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public boolean isSynchronized(int depth) { throw new UnsupportedOperationException(); } @@ -300,6 +384,17 @@ } /** {@inheritDoc} */ + public boolean isVirtual() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void move(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) + throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public void move(IPath destination, boolean force, IProgressMonitor monitor) throws CoreException { throw new UnsupportedOperationException(); } @@ -331,6 +426,38 @@ } /** {@inheritDoc} */ + public void setCharset(String newCharset) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void setCharset(String newCharset, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void setContents(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) + throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void setContents(IFileState source, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void setContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) + throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + public void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ public void setDerived(boolean isDerived) throws CoreException { throw new UnsupportedOperationException(); } @@ -385,127 +512,24 @@ throw new UnsupportedOperationException(); } - /** {@inheritDoc} */ - public void appendContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) - throws CoreException { - throw new UnsupportedOperationException(); + public int markerCount(String type) { + List<MarkerStub> markers = markersByType.get(type); + return (markers == null) ? 0 : markers.size(); + } + + public List<MarkerStub> markers(String type) { + List<MarkerStub> markers = markersByType.get(type); + if (markers == null) return emptyList(); + return unmodifiableList(markers); } - /** {@inheritDoc} */ - public void appendContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void create(InputStream source, boolean force, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void create(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void createLink(IPath localLocation, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void createLink(URI location, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void delete(boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public String getCharset() throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public String getCharset(boolean checkImplicit) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public String getCharsetFor(Reader reader) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public InputStream getContents() throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public InputStream getContents(boolean force) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public int getEncoding() throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public IPath getFullPath() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public IFileState[] getHistory(IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public String getName() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public boolean isReadOnly() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void move(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) - throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setCharset(String newCharset) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setCharset(String newCharset, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor) - throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setContents(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) - throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - public void setContents(IFileState source, int updateFlags, IProgressMonitor monitor) throws CoreException { - throw new UnsupportedOperationException(); + public void addMarker(MarkerStub marker) throws CoreException { + String type = marker.getType(); + List<MarkerStub> markers = markersByType.get(type); + if (markers == null) { + markers = new ArrayList<MarkerStub>(); + markersByType.put(type, markers); + } + markers.add(marker); } }
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/MarkerStub.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/MarkerStub.java index 0578871..4312479 100644 --- a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/MarkerStub.java +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/stubs/MarkerStub.java
@@ -1,9 +1,6 @@ /* - * Copyright (c) 2011 Google Inc. - * - * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse - * Public License v1.0 which accompanies this distribution, and is available at - * + * Copyright (c) 2011 Google Inc. All rights reserved. This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ package com.google.eclipse.protobuf.junit.stubs; @@ -25,6 +22,14 @@ private final String type; private final long creationTime; + public static MarkerStub error(String type, String description, int lineNumber) throws CoreException { + MarkerStub marker = new MarkerStub(type); + marker.setAttribute(SEVERITY, SEVERITY_ERROR); + marker.setAttribute(MESSAGE, description); + marker.setAttribute(LINE_NUMBER, lineNumber); + return marker; + } + public MarkerStub(String type) { this.type = type; creationTime = System.currentTimeMillis(); @@ -79,7 +84,8 @@ /** {@inheritDoc} */ public Object[] getAttributes(String[] attributeNames) throws CoreException { List<Object> values = new ArrayList<Object>(); - for (String name : attributeNames) values.add(attributes.get(name)); + for (String name : attributeNames) + values.add(attributes.get(name)); return values.toArray(); } @@ -132,4 +138,16 @@ public void setAttributes(Map<String, ? extends Object> attributes) throws CoreException { this.attributes.putAll(attributes); } + + public int severity() throws CoreException { + return getAttribute(SEVERITY, -1); + } + + public String message() throws CoreException { + return (String) getAttribute(MESSAGE); + } + + public int lineNumber() throws CoreException { + return getAttribute(LINE_NUMBER, -1); + } }
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 new file mode 100644 index 0000000..c88b35c --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtocMarkerFactory_createErrorIfNecessary_Test.java
@@ -0,0 +1,65 @@ +/* + * Copyright (c) 2011 Google Inc. + * + * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse + * Public License v1.0 which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package com.google.eclipse.protobuf.ui.builder; + +import static com.google.eclipse.protobuf.junit.stubs.MarkerStub.error; +import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR; +import static org.eclipse.xtext.ui.MarkerTypes.FAST_VALIDATION; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.junit.Before; +import org.junit.Test; + +import com.google.eclipse.protobuf.junit.stubs.FileStub; +import com.google.eclipse.protobuf.junit.stubs.MarkerStub; + +/** + * Tests for <code>{@link ProtocMarkerFactory#createErrorIfNecessary(String, int)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class ProtocMarkerFactory_createErrorIfNecessary_Test { + + private static final String PROTOC = "com.google.eclipse.protobuf.ui.protocMarker"; + + private FileStub file; + private MarkerStub fastValidationMarker; + private ProtocMarkerFactory markerFactory; + + @Before public void setUp() throws CoreException { + file = new FileStub(); + file.createMarker(PROTOC); + assertThat(file.markerCount(PROTOC), equalTo(1)); + fastValidationMarker = error(FAST_VALIDATION, "Expected field name.", 68); + file.addMarker(fastValidationMarker); + markerFactory = new ProtocMarkerFactory(file); + assertThat(file.markerCount(PROTOC), equalTo(0)); + } + + @Test public void should_create_marker_if_a_similar_one_does_not_exist() throws CoreException { + String message = "File not found."; + int lineNumber = 8; + markerFactory.createErrorIfNecessary(message, lineNumber); + List<MarkerStub> markers = file.markers(PROTOC); + assertThat(markers.size(), equalTo(1)); + MarkerStub marker = markers.get(0); + assertThat(marker.severity(), equalTo(SEVERITY_ERROR)); + assertThat(marker.message(), equalTo(message)); + assertThat(marker.lineNumber(), equalTo(lineNumber)); + } + + @Test public void should_not_create_marker_if_a_similar_one_exists() throws CoreException { + markerFactory.createErrorIfNecessary(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 5a37962..58d02ea 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
@@ -36,8 +36,8 @@ } @Test public void should_attempt_to_create_IMarker_if_line_matches_error_pattern() throws CoreException { - String line = "test.proto:23:21: Expected field name"; + String line = "test.proto:23:21: Expected field name."; outputParser.parseAndAddMarkerIfNecessary(line, markerFactory); - verify(markerFactory).createErrorIfNecessary("Expected field name", 23); + verify(markerFactory).createErrorIfNecessary("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 f62fd06..a1eb6c9 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
@@ -23,7 +23,7 @@ */ class ProtocMarkerFactory { - static final String PROTOC = "com.google.eclipse.protobuf.ui.protocMarker"; + private static final String PROTOC = "com.google.eclipse.protobuf.ui.protocMarker"; private final IFile file; private final IMarker[] markers; @@ -34,11 +34,11 @@ markers = file.findMarkers(FAST_VALIDATION, true, DEPTH_INFINITE); } - void createErrorIfNecessary(String description, int lineNumber) throws CoreException { - if (containsMarker(description, lineNumber)) return; + void createErrorIfNecessary(String message, int lineNumber) throws CoreException { + if (containsMarker(message, lineNumber)) return; IMarker marker = file.createMarker(PROTOC); marker.setAttribute(SEVERITY, SEVERITY_ERROR); - marker.setAttribute(MESSAGE, description); + marker.setAttribute(MESSAGE, message); marker.setAttribute(LINE_NUMBER, lineNumber); }
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 f7ba1c6..d4822a2 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
@@ -38,7 +38,7 @@ Matcher errorMatcher = ERROR_PATTERN.matcher(line); if (!errorMatcher.matches()) return; int lineNumber = parseInt(errorMatcher.group(2)); - String description = errorMatcher.group(4); - markerFactory.createErrorIfNecessary(description, lineNumber); + String message = errorMatcher.group(4); + markerFactory.createErrorIfNecessary(message, lineNumber); } }