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 import junit.framework.TestCase;
37 
38 /** Test @{link TextFormatParseInfoTree}. */
39 public class TextFormatParseInfoTreeTest extends TestCase {
40 
41   private static final Descriptor DESCRIPTOR = TestAllTypes.getDescriptor();
42   private static final FieldDescriptor OPTIONAL_INT32 =
43       DESCRIPTOR.findFieldByName("optional_int32");
44   private static final FieldDescriptor OPTIONAL_BOOLEAN =
45       DESCRIPTOR.findFieldByName("optional_boolean");
46   private static final FieldDescriptor REPEATED_INT32 =
47       DESCRIPTOR.findFieldByName("repeated_int32");
48   private static final FieldDescriptor OPTIONAL_NESTED_MESSAGE =
49       DESCRIPTOR.findFieldByName("optional_nested_message");
50   private static final FieldDescriptor REPEATED_NESTED_MESSAGE =
51       DESCRIPTOR.findFieldByName("repeated_nested_message");
52   private static final FieldDescriptor FIELD_BB =
53       TestAllTypes.NestedMessage.getDescriptor().findFieldByName("bb");
54 
55   private static final TextFormatParseLocation LOC0 = TextFormatParseLocation.create(1, 2);
56   private static final TextFormatParseLocation LOC1 = TextFormatParseLocation.create(2, 3);
57 
58   private TextFormatParseInfoTree.Builder rootBuilder;
59 
60   @Override
setUp()61   public void setUp() {
62     rootBuilder = TextFormatParseInfoTree.builder();
63   }
64 
testBuildEmptyParseTree()65   public void testBuildEmptyParseTree() {
66     TextFormatParseInfoTree tree = rootBuilder.build();
67     assertTrue(tree.getLocations(null).isEmpty());
68   }
69 
testGetLocationReturnsSingleLocation()70   public void testGetLocationReturnsSingleLocation() {
71     rootBuilder.setLocation(OPTIONAL_INT32, LOC0);
72     TextFormatParseInfoTree root = rootBuilder.build();
73     assertEquals(LOC0, root.getLocation(OPTIONAL_INT32, 0));
74     assertEquals(1, root.getLocations(OPTIONAL_INT32).size());
75   }
76 
testGetLocationsReturnsNoParseLocationsForUnknownField()77   public void testGetLocationsReturnsNoParseLocationsForUnknownField() {
78     assertTrue(rootBuilder.build().getLocations(OPTIONAL_INT32).isEmpty());
79     rootBuilder.setLocation(OPTIONAL_BOOLEAN, LOC0);
80     TextFormatParseInfoTree root = rootBuilder.build();
81     assertTrue(root.getLocations(OPTIONAL_INT32).isEmpty());
82     assertEquals(LOC0, root.getLocations(OPTIONAL_BOOLEAN).get(0));
83   }
84 
testGetLocationThrowsIllegalArgumentExceptionForUnknownField()85   public void testGetLocationThrowsIllegalArgumentExceptionForUnknownField() {
86     rootBuilder.setLocation(REPEATED_INT32, LOC0);
87     TextFormatParseInfoTree root = rootBuilder.build();
88     try {
89       root.getNestedTree(OPTIONAL_INT32, 0);
90       fail("Did not detect unknown field");
91     } catch (IllegalArgumentException expected) {
92       // pass
93     }
94   }
95 
testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex()96   public void testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex() {
97     TextFormatParseInfoTree root = rootBuilder.setLocation(OPTIONAL_INT32, LOC0).build();
98     try {
99       root.getLocation(OPTIONAL_INT32, 1);
100       fail("Invalid index not detected");
101     } catch (IllegalArgumentException expected) {
102       // pass
103     }
104     try {
105       root.getLocation(OPTIONAL_INT32, -1);
106       fail("Negative index not detected");
107     } catch (IllegalArgumentException expected) {
108       // pass
109     }
110   }
111 
testGetLocationsReturnsMultipleLocations()112   public void testGetLocationsReturnsMultipleLocations() {
113     rootBuilder.setLocation(REPEATED_INT32, LOC0);
114     rootBuilder.setLocation(REPEATED_INT32, LOC1);
115     TextFormatParseInfoTree root = rootBuilder.build();
116     assertEquals(LOC0, root.getLocation(REPEATED_INT32, 0));
117     assertEquals(LOC1, root.getLocation(REPEATED_INT32, 1));
118     assertEquals(2, root.getLocations(REPEATED_INT32).size());
119   }
120 
testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField()121   public void testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField() {
122     rootBuilder.setLocation(REPEATED_INT32, LOC0);
123     TextFormatParseInfoTree root = rootBuilder.build();
124     try {
125       root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 0);
126       fail("Did not detect unknown field");
127     } catch (IllegalArgumentException expected) {
128       // pass
129     }
130   }
131 
testGetNestedTreesReturnsNoParseInfoTreesForUnknownField()132   public void testGetNestedTreesReturnsNoParseInfoTreesForUnknownField() {
133     rootBuilder.setLocation(REPEATED_INT32, LOC0);
134     TextFormatParseInfoTree root = rootBuilder.build();
135     assertTrue(root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).isEmpty());
136   }
137 
testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex()138   public void testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex() {
139     rootBuilder.setLocation(REPEATED_INT32, LOC0);
140     rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE);
141     TextFormatParseInfoTree root = rootBuilder.build();
142     try {
143       root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 1);
144       fail("Submessage index that is too large not detected");
145     } catch (IllegalArgumentException expected) {
146       // pass
147     }
148     try {
149       rootBuilder.build().getNestedTree(OPTIONAL_NESTED_MESSAGE, -1);
150       fail("Invalid submessage index (-1) not detected");
151     } catch (IllegalArgumentException expected) {
152       // pass
153     }
154   }
155 
testGetNestedTreesReturnsSingleTree()156   public void testGetNestedTreesReturnsSingleTree() {
157     rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE);
158     TextFormatParseInfoTree root = rootBuilder.build();
159     assertEquals(1, root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).size());
160     TextFormatParseInfoTree subtree = root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).get(0);
161     assertNotNull(subtree);
162   }
163 
testGetNestedTreesReturnsMultipleTrees()164   public void testGetNestedTreesReturnsMultipleTrees() {
165     TextFormatParseInfoTree.Builder subtree1Builder =
166         rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE);
167     subtree1Builder.getBuilderForSubMessageField(FIELD_BB);
168     subtree1Builder.getBuilderForSubMessageField(FIELD_BB);
169     TextFormatParseInfoTree.Builder subtree2Builder =
170         rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE);
171     subtree2Builder.getBuilderForSubMessageField(FIELD_BB);
172     TextFormatParseInfoTree root = rootBuilder.build();
173     assertEquals(2, root.getNestedTrees(REPEATED_NESTED_MESSAGE).size());
174     assertEquals(
175         2, root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(0).getNestedTrees(FIELD_BB).size());
176     assertEquals(
177         1, root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(1).getNestedTrees(FIELD_BB).size());
178   }
179 }
180