1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import com.google.protobuf.Descriptors.Descriptor;
34 import com.google.protobuf.Descriptors.FieldDescriptor;
35 import protobuf_unittest.UnittestProto.TestAllTypes;
36 
37 import junit.framework.TestCase;
38 
39 /**
40  * Test @{link TextFormatParseInfoTree}.
41  */
42 public class TextFormatParseInfoTreeTest extends TestCase {
43 
44   private static final Descriptor DESCRIPTOR = TestAllTypes.getDescriptor();
45   private static final FieldDescriptor OPTIONAL_INT32 =
46       DESCRIPTOR.findFieldByName("optional_int32");
47   private static final FieldDescriptor OPTIONAL_BOOLEAN =
48       DESCRIPTOR.findFieldByName("optional_boolean");
49   private static final FieldDescriptor REPEATED_INT32 =
50       DESCRIPTOR.findFieldByName("repeated_int32");
51   private static final FieldDescriptor OPTIONAL_NESTED_MESSAGE =
52       DESCRIPTOR.findFieldByName("optional_nested_message");
53   private static final FieldDescriptor REPEATED_NESTED_MESSAGE =
54       DESCRIPTOR.findFieldByName("repeated_nested_message");
55   private static final FieldDescriptor FIELD_BB =
56       TestAllTypes.NestedMessage.getDescriptor().findFieldByName("bb");
57 
58   private static final TextFormatParseLocation LOC0 = TextFormatParseLocation.create(1, 2);
59   private static final TextFormatParseLocation LOC1 = TextFormatParseLocation.create(2, 3);
60 
61   private TextFormatParseInfoTree.Builder rootBuilder;
62 
63   @Override
setUp()64   public void setUp() {
65     rootBuilder = TextFormatParseInfoTree.builder();
66   }
67 
testBuildEmptyParseTree()68   public void testBuildEmptyParseTree() {
69     TextFormatParseInfoTree tree = rootBuilder.build();
70     assertTrue(tree.getLocations(null).isEmpty());
71   }
72 
testGetLocationReturnsSingleLocation()73   public void testGetLocationReturnsSingleLocation() {
74     rootBuilder.setLocation(OPTIONAL_INT32, LOC0);
75     TextFormatParseInfoTree root = rootBuilder.build();
76     assertEquals(LOC0, root.getLocation(OPTIONAL_INT32, 0));
77     assertEquals(1, root.getLocations(OPTIONAL_INT32).size());
78     }
79 
testGetLocationsReturnsNoParseLocationsForUnknownField()80   public void testGetLocationsReturnsNoParseLocationsForUnknownField() {
81     assertTrue(rootBuilder.build().getLocations(OPTIONAL_INT32).isEmpty());
82     rootBuilder.setLocation(OPTIONAL_BOOLEAN, LOC0);
83     TextFormatParseInfoTree root = rootBuilder.build();
84     assertTrue(root.getLocations(OPTIONAL_INT32).isEmpty());
85     assertEquals(LOC0, root.getLocations(OPTIONAL_BOOLEAN).get(0));
86   }
87 
testGetLocationThrowsIllegalArgumentExceptionForUnknownField()88   public void testGetLocationThrowsIllegalArgumentExceptionForUnknownField() {
89     rootBuilder.setLocation(REPEATED_INT32, LOC0);
90     TextFormatParseInfoTree root = rootBuilder.build();
91     try {
92       root.getNestedTree(OPTIONAL_INT32, 0);
93       fail("Did not detect unknown field");
94     } catch (IllegalArgumentException expected) {
95       // pass
96     }
97   }
98 
testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex()99   public void testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex() {
100     TextFormatParseInfoTree root = rootBuilder.setLocation(OPTIONAL_INT32, LOC0).build();
101     try {
102       root.getLocation(OPTIONAL_INT32, 1);
103       fail("Invalid index not detected");
104     } catch (IllegalArgumentException expected) {
105       // pass
106     }
107     try {
108       root.getLocation(OPTIONAL_INT32, -1);
109       fail("Negative index not detected");
110     } catch (IllegalArgumentException expected) {
111       // pass
112     }
113   }
114 
testGetLocationsReturnsMultipleLocations()115   public void testGetLocationsReturnsMultipleLocations() {
116     rootBuilder.setLocation(REPEATED_INT32, LOC0);
117     rootBuilder.setLocation(REPEATED_INT32, LOC1);
118     TextFormatParseInfoTree root = rootBuilder.build();
119     assertEquals(LOC0, root.getLocation(REPEATED_INT32, 0));
120     assertEquals(LOC1, root.getLocation(REPEATED_INT32, 1));
121     assertEquals(2, root.getLocations(REPEATED_INT32).size());
122   }
123 
testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField()124   public void testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField() {
125     rootBuilder.setLocation(REPEATED_INT32, LOC0);
126     TextFormatParseInfoTree root = rootBuilder.build();
127     try {
128       root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 0);
129       fail("Did not detect unknown field");
130     } catch (IllegalArgumentException expected) {
131       // pass
132     }
133   }
134 
testGetNestedTreesReturnsNoParseInfoTreesForUnknownField()135   public void testGetNestedTreesReturnsNoParseInfoTreesForUnknownField() {
136     rootBuilder.setLocation(REPEATED_INT32, LOC0);
137     TextFormatParseInfoTree root = rootBuilder.build();
138     assertTrue(root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).isEmpty());
139   }
140 
testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex()141   public void testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex() {
142     rootBuilder.setLocation(REPEATED_INT32, LOC0);
143     rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE);
144     TextFormatParseInfoTree root = rootBuilder.build();
145     try {
146       root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 1);
147       fail("Submessage index that is too large not detected");
148     } catch (IllegalArgumentException expected) {
149       // pass
150     }
151     try {
152       rootBuilder.build().getNestedTree(OPTIONAL_NESTED_MESSAGE, -1);
153       fail("Invalid submessage index (-1) not detected");
154     } catch (IllegalArgumentException expected) {
155       // pass
156     }
157   }
158 
testGetNestedTreesReturnsSingleTree()159   public void testGetNestedTreesReturnsSingleTree() {
160     rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE);
161     TextFormatParseInfoTree root = rootBuilder.build();
162     assertEquals(1, root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).size());
163     TextFormatParseInfoTree subtree = root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).get(0);
164     assertNotNull(subtree);
165   }
166 
testGetNestedTreesReturnsMultipleTrees()167   public void testGetNestedTreesReturnsMultipleTrees() {
168     TextFormatParseInfoTree.Builder subtree1Builder =
169         rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE);
170     subtree1Builder.getBuilderForSubMessageField(FIELD_BB);
171     subtree1Builder.getBuilderForSubMessageField(FIELD_BB);
172     TextFormatParseInfoTree.Builder subtree2Builder =
173         rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE);
174     subtree2Builder.getBuilderForSubMessageField(FIELD_BB);
175     TextFormatParseInfoTree root = rootBuilder.build();
176     assertEquals(2, root.getNestedTrees(REPEATED_NESTED_MESSAGE).size());
177     assertEquals(
178         2, root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(0).getNestedTrees(FIELD_BB).size());
179     assertEquals(
180         1, root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(1).getNestedTrees(FIELD_BB).size());
181   }
182 }
183