Adding unit tests. Cleaning up code.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java
new file mode 100644
index 0000000..8cfe447
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java
@@ -0,0 +1,46 @@
+/*
+ * 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.scoping;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.scoping.impl.ImportUriResolver;
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.XtextRule;
+import com.google.eclipse.protobuf.protobuf.Import;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
+
+/**
+ * Tests for <code>{@link ImportUriFixerAndResolver#apply(EObject)}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ImportUriFixerAndResolver_apply_Test {
+
+ @Rule public XtextRule xtext = new XtextRule();
+
+ private ImportUriFixerAndResolver resolver;
+
+ @Before public void setUp() {
+ resolver = (ImportUriFixerAndResolver) xtext.getInstanceOf(ImportUriResolver.class);
+ }
+
+ @Test public void should_fix_import_URI_if_missing_scheme() {
+ StringBuilder proto = new StringBuilder();
+ proto.append("import \"folder1/test.proto\";");
+ Protobuf root = xtext.parse(proto);
+ Import anImport = root.getImports().get(0);
+ String resolved = resolver.apply(anImport);
+ assertThat(resolved, equalTo("platform:/resource/folder1/test.proto"));
+ }
+
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
index 13abf5f..90ccad9 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
@@ -8,7 +8,7 @@
*/
package com.google.eclipse.protobuf.ui.labeling;
-import static com.google.eclipse.protobuf.scoping.SimpleImportUriResolver.URI_PREFIX;
+import static com.google.eclipse.protobuf.scoping.ImportUriFixerAndResolver.URI_PREFIX;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.ui.util.Properties;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
index 4896ae9..a8b9ace 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/ProtobufRuntimeModule.java
@@ -12,7 +12,7 @@
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
import com.google.eclipse.protobuf.naming.ProtobufQualifiedNameProvider;
-import com.google.eclipse.protobuf.scoping.SimpleImportUriResolver;
+import com.google.eclipse.protobuf.scoping.ImportUriFixerAndResolver;
import com.google.inject.Binder;
/**
@@ -26,6 +26,6 @@
}
public void configureImportUriResolver(Binder binder) {
- binder.bind(ImportUriResolver.class).to(SimpleImportUriResolver.class);
+ binder.bind(ImportUriResolver.class).to(ImportUriFixerAndResolver.class);
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/SimpleImportUriResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
similarity index 74%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/SimpleImportUriResolver.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
index 866eebf..2de2ca2 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/SimpleImportUriResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
@@ -8,8 +8,6 @@
*/
package com.google.eclipse.protobuf.scoping;
-import java.util.List;
-
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
@@ -27,7 +25,7 @@
*
* @author alruiz@google.com (Alex Ruiz)
*/
-public class SimpleImportUriResolver extends ImportUriResolver {
+public class ImportUriFixerAndResolver extends ImportUriResolver {
private static final String PREFIX = "platform:/resource";
@@ -63,22 +61,9 @@
* We need to have the import URI as "platform:/resource/protobuf-test/folder/proto2.proto" for the editor to see it.
*/
private void fixUri(Import i) {
- String prefix = uriPrefix(i.eResource().getURI());
- String uri = i.getImportURI();
- if (!uri.startsWith(prefix)) {
- if (!uri.startsWith(prefix)) prefix += "/";
- i.setImportURI(prefix + uri);
- }
- }
-
- private String uriPrefix(URI containerUri) {
- StringBuilder prefix = new StringBuilder();
- prefix.append(PREFIX);
- int start = (containerUri.scheme() == null) ? 0 : 1; // ignore the scheme if present (e.g. "resource")
- List<String> segments = containerUri.segmentsList();
- int end = segments.size() - 1; // ignore file name (at the end of the URI)
- for (int j = start; j < end; j++)
- prefix.append("/").append(segments.get(j));
- return prefix.toString();
+ URI importUri = URI.createURI(i.getImportURI());
+ if (importUri.scheme() != null) return;
+ URI fixedUri = URI.createURI(PREFIX).appendSegments(importUri.segments());
+ i.setImportURI(fixedUri.toString());
}
}