Adding more tests.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/model/find/PackageFinder.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/model/find/PackageFinder.java
new file mode 100644
index 0000000..189f121
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/model/find/PackageFinder.java
@@ -0,0 +1,27 @@
+/*
+ * 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.model.find;
+
+import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
+
+import com.google.eclipse.protobuf.protobuf.Package;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public final class PackageFinder {
+
+ public static Package findPackage(Name name, Root root) {
+ for (Package aPackage : getAllContentsOfType(root.value, Package.class))
+ if (name.value.equals(aPackage.getName())) return aPackage;
+ return null;
+ }
+
+ private PackageFinder() {}
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkOnlyOnePackageDefinition_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkOnlyOnePackageDefinition_Test.java
new file mode 100644
index 0000000..4a40ee1
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkOnlyOnePackageDefinition_Test.java
@@ -0,0 +1,64 @@
+/*
+ * 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.validation;
+
+import static com.google.eclipse.protobuf.junit.model.find.Name.name;
+import static com.google.eclipse.protobuf.junit.model.find.PackageFinder.findPackage;
+import static com.google.eclipse.protobuf.junit.model.find.Root.in;
+import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.PACKAGE__NAME;
+import static com.google.eclipse.protobuf.validation.ProtobufJavaValidator.MORE_THAN_ONE_PACKAGE_ERROR;
+import static org.eclipse.xtext.validation.ValidationMessageAcceptor.INSIGNIFICANT_INDEX;
+import static org.mockito.Mockito.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.junit.util.MultiLineTextBuilder;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.eclipse.protobuf.protobuf.Package;
+
+import org.eclipse.xtext.validation.ValidationMessageAcceptor;
+import org.junit.*;
+
+/**
+ * Tests for <code>{@link ProtobufJavaValidator#checkOnlyOnePackageDefinition(Package)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufJavaValidator_checkOnlyOnePackageDefinition_Test {
+
+ @Rule public XtextRule xtext = XtextRule.unitTestSetup();
+
+ private ValidationMessageAcceptor messageAcceptor;
+ private ProtobufJavaValidator validator;
+
+ @Before public void setUp() {
+ messageAcceptor = mock(ValidationMessageAcceptor.class);
+ validator = xtext.getInstanceOf(ProtobufJavaValidator.class);
+ validator.setMessageAcceptor(messageAcceptor);
+ }
+
+ @Test public void should_create_error_if_there_are_more_than_one_package_definitions() {
+ MultiLineTextBuilder proto = new MultiLineTextBuilder();
+ proto.append("package com.google.protobuf;")
+ .append("package com.google.eclipse; ");
+ Protobuf root = xtext.parseText(proto);
+ Package p = findPackage(name("com.google.eclipse"), in(root));
+ validator.checkOnlyOnePackageDefinition(p);
+ String message = "Multiple package definitions.";
+ verify(messageAcceptor).acceptError(message, p, PACKAGE__NAME, INSIGNIFICANT_INDEX, MORE_THAN_ONE_PACKAGE_ERROR);
+ }
+
+ @Test public void should_not_create_error_if_there_is_only_one_package_definition() {
+ MultiLineTextBuilder proto = new MultiLineTextBuilder();
+ proto.append("package com.google.eclipse; ");
+ Protobuf root = xtext.parseText(proto);
+ Package p = findPackage(name("com.google.eclipse"), in(root));
+ validator.checkOnlyOnePackageDefinition(p);
+ verifyZeroInteractions(messageAcceptor);
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsGreaterThanZero_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsGreaterThanZero_Test.java
index 7a01230..21414fb 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsGreaterThanZero_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsGreaterThanZero_Test.java
@@ -43,9 +43,9 @@
@Test public void should_create_error_if_field_index_is_zero() {
MultiLineTextBuilder proto = new MultiLineTextBuilder();
- proto.append("message Person { ");
- proto.append(" optional long id = 0;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional long id = 0;")
+ .append("} ");
Protobuf root = xtext.parseText(proto);
Property p = findProperty(name("id"), in(root));
validator.checkTagNumberIsGreaterThanZero(p);
@@ -55,9 +55,9 @@
@Test public void should_create_error_if_field_index_is_negative() {
MultiLineTextBuilder proto = new MultiLineTextBuilder();
- proto.append("message Person { ");
- proto.append(" optional long id = -1;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional long id = -1;")
+ .append("} ");
Protobuf root = xtext.parseText(proto);
Property p = findProperty(name("id"), in(root));
validator.checkTagNumberIsGreaterThanZero(p);
@@ -67,9 +67,9 @@
@Test public void should_not_create_error_if_field_index_is_greater_than_zero() {
MultiLineTextBuilder proto = new MultiLineTextBuilder();
- proto.append("message Person { ");
- proto.append(" optional long id = 1;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional long id = 1;")
+ .append("} ");
Protobuf root = xtext.parseText(proto);
Property p = findProperty(name("id"), in(root));
validator.checkTagNumberIsGreaterThanZero(p);
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsUnique_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsUnique_Test.java
index 9d33dcd..c0f33bb 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsUnique_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator_checkTagNumberIsUnique_Test.java
@@ -43,10 +43,10 @@
@Test public void should_create_error_if_field_does_not_have_unique_tag_number() {
MultiLineTextBuilder proto = new MultiLineTextBuilder();
- proto.append("message Person { ");
- proto.append(" optional long id = 1; ");
- proto.append(" optional string name = 1;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional long id = 1; ")
+ .append(" optional string name = 1;")
+ .append("} ");
Protobuf root = xtext.parseText(proto);
Property p = findProperty(name("name"), in(root));
validator.checkTagNumberIsUnique(p);
@@ -56,10 +56,10 @@
@Test public void should_not_create_error_if_field_has_unique_tag_number() {
MultiLineTextBuilder proto = new MultiLineTextBuilder();
- proto.append("message Person { ");
- proto.append(" optional long id = 1; ");
- proto.append(" optional string name = 2;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional long id = 1; ")
+ .append(" optional string name = 2;")
+ .append("} ");
Protobuf root = xtext.parseText(proto);
Property p = findProperty(name("name"), in(root));
validator.checkTagNumberIsUnique(p);