Skip to content
Permalink
Browse files
Added unit tests for the protobuf stuff. Basically a port of the com.…
…google.protobuf unit tests. Many tests were omitted since this implemenation does

not support the reflection features.



git-svn-id: https://svn.apache.org/repos/asf/activemq/sandbox/activemq-protobuf@691375 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chirino committed Sep 2, 2008
1 parent 25de73a commit 566e9e1ddf348e490c46c22133a916895cf7e0ff
Showing 6 changed files with 1,181 additions and 0 deletions.
@@ -0,0 +1,53 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Author: kenton@google.com (Kenton Varda)
//
// A proto file which tests the java_multiple_files option.


import "unittest.proto";

package protobuf_unittest;

option java_multiple_files = true;
option java_outer_classname = "MultipleFilesTestProto";

message MessageWithNoOuter {
message NestedMessage {
optional int32 i = 1;
}
enum NestedEnum {
BAZ = 3;
}
optional NestedMessage nested = 1;
repeated TestAllTypes foreign = 2;
optional NestedEnum nested_enum = 3;
optional EnumWithNoOuter foreign_enum = 4;
}

enum EnumWithNoOuter {
FOO = 1;
BAR = 2;
}

service ServiceWithNoOuter {
rpc Foo(MessageWithNoOuter) returns(TestAllTypes);
}

extend TestAllExtensions {
optional int32 extension_with_outer = 1234567;
}
@@ -0,0 +1,116 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.protobuf;

import java.util.Arrays;

import junit.framework.TestCase;
import protobuf_unittest.EnumWithNoOuter;
import protobuf_unittest.MessageWithNoOuter;
import protobuf_unittest.UnittestProto.ForeignEnum;
import protobuf_unittest.UnittestProto.ForeignMessage;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;

/**
* Unit test for generated messages and generated code. See also
* {@link MessageTest}, which tests some generated message functionality.
*
* @author kenton@google.com Kenton Varda
*/
public class GeneratedMessageTest extends TestCase {


public void testAccessors() throws Exception {
TestAllTypes builder = new TestAllTypes();
TestUtil.setAllFields(builder);
TestAllTypes message = builder;
TestUtil.assertAllFieldsSet(message);
}

public void testRepeatedSetters() throws Exception {
TestAllTypes builder = new TestAllTypes();
TestUtil.setAllFields(builder);
TestUtil.modifyRepeatedFields(builder);
TestAllTypes message = builder;
TestUtil.assertRepeatedFieldsModified(message);
}

public void testRepeatedAppend() throws Exception {
TestAllTypes builder = new TestAllTypes();

builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4));
builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ));

ForeignMessage foreignMessage = new ForeignMessage().setC(12);
builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage));

TestAllTypes message = builder;
assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4));
assertEquals(message.getRepeatedForeignEnumList(),
Arrays.asList(ForeignEnum.FOREIGN_BAZ));
assertEquals(1, message.getRepeatedForeignMessageCount());
assertEquals(12, message.getRepeatedForeignMessage(0).getC());
}

public void testSettingForeignMessageUsingBuilder() throws Exception {
TestAllTypes message = new TestAllTypes()
// Pass builder for foreign message instance.
.setOptionalForeignMessage(new ForeignMessage().setC(123))
;
TestAllTypes expectedMessage = new TestAllTypes()
// Create expected version passing foreign message instance explicitly.
.setOptionalForeignMessage(new ForeignMessage().setC(123))
;
// TODO(ngd): Upgrade to using real #equals method once implemented
assertEquals(expectedMessage.toString(), message.toString());
}

public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception {
TestAllTypes message = new TestAllTypes()
// Pass builder for foreign message instance.
.addRepeatedForeignMessage(new ForeignMessage().setC(456))
;
TestAllTypes expectedMessage = new TestAllTypes()
// Create expected version passing foreign message instance explicitly.
.addRepeatedForeignMessage(
new ForeignMessage().setC(456))
;
assertEquals(expectedMessage.toString(), message.toString());
}

public void testDefaults() throws Exception {
TestUtil.assertClear(new TestAllTypes());

assertEquals("\u1234", new TestExtremeDefaultValues().getUtf8String());
}

// =================================================================
// multiple_files_test

public void testMultipleFilesOption() throws Exception {
// We mostly just want to check that things compile.
MessageWithNoOuter message =
new MessageWithNoOuter()
.setNested(new MessageWithNoOuter.NestedMessage().setI(1))
.addForeign(new TestAllTypes().setOptionalInt32(1))
.setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ)
.setForeignEnum(EnumWithNoOuter.BAR)
;
assertEquals(message.toString(), MessageWithNoOuter.parseFrom(message.toByteArray()).toString());
}
}
@@ -0,0 +1,115 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.protobuf;

import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestRequired;
import protobuf_unittest.UnittestProto.TestRequiredForeign;
import protobuf_unittest.UnittestProto.ForeignMessage;

import junit.framework.TestCase;

/**
* Misc. unit tests for message operations that apply to both generated
* and dynamic messages.
*
* @author kenton@google.com Kenton Varda
*/
public class MessageTest extends TestCase {
// =================================================================
// Message-merging tests.

static final TestAllTypes MERGE_SOURCE =
new TestAllTypes()
.setOptionalInt32(1)
.setOptionalString("foo")
.setOptionalForeignMessage(new ForeignMessage())
.addRepeatedString("bar")
;

static final TestAllTypes MERGE_DEST =
new TestAllTypes()
.setOptionalInt64(2)
.setOptionalString("baz")
.setOptionalForeignMessage(new ForeignMessage().setC(3))
.addRepeatedString("qux")
;

static final String MERGE_RESULT_TEXT =
"optional_int32: 1\n" +
"optional_int64: 2\n" +
"optional_string: foo\n" +
"optional_foreign_message {\n" +
" c: 3\n" +
"}\n" +
"repeated_string[0]: qux\n" +
"repeated_string[1]: bar\n";

public void testMergeFrom() throws Exception {
TestAllTypes result =
new TestAllTypes().mergeFrom(MERGE_DEST)
.mergeFrom(MERGE_SOURCE);

assertEquals(MERGE_RESULT_TEXT, result.toString());
}


// =================================================================
// Required-field-related tests.

private static final TestRequired TEST_REQUIRED_UNINITIALIZED =
new TestRequired();
private static final TestRequired TEST_REQUIRED_INITIALIZED =
new TestRequired().setA(1).setB(2).setC(3);

public void testRequired() throws Exception {
TestRequired builder = new TestRequired();

assertFalse(builder.isInitialized());
builder.setA(1);
assertFalse(builder.isInitialized());
builder.setB(1);
assertFalse(builder.isInitialized());
builder.setC(1);
assertTrue(builder.isInitialized());
}

public void testRequiredForeign() throws Exception {
TestRequiredForeign builder = new TestRequiredForeign();

assertTrue(builder.isInitialized());

builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED);
assertFalse(builder.isInitialized());

builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED);
assertTrue(builder.isInitialized());

builder.addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED);
assertFalse(builder.isInitialized());

builder.setRepeatedMessage(0, TEST_REQUIRED_INITIALIZED);
assertTrue(builder.isInitialized());
}


public void testIsInitialized() throws Exception {
assertFalse(new TestRequired().isInitialized());
}

}

0 comments on commit 566e9e1

Please sign in to comment.