| 1 |
// Protocol Buffers - Google's data interchange format |
| 2 |
// Copyright 2008 Google Inc. |
| 3 |
// http://code.google.com/p/protobuf/ |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
package com.google.protobuf; |
| 18 |
|
| 19 |
import protobuf_unittest.UnittestProto.TestAllTypes; |
| 20 |
import protobuf_unittest.UnittestProto.TestAllExtensions; |
| 21 |
import protobuf_unittest.UnittestProto.TestRequired; |
| 22 |
import protobuf_unittest.UnittestProto.TestRequiredForeign; |
| 23 |
import protobuf_unittest.UnittestProto.ForeignMessage; |
| 24 |
|
| 25 |
import junit.framework.TestCase; |
| 26 |
|
| 27 |
/** |
| 28 |
* Misc. unit tests for message operations that apply to both generated |
| 29 |
* and dynamic messages. |
| 30 |
* |
| 31 |
* @author [email protected] Kenton Varda |
| 32 |
*/ |
| 33 |
public class MessageTest extends TestCase { |
| 34 |
// ================================================================= |
| 35 |
// Message-merging tests. |
| 36 |
|
| 37 |
static final TestAllTypes MERGE_SOURCE = |
| 38 |
new TestAllTypes() |
| 39 |
.setOptionalInt32(1) |
| 40 |
.setOptionalString("foo") |
| 41 |
.setOptionalForeignMessage(new ForeignMessage()) |
| 42 |
.addRepeatedString("bar") |
| 43 |
; |
| 44 |
|
| 45 |
static final TestAllTypes MERGE_DEST = |
| 46 |
new TestAllTypes() |
| 47 |
.setOptionalInt64(2) |
| 48 |
.setOptionalString("baz") |
| 49 |
.setOptionalForeignMessage(new ForeignMessage().setC(3)) |
| 50 |
.addRepeatedString("qux") |
| 51 |
; |
| 52 |
|
| 53 |
static final String MERGE_RESULT_TEXT = |
| 54 |
"optional_int32: 1\n" + |
| 55 |
"optional_int64: 2\n" + |
| 56 |
"optional_string: foo\n" + |
| 57 |
"optional_foreign_message {\n" + |
| 58 |
" c: 3\n" + |
| 59 |
"}\n" + |
| 60 |
"repeated_string[0]: qux\n" + |
| 61 |
"repeated_string[1]: bar\n"; |
| 62 |
|
| 63 |
public void testMergeFrom() throws Exception { |
| 64 |
TestAllTypes result = |
| 65 |
new TestAllTypes().mergeFrom(MERGE_DEST) |
| 66 |
.mergeFrom(MERGE_SOURCE); |
| 67 |
|
| 68 |
assertEquals(MERGE_RESULT_TEXT, result.toString()); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
// ================================================================= |
| 73 |
// Required-field-related tests. |
| 74 |
|
| 75 |
private static final TestRequired TEST_REQUIRED_UNINITIALIZED = |
| 76 |
new TestRequired(); |
| 77 |
private static final TestRequired TEST_REQUIRED_INITIALIZED = |
| 78 |
new TestRequired().setA(1).setB(2).setC(3); |
| 79 |
|
| 80 |
public void testRequired() throws Exception { |
| 81 |
TestRequired builder = new TestRequired(); |
| 82 |
|
| 83 |
assertFalse(builder.isInitialized()); |
| 84 |
builder.setA(1); |
| 85 |
assertFalse(builder.isInitialized()); |
| 86 |
builder.setB(1); |
| 87 |
assertFalse(builder.isInitialized()); |
| 88 |
builder.setC(1); |
| 89 |
assertTrue(builder.isInitialized()); |
| 90 |
} |
| 91 |
|
| 92 |
public void testRequiredForeign() throws Exception { |
| 93 |
TestRequiredForeign builder = new TestRequiredForeign(); |
| 94 |
|
| 95 |
assertTrue(builder.isInitialized()); |
| 96 |
|
| 97 |
builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED); |
| 98 |
assertFalse(builder.isInitialized()); |
| 99 |
|
| 100 |
builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED); |
| 101 |
assertTrue(builder.isInitialized()); |
| 102 |
|
| 103 |
builder.addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED); |
| 104 |
assertFalse(builder.isInitialized()); |
| 105 |
|
| 106 |
builder.setRepeatedMessage(0, TEST_REQUIRED_INITIALIZED); |
| 107 |
assertTrue(builder.isInitialized()); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
public void testIsInitialized() throws Exception { |
| 112 |
assertFalse(new TestRequired().isInitialized()); |
| 113 |
} |
| 114 |
|
| 115 |
} |