Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder_findRootOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder_findRootOf_Test.java
index ff28551..015dda5 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder_findRootOf_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder_findRootOf_Test.java
@@ -8,7 +8,6 @@
*/
package com.google.eclipse.protobuf.ui.builder;
-import static java.io.File.separator;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
@@ -27,7 +26,7 @@
private static ProtoDescriptorPathFinder finder;
@BeforeClass public static void setUpOnce() {
- finder = new ProtoDescriptorPathFinder();
+ finder = new ProtoDescriptorPathFinder("/");
}
@Rule public ExpectedException thrown = none();
@@ -42,21 +41,11 @@
@Test public void should_throw_error_if_path_does_not_contain_descriptor_FQN() {
thrown.expect(IllegalArgumentException.class);
- if (separator.equals("\\")) {
- thrown.expectMessage("Path '\\usr\\local\\include' does not contain '\\google\\protobuf\\descriptor.proto'");
- finder.findRootOf("\\usr\\local\\include");
- return;
- }
thrown.expectMessage("Path '/usr/local/include' does not contain '/google/protobuf/descriptor.proto'");
finder.findRootOf("/usr/local/include");
}
@Test public void should_find_import_root_of_descriptor() {
- if (separator.equals("\\")) {
- String filePath = "\\usr\\local\\include\\google\\protobuf\\descriptor.proto";
- assertThat(finder.findRootOf(filePath), equalTo("\\usr\\local\\include"));
- return;
- }
String filePath = "/usr/local/include/google/protobuf/descriptor.proto";
assertThat(finder.findRootOf(filePath), equalTo("/usr/local/include"));
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder.java
index f2d984a..f419144 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/builder/ProtoDescriptorPathFinder.java
@@ -12,19 +12,30 @@
import static java.util.Arrays.asList;
import static org.eclipse.xtext.util.Strings.*;
+import com.google.inject.Singleton;
+
/**
* @author alruiz@google.com (Alex Ruiz)
*/
+@Singleton
class ProtoDescriptorPathFinder {
- private static final String DESCRIPTOR_FQN = concat(separator, asList("", "google", "protobuf", "descriptor.proto"));
-
- public String findRootOf(String descriptorFilePath) {
+ private final String descriptorFqn;
+
+ ProtoDescriptorPathFinder() {
+ this(separator);
+ }
+
+ ProtoDescriptorPathFinder(String fileSeparator) {
+ descriptorFqn = concat(separator, asList("", "google", "protobuf", "descriptor.proto"));
+ }
+
+ String findRootOf(String descriptorFilePath) {
if (isEmpty(descriptorFilePath)) return null;
- int indexOfDescriptorFqn = descriptorFilePath.indexOf(DESCRIPTOR_FQN);
+ int indexOfDescriptorFqn = descriptorFilePath.indexOf(descriptorFqn);
if (indexOfDescriptorFqn == -1) {
String format = "Path '%s' does not contain '%s'";
- throw new IllegalArgumentException(String.format(format, descriptorFilePath, DESCRIPTOR_FQN));
+ throw new IllegalArgumentException(String.format(format, descriptorFilePath, descriptorFqn));
}
return descriptorFilePath.substring(0, indexOfDescriptorFqn);
}