OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 23 matching lines...) Expand all Loading... |
34 import protobuf_unittest.UnittestProto.NestedTestAllTypes; | 34 import protobuf_unittest.UnittestProto.NestedTestAllTypes; |
35 import protobuf_unittest.UnittestProto.TestAllTypes; | 35 import protobuf_unittest.UnittestProto.TestAllTypes; |
36 | 36 |
37 import junit.framework.TestCase; | 37 import junit.framework.TestCase; |
38 | 38 |
39 /** Unit tests for {@link FieldMaskUtil}. */ | 39 /** Unit tests for {@link FieldMaskUtil}. */ |
40 public class FieldMaskUtilTest extends TestCase { | 40 public class FieldMaskUtilTest extends TestCase { |
41 public void testIsValid() throws Exception { | 41 public void testIsValid() throws Exception { |
42 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload")); | 42 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload")); |
43 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "nonexist")); | 43 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "nonexist")); |
44 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional
_int32")); | 44 assertTrue(FieldMaskUtil.isValid( |
45 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated
_int32")); | 45 NestedTestAllTypes.class, "payload.optional_int32")); |
46 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional
_nested_message")); | 46 assertTrue(FieldMaskUtil.isValid( |
47 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated
_nested_message")); | 47 NestedTestAllTypes.class, "payload.repeated_int32")); |
48 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.nonexis
t")); | 48 assertTrue(FieldMaskUtil.isValid( |
49 | 49 NestedTestAllTypes.class, "payload.optional_nested_message")); |
50 assertTrue( | 50 assertTrue(FieldMaskUtil.isValid( |
51 FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString
("payload"))); | 51 NestedTestAllTypes.class, "payload.repeated_nested_message")); |
52 assertFalse( | 52 assertFalse(FieldMaskUtil.isValid( |
53 FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString
("nonexist"))); | 53 NestedTestAllTypes.class, "payload.nonexist")); |
54 assertFalse( | 54 |
55 FieldMaskUtil.isValid( | 55 assertTrue(FieldMaskUtil.isValid( |
56 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist
"))); | 56 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload"))); |
57 | 57 assertFalse(FieldMaskUtil.isValid( |
| 58 NestedTestAllTypes.class, FieldMaskUtil.fromString("nonexist"))); |
| 59 assertFalse(FieldMaskUtil.isValid( |
| 60 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist")))
; |
| 61 |
58 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "payloa
d")); | 62 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "payloa
d")); |
59 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "nonex
ist")); | 63 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "nonex
ist")); |
60 | 64 |
61 assertTrue( | 65 assertTrue(FieldMaskUtil.isValid( |
62 FieldMaskUtil.isValid( | 66 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payload"))
); |
63 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payloa
d"))); | 67 assertFalse(FieldMaskUtil.isValid( |
64 assertFalse( | 68 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexist")
)); |
65 FieldMaskUtil.isValid( | 69 |
66 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexi
st"))); | 70 assertTrue(FieldMaskUtil.isValid( |
67 | 71 NestedTestAllTypes.class, "payload.optional_nested_message.bb")); |
68 assertTrue( | |
69 FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_nested
_message.bb")); | |
70 // Repeated fields cannot have sub-paths. | 72 // Repeated fields cannot have sub-paths. |
71 assertFalse( | 73 assertFalse(FieldMaskUtil.isValid( |
72 FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated_nested
_message.bb")); | 74 NestedTestAllTypes.class, "payload.repeated_nested_message.bb")); |
73 // Non-message fields cannot have sub-paths. | 75 // Non-message fields cannot have sub-paths. |
74 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optiona
l_int32.bb")); | 76 assertFalse(FieldMaskUtil.isValid( |
| 77 NestedTestAllTypes.class, "payload.optional_int32.bb")); |
75 } | 78 } |
76 | 79 |
77 public void testToString() throws Exception { | 80 public void testToString() throws Exception { |
78 assertEquals("", FieldMaskUtil.toString(FieldMask.getDefaultInstance())); | 81 assertEquals("", FieldMaskUtil.toString(FieldMask.getDefaultInstance())); |
79 FieldMask mask = FieldMask.newBuilder().addPaths("foo").build(); | 82 FieldMask mask = FieldMask.newBuilder().addPaths("foo").build(); |
80 assertEquals("foo", FieldMaskUtil.toString(mask)); | 83 assertEquals("foo", FieldMaskUtil.toString(mask)); |
81 mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar").build(); | 84 mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar").build(); |
82 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); | 85 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); |
83 | 86 |
84 // Empty field paths are ignored. | 87 // Empty field paths are ignored. |
85 mask = | 88 mask = FieldMask.newBuilder().addPaths("").addPaths("foo").addPaths(""). |
86 FieldMask.newBuilder() | 89 addPaths("bar").addPaths("").build(); |
87 .addPaths("") | |
88 .addPaths("foo") | |
89 .addPaths("") | |
90 .addPaths("bar") | |
91 .addPaths("") | |
92 .build(); | |
93 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); | 90 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); |
94 } | 91 } |
95 | 92 |
96 public void testFromString() throws Exception { | 93 public void testFromString() throws Exception { |
97 FieldMask mask = FieldMaskUtil.fromString(""); | 94 FieldMask mask = FieldMaskUtil.fromString(""); |
98 assertEquals(0, mask.getPathsCount()); | 95 assertEquals(0, mask.getPathsCount()); |
99 mask = FieldMaskUtil.fromString("foo"); | 96 mask = FieldMaskUtil.fromString("foo"); |
100 assertEquals(1, mask.getPathsCount()); | 97 assertEquals(1, mask.getPathsCount()); |
101 assertEquals("foo", mask.getPaths(0)); | 98 assertEquals("foo", mask.getPaths(0)); |
102 mask = FieldMaskUtil.fromString("foo,bar.baz"); | 99 mask = FieldMaskUtil.fromString("foo,bar.baz"); |
103 assertEquals(2, mask.getPathsCount()); | 100 assertEquals(2, mask.getPathsCount()); |
104 assertEquals("foo", mask.getPaths(0)); | 101 assertEquals("foo", mask.getPaths(0)); |
105 assertEquals("bar.baz", mask.getPaths(1)); | 102 assertEquals("bar.baz", mask.getPaths(1)); |
106 | 103 |
107 // Empty field paths are ignore. | 104 // Empty field paths are ignore. |
108 mask = FieldMaskUtil.fromString(",foo,,bar,"); | 105 mask = FieldMaskUtil.fromString(",foo,,bar,"); |
109 assertEquals(2, mask.getPathsCount()); | 106 assertEquals(2, mask.getPathsCount()); |
110 assertEquals("foo", mask.getPaths(0)); | 107 assertEquals("foo", mask.getPaths(0)); |
111 assertEquals("bar", mask.getPaths(1)); | 108 assertEquals("bar", mask.getPaths(1)); |
112 | 109 |
113 // Check whether the field paths are valid if a class parameter is provided. | 110 // Check whether the field paths are valid if a class parameter is provided. |
114 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, ",payload"); | 111 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, ",payload"); |
115 | 112 |
116 try { | 113 try { |
117 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, "payload,nonexis
t"); | 114 mask = FieldMaskUtil.fromString( |
| 115 NestedTestAllTypes.class, "payload,nonexist"); |
118 fail("Exception is expected."); | 116 fail("Exception is expected."); |
119 } catch (IllegalArgumentException e) { | 117 } catch (IllegalArgumentException e) { |
120 // Expected. | 118 // Expected. |
121 } | 119 } |
122 } | 120 } |
123 | 121 |
124 public void testFromFieldNumbers() throws Exception { | 122 public void testFromFieldNumbers() throws Exception { |
125 FieldMask mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class); | 123 FieldMask mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class); |
126 assertEquals(0, mask.getPathsCount()); | 124 assertEquals(0, mask.getPathsCount()); |
127 mask = | 125 mask = |
(...skipping 10 matching lines...) Expand all Loading... |
138 assertEquals("optional_int32", mask.getPaths(0)); | 136 assertEquals("optional_int32", mask.getPaths(0)); |
139 assertEquals("optional_int64", mask.getPaths(1)); | 137 assertEquals("optional_int64", mask.getPaths(1)); |
140 | 138 |
141 try { | 139 try { |
142 int invalidFieldNumber = 1000; | 140 int invalidFieldNumber = 1000; |
143 mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class, invalidFieldNumb
er); | 141 mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class, invalidFieldNumb
er); |
144 fail("Exception is expected."); | 142 fail("Exception is expected."); |
145 } catch (IllegalArgumentException expected) { | 143 } catch (IllegalArgumentException expected) { |
146 } | 144 } |
147 } | 145 } |
148 | 146 |
149 public void testToJsonString() throws Exception { | |
150 FieldMask mask = FieldMask.getDefaultInstance(); | |
151 assertEquals("", FieldMaskUtil.toJsonString(mask)); | |
152 mask = FieldMask.newBuilder().addPaths("foo").build(); | |
153 assertEquals("foo", FieldMaskUtil.toJsonString(mask)); | |
154 mask = FieldMask.newBuilder().addPaths("foo.bar_baz").addPaths("").build(); | |
155 assertEquals("foo.barBaz", FieldMaskUtil.toJsonString(mask)); | |
156 mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar_baz").build(); | |
157 assertEquals("foo,barBaz", FieldMaskUtil.toJsonString(mask)); | |
158 } | |
159 | |
160 public void testFromJsonString() throws Exception { | |
161 FieldMask mask = FieldMaskUtil.fromJsonString(""); | |
162 assertEquals(0, mask.getPathsCount()); | |
163 mask = FieldMaskUtil.fromJsonString("foo"); | |
164 assertEquals(1, mask.getPathsCount()); | |
165 assertEquals("foo", mask.getPaths(0)); | |
166 mask = FieldMaskUtil.fromJsonString("foo.barBaz"); | |
167 assertEquals(1, mask.getPathsCount()); | |
168 assertEquals("foo.bar_baz", mask.getPaths(0)); | |
169 mask = FieldMaskUtil.fromJsonString("foo,barBaz"); | |
170 assertEquals(2, mask.getPathsCount()); | |
171 assertEquals("foo", mask.getPaths(0)); | |
172 assertEquals("bar_baz", mask.getPaths(1)); | |
173 } | |
174 | |
175 public void testUnion() throws Exception { | 147 public void testUnion() throws Exception { |
176 // Only test a simple case here and expect | 148 // Only test a simple case here and expect |
177 // {@link FieldMaskTreeTest#testAddFieldPath} to cover all scenarios. | 149 // {@link FieldMaskTreeTest#testAddFieldPath} to cover all scenarios. |
178 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); | 150 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); |
179 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); | 151 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); |
180 FieldMask result = FieldMaskUtil.union(mask1, mask2); | 152 FieldMask result = FieldMaskUtil.union(mask1, mask2); |
181 assertEquals("bar,foo", FieldMaskUtil.toString(result)); | 153 assertEquals("bar,foo", FieldMaskUtil.toString(result)); |
182 } | 154 } |
183 | 155 |
184 public void testUnion_usingVarArgs() throws Exception { | 156 public void testUnion_usingVarArgs() throws Exception { |
185 FieldMask mask1 = FieldMaskUtil.fromString("foo"); | 157 FieldMask mask1 = FieldMaskUtil.fromString("foo"); |
186 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar.quz"); | 158 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar.quz"); |
187 FieldMask mask3 = FieldMaskUtil.fromString("bar.quz"); | 159 FieldMask mask3 = FieldMaskUtil.fromString("bar.quz"); |
188 FieldMask mask4 = FieldMaskUtil.fromString("bar"); | 160 FieldMask mask4 = FieldMaskUtil.fromString("bar"); |
189 FieldMask result = FieldMaskUtil.union(mask1, mask2, mask3, mask4); | 161 FieldMask result = FieldMaskUtil.union(mask1, mask2, mask3, mask4); |
190 assertEquals("bar,foo", FieldMaskUtil.toString(result)); | 162 assertEquals("bar,foo", FieldMaskUtil.toString(result)); |
191 } | 163 } |
192 | 164 |
193 public void testIntersection() throws Exception { | 165 public void testIntersection() throws Exception { |
194 // Only test a simple case here and expect | 166 // Only test a simple case here and expect |
195 // {@link FieldMaskTreeTest#testIntersectFieldPath} to cover all scenarios. | 167 // {@link FieldMaskTreeTest#testIntersectFieldPath} to cover all scenarios. |
196 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); | 168 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); |
197 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); | 169 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); |
198 FieldMask result = FieldMaskUtil.intersection(mask1, mask2); | 170 FieldMask result = FieldMaskUtil.intersection(mask1, mask2); |
199 assertEquals("bar.baz,bar.quz,foo.bar", FieldMaskUtil.toString(result)); | 171 assertEquals("bar.baz,bar.quz,foo.bar", FieldMaskUtil.toString(result)); |
200 } | 172 } |
201 | 173 |
202 public void testMerge() throws Exception { | 174 public void testMerge() throws Exception { |
203 // Only test a simple case here and expect | 175 // Only test a simple case here and expect |
204 // {@link FieldMaskTreeTest#testMerge} to cover all scenarios. | 176 // {@link FieldMaskTreeTest#testMerge} to cover all scenarios. |
205 NestedTestAllTypes source = | 177 NestedTestAllTypes source = NestedTestAllTypes.newBuilder() |
206 NestedTestAllTypes.newBuilder() | 178 .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) |
207 .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) | 179 .build(); |
208 .build(); | |
209 NestedTestAllTypes.Builder builder = NestedTestAllTypes.newBuilder(); | 180 NestedTestAllTypes.Builder builder = NestedTestAllTypes.newBuilder(); |
210 FieldMaskUtil.merge(FieldMaskUtil.fromString("payload"), source, builder); | 181 FieldMaskUtil.merge(FieldMaskUtil.fromString("payload"), source, builder); |
211 assertEquals(1234, builder.getPayload().getOptionalInt32()); | 182 assertEquals(1234, builder.getPayload().getOptionalInt32()); |
212 } | 183 } |
213 } | 184 } |
OLD | NEW |