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( | 44 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional
_int32")); |
45 NestedTestAllTypes.class, "payload.optional_int32")); | 45 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated
_int32")); |
46 assertTrue(FieldMaskUtil.isValid( | 46 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional
_nested_message")); |
47 NestedTestAllTypes.class, "payload.repeated_int32")); | 47 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated
_nested_message")); |
48 assertTrue(FieldMaskUtil.isValid( | 48 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.nonexis
t")); |
49 NestedTestAllTypes.class, "payload.optional_nested_message")); | 49 |
50 assertTrue(FieldMaskUtil.isValid( | 50 assertTrue( |
51 NestedTestAllTypes.class, "payload.repeated_nested_message")); | 51 FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString
("payload"))); |
52 assertFalse(FieldMaskUtil.isValid( | 52 assertFalse( |
53 NestedTestAllTypes.class, "payload.nonexist")); | 53 FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString
("nonexist"))); |
54 | 54 assertFalse( |
55 assertTrue(FieldMaskUtil.isValid( | 55 FieldMaskUtil.isValid( |
56 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload"))); | 56 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist
"))); |
57 assertFalse(FieldMaskUtil.isValid( | 57 |
58 NestedTestAllTypes.class, FieldMaskUtil.fromString("nonexist"))); | |
59 assertFalse(FieldMaskUtil.isValid( | |
60 NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist")))
; | |
61 | |
62 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "payloa
d")); | 58 assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "payloa
d")); |
63 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "nonex
ist")); | 59 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "nonex
ist")); |
64 | 60 |
65 assertTrue(FieldMaskUtil.isValid( | 61 assertTrue( |
66 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payload"))
); | 62 FieldMaskUtil.isValid( |
67 assertFalse(FieldMaskUtil.isValid( | 63 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payloa
d"))); |
68 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexist")
)); | 64 assertFalse( |
69 | 65 FieldMaskUtil.isValid( |
70 assertTrue(FieldMaskUtil.isValid( | 66 NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexi
st"))); |
71 NestedTestAllTypes.class, "payload.optional_nested_message.bb")); | 67 |
| 68 assertTrue( |
| 69 FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_nested
_message.bb")); |
72 // Repeated fields cannot have sub-paths. | 70 // Repeated fields cannot have sub-paths. |
73 assertFalse(FieldMaskUtil.isValid( | 71 assertFalse( |
74 NestedTestAllTypes.class, "payload.repeated_nested_message.bb")); | 72 FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated_nested
_message.bb")); |
75 // Non-message fields cannot have sub-paths. | 73 // Non-message fields cannot have sub-paths. |
76 assertFalse(FieldMaskUtil.isValid( | 74 assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optiona
l_int32.bb")); |
77 NestedTestAllTypes.class, "payload.optional_int32.bb")); | |
78 } | 75 } |
79 | 76 |
80 public void testToString() throws Exception { | 77 public void testToString() throws Exception { |
81 assertEquals("", FieldMaskUtil.toString(FieldMask.getDefaultInstance())); | 78 assertEquals("", FieldMaskUtil.toString(FieldMask.getDefaultInstance())); |
82 FieldMask mask = FieldMask.newBuilder().addPaths("foo").build(); | 79 FieldMask mask = FieldMask.newBuilder().addPaths("foo").build(); |
83 assertEquals("foo", FieldMaskUtil.toString(mask)); | 80 assertEquals("foo", FieldMaskUtil.toString(mask)); |
84 mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar").build(); | 81 mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar").build(); |
85 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); | 82 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); |
86 | 83 |
87 // Empty field paths are ignored. | 84 // Empty field paths are ignored. |
88 mask = FieldMask.newBuilder().addPaths("").addPaths("foo").addPaths(""). | 85 mask = |
89 addPaths("bar").addPaths("").build(); | 86 FieldMask.newBuilder() |
| 87 .addPaths("") |
| 88 .addPaths("foo") |
| 89 .addPaths("") |
| 90 .addPaths("bar") |
| 91 .addPaths("") |
| 92 .build(); |
90 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); | 93 assertEquals("foo,bar", FieldMaskUtil.toString(mask)); |
91 } | 94 } |
92 | 95 |
93 public void testFromString() throws Exception { | 96 public void testFromString() throws Exception { |
94 FieldMask mask = FieldMaskUtil.fromString(""); | 97 FieldMask mask = FieldMaskUtil.fromString(""); |
95 assertEquals(0, mask.getPathsCount()); | 98 assertEquals(0, mask.getPathsCount()); |
96 mask = FieldMaskUtil.fromString("foo"); | 99 mask = FieldMaskUtil.fromString("foo"); |
97 assertEquals(1, mask.getPathsCount()); | 100 assertEquals(1, mask.getPathsCount()); |
98 assertEquals("foo", mask.getPaths(0)); | 101 assertEquals("foo", mask.getPaths(0)); |
99 mask = FieldMaskUtil.fromString("foo,bar.baz"); | 102 mask = FieldMaskUtil.fromString("foo,bar.baz"); |
100 assertEquals(2, mask.getPathsCount()); | 103 assertEquals(2, mask.getPathsCount()); |
101 assertEquals("foo", mask.getPaths(0)); | 104 assertEquals("foo", mask.getPaths(0)); |
102 assertEquals("bar.baz", mask.getPaths(1)); | 105 assertEquals("bar.baz", mask.getPaths(1)); |
103 | 106 |
104 // Empty field paths are ignore. | 107 // Empty field paths are ignore. |
105 mask = FieldMaskUtil.fromString(",foo,,bar,"); | 108 mask = FieldMaskUtil.fromString(",foo,,bar,"); |
106 assertEquals(2, mask.getPathsCount()); | 109 assertEquals(2, mask.getPathsCount()); |
107 assertEquals("foo", mask.getPaths(0)); | 110 assertEquals("foo", mask.getPaths(0)); |
108 assertEquals("bar", mask.getPaths(1)); | 111 assertEquals("bar", mask.getPaths(1)); |
109 | 112 |
110 // Check whether the field paths are valid if a class parameter is provided. | 113 // Check whether the field paths are valid if a class parameter is provided. |
111 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, ",payload"); | 114 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, ",payload"); |
112 | 115 |
113 try { | 116 try { |
114 mask = FieldMaskUtil.fromString( | 117 mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, "payload,nonexis
t"); |
115 NestedTestAllTypes.class, "payload,nonexist"); | |
116 fail("Exception is expected."); | 118 fail("Exception is expected."); |
117 } catch (IllegalArgumentException e) { | 119 } catch (IllegalArgumentException e) { |
118 // Expected. | 120 // Expected. |
119 } | 121 } |
120 } | 122 } |
121 | 123 |
122 public void testFromFieldNumbers() throws Exception { | 124 public void testFromFieldNumbers() throws Exception { |
123 FieldMask mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class); | 125 FieldMask mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class); |
124 assertEquals(0, mask.getPathsCount()); | 126 assertEquals(0, mask.getPathsCount()); |
125 mask = | 127 mask = |
(...skipping 10 matching lines...) Expand all Loading... |
136 assertEquals("optional_int32", mask.getPaths(0)); | 138 assertEquals("optional_int32", mask.getPaths(0)); |
137 assertEquals("optional_int64", mask.getPaths(1)); | 139 assertEquals("optional_int64", mask.getPaths(1)); |
138 | 140 |
139 try { | 141 try { |
140 int invalidFieldNumber = 1000; | 142 int invalidFieldNumber = 1000; |
141 mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class, invalidFieldNumb
er); | 143 mask = FieldMaskUtil.fromFieldNumbers(TestAllTypes.class, invalidFieldNumb
er); |
142 fail("Exception is expected."); | 144 fail("Exception is expected."); |
143 } catch (IllegalArgumentException expected) { | 145 } catch (IllegalArgumentException expected) { |
144 } | 146 } |
145 } | 147 } |
146 | 148 |
| 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 |
147 public void testUnion() throws Exception { | 175 public void testUnion() throws Exception { |
148 // Only test a simple case here and expect | 176 // Only test a simple case here and expect |
149 // {@link FieldMaskTreeTest#testAddFieldPath} to cover all scenarios. | 177 // {@link FieldMaskTreeTest#testAddFieldPath} to cover all scenarios. |
150 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); | 178 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); |
151 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); | 179 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); |
152 FieldMask result = FieldMaskUtil.union(mask1, mask2); | 180 FieldMask result = FieldMaskUtil.union(mask1, mask2); |
153 assertEquals("bar,foo", FieldMaskUtil.toString(result)); | 181 assertEquals("bar,foo", FieldMaskUtil.toString(result)); |
154 } | 182 } |
155 | 183 |
156 public void testUnion_usingVarArgs() throws Exception { | 184 public void testUnion_usingVarArgs() throws Exception { |
157 FieldMask mask1 = FieldMaskUtil.fromString("foo"); | 185 FieldMask mask1 = FieldMaskUtil.fromString("foo"); |
158 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar.quz"); | 186 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar.quz"); |
159 FieldMask mask3 = FieldMaskUtil.fromString("bar.quz"); | 187 FieldMask mask3 = FieldMaskUtil.fromString("bar.quz"); |
160 FieldMask mask4 = FieldMaskUtil.fromString("bar"); | 188 FieldMask mask4 = FieldMaskUtil.fromString("bar"); |
161 FieldMask result = FieldMaskUtil.union(mask1, mask2, mask3, mask4); | 189 FieldMask result = FieldMaskUtil.union(mask1, mask2, mask3, mask4); |
162 assertEquals("bar,foo", FieldMaskUtil.toString(result)); | 190 assertEquals("bar,foo", FieldMaskUtil.toString(result)); |
163 } | 191 } |
164 | 192 |
165 public void testIntersection() throws Exception { | 193 public void testIntersection() throws Exception { |
166 // Only test a simple case here and expect | 194 // Only test a simple case here and expect |
167 // {@link FieldMaskTreeTest#testIntersectFieldPath} to cover all scenarios. | 195 // {@link FieldMaskTreeTest#testIntersectFieldPath} to cover all scenarios. |
168 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); | 196 FieldMask mask1 = FieldMaskUtil.fromString("foo,bar.baz,bar.quz"); |
169 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); | 197 FieldMask mask2 = FieldMaskUtil.fromString("foo.bar,bar"); |
170 FieldMask result = FieldMaskUtil.intersection(mask1, mask2); | 198 FieldMask result = FieldMaskUtil.intersection(mask1, mask2); |
171 assertEquals("bar.baz,bar.quz,foo.bar", FieldMaskUtil.toString(result)); | 199 assertEquals("bar.baz,bar.quz,foo.bar", FieldMaskUtil.toString(result)); |
172 } | 200 } |
173 | 201 |
174 public void testMerge() throws Exception { | 202 public void testMerge() throws Exception { |
175 // Only test a simple case here and expect | 203 // Only test a simple case here and expect |
176 // {@link FieldMaskTreeTest#testMerge} to cover all scenarios. | 204 // {@link FieldMaskTreeTest#testMerge} to cover all scenarios. |
177 NestedTestAllTypes source = NestedTestAllTypes.newBuilder() | 205 NestedTestAllTypes source = |
178 .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) | 206 NestedTestAllTypes.newBuilder() |
179 .build(); | 207 .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) |
| 208 .build(); |
180 NestedTestAllTypes.Builder builder = NestedTestAllTypes.newBuilder(); | 209 NestedTestAllTypes.Builder builder = NestedTestAllTypes.newBuilder(); |
181 FieldMaskUtil.merge(FieldMaskUtil.fromString("payload"), source, builder); | 210 FieldMaskUtil.merge(FieldMaskUtil.fromString("payload"), source, builder); |
182 assertEquals(1234, builder.getPayload().getOptionalInt32()); | 211 assertEquals(1234, builder.getPayload().getOptionalInt32()); |
183 } | 212 } |
184 } | 213 } |
OLD | NEW |