1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 import static com.google.common.truth.Truth.assertThat; 11 import static com.google.common.truth.Truth.assertWithMessage; 12 13 import com.google.protobuf.Descriptors.Descriptor; 14 import com.google.protobuf.Descriptors.FieldDescriptor; 15 import protobuf_unittest.UnittestProto.TestAllTypes; 16 import org.junit.Before; 17 import org.junit.Test; 18 import org.junit.runner.RunWith; 19 import org.junit.runners.JUnit4; 20 21 /** Test {@link TextFormatParseInfoTree}. */ 22 @RunWith(JUnit4.class) 23 public class TextFormatParseInfoTreeTest { 24 25 private static final Descriptor DESCRIPTOR = TestAllTypes.getDescriptor(); 26 private static final FieldDescriptor OPTIONAL_INT32 = 27 DESCRIPTOR.findFieldByName("optional_int32"); 28 private static final FieldDescriptor OPTIONAL_BOOLEAN = 29 DESCRIPTOR.findFieldByName("optional_boolean"); 30 private static final FieldDescriptor REPEATED_INT32 = 31 DESCRIPTOR.findFieldByName("repeated_int32"); 32 private static final FieldDescriptor OPTIONAL_NESTED_MESSAGE = 33 DESCRIPTOR.findFieldByName("optional_nested_message"); 34 private static final FieldDescriptor REPEATED_NESTED_MESSAGE = 35 DESCRIPTOR.findFieldByName("repeated_nested_message"); 36 private static final FieldDescriptor FIELD_BB = 37 TestAllTypes.NestedMessage.getDescriptor().findFieldByName("bb"); 38 39 private static final TextFormatParseLocation LOC0 = TextFormatParseLocation.create(1, 2); 40 private static final TextFormatParseLocation LOC1 = TextFormatParseLocation.create(2, 3); 41 42 private TextFormatParseInfoTree.Builder rootBuilder; 43 44 @Before setUp()45 public void setUp() { 46 rootBuilder = TextFormatParseInfoTree.builder(); 47 } 48 49 @Test testBuildEmptyParseTree()50 public void testBuildEmptyParseTree() { 51 TextFormatParseInfoTree tree = rootBuilder.build(); 52 assertThat(tree.getLocations(null)).isEmpty(); 53 } 54 55 @Test testGetLocationReturnsSingleLocation()56 public void testGetLocationReturnsSingleLocation() { 57 rootBuilder.setLocation(OPTIONAL_INT32, LOC0); 58 TextFormatParseInfoTree root = rootBuilder.build(); 59 assertThat(root.getLocation(OPTIONAL_INT32, 0)).isEqualTo(LOC0); 60 assertThat(root.getLocations(OPTIONAL_INT32)).hasSize(1); 61 } 62 63 @Test testGetLocationsReturnsNoParseLocationsForUnknownField()64 public void testGetLocationsReturnsNoParseLocationsForUnknownField() { 65 assertThat(rootBuilder.build().getLocations(OPTIONAL_INT32)).isEmpty(); 66 rootBuilder.setLocation(OPTIONAL_BOOLEAN, LOC0); 67 TextFormatParseInfoTree root = rootBuilder.build(); 68 assertThat(root.getLocations(OPTIONAL_INT32)).isEmpty(); 69 assertThat(root.getLocations(OPTIONAL_BOOLEAN).get(0)).isEqualTo(LOC0); 70 } 71 72 @Test testGetLocationThrowsIllegalArgumentExceptionForUnknownField()73 public void testGetLocationThrowsIllegalArgumentExceptionForUnknownField() { 74 rootBuilder.setLocation(REPEATED_INT32, LOC0); 75 TextFormatParseInfoTree root = rootBuilder.build(); 76 try { 77 root.getNestedTree(OPTIONAL_INT32, 0); 78 assertWithMessage("Did not detect unknown field").fail(); 79 } catch (IllegalArgumentException expected) { 80 // pass 81 } 82 } 83 84 @Test testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex()85 public void testGetLocationThrowsIllegalArgumentExceptionForInvalidIndex() { 86 TextFormatParseInfoTree root = rootBuilder.setLocation(OPTIONAL_INT32, LOC0).build(); 87 try { 88 root.getLocation(OPTIONAL_INT32, 1); 89 assertWithMessage("Invalid index not detected").fail(); 90 } catch (IllegalArgumentException expected) { 91 // pass 92 } 93 try { 94 root.getLocation(OPTIONAL_INT32, -1); 95 assertWithMessage("Negative index not detected").fail(); 96 } catch (IllegalArgumentException expected) { 97 // pass 98 } 99 } 100 101 @Test testGetLocationsReturnsMultipleLocations()102 public void testGetLocationsReturnsMultipleLocations() { 103 rootBuilder.setLocation(REPEATED_INT32, LOC0); 104 rootBuilder.setLocation(REPEATED_INT32, LOC1); 105 TextFormatParseInfoTree root = rootBuilder.build(); 106 assertThat(root.getLocation(REPEATED_INT32, 0)).isEqualTo(LOC0); 107 assertThat(root.getLocation(REPEATED_INT32, 1)).isEqualTo(LOC1); 108 assertThat(root.getLocations(REPEATED_INT32)).hasSize(2); 109 } 110 111 @Test testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField()112 public void testGetNestedTreeThrowsIllegalArgumentExceptionForUnknownField() { 113 rootBuilder.setLocation(REPEATED_INT32, LOC0); 114 TextFormatParseInfoTree root = rootBuilder.build(); 115 try { 116 root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 0); 117 assertWithMessage("Did not detect unknown field").fail(); 118 } catch (IllegalArgumentException expected) { 119 // pass 120 } 121 } 122 123 @Test testGetNestedTreesReturnsNoParseInfoTreesForUnknownField()124 public void testGetNestedTreesReturnsNoParseInfoTreesForUnknownField() { 125 rootBuilder.setLocation(REPEATED_INT32, LOC0); 126 TextFormatParseInfoTree root = rootBuilder.build(); 127 assertThat(root.getNestedTrees(OPTIONAL_NESTED_MESSAGE)).isEmpty(); 128 } 129 130 @Test testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex()131 public void testGetNestedTreeThrowsIllegalArgumentExceptionForInvalidIndex() { 132 rootBuilder.setLocation(REPEATED_INT32, LOC0); 133 rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE); 134 TextFormatParseInfoTree root = rootBuilder.build(); 135 try { 136 root.getNestedTree(OPTIONAL_NESTED_MESSAGE, 1); 137 assertWithMessage("Submessage index that is too large not detected").fail(); 138 } catch (IllegalArgumentException expected) { 139 // pass 140 } 141 try { 142 rootBuilder.build().getNestedTree(OPTIONAL_NESTED_MESSAGE, -1); 143 assertWithMessage("Invalid submessage index (-1) not detected").fail(); 144 } catch (IllegalArgumentException expected) { 145 // pass 146 } 147 } 148 149 @Test testGetNestedTreesReturnsSingleTree()150 public void testGetNestedTreesReturnsSingleTree() { 151 rootBuilder.getBuilderForSubMessageField(OPTIONAL_NESTED_MESSAGE); 152 TextFormatParseInfoTree root = rootBuilder.build(); 153 assertThat(root.getNestedTrees(OPTIONAL_NESTED_MESSAGE)).hasSize(1); 154 TextFormatParseInfoTree subtree = root.getNestedTrees(OPTIONAL_NESTED_MESSAGE).get(0); 155 assertThat(subtree).isNotNull(); 156 } 157 158 @Test testGetNestedTreesReturnsMultipleTrees()159 public void testGetNestedTreesReturnsMultipleTrees() { 160 TextFormatParseInfoTree.Builder subtree1Builder = 161 rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE); 162 subtree1Builder.getBuilderForSubMessageField(FIELD_BB); 163 subtree1Builder.getBuilderForSubMessageField(FIELD_BB); 164 TextFormatParseInfoTree.Builder subtree2Builder = 165 rootBuilder.getBuilderForSubMessageField(REPEATED_NESTED_MESSAGE); 166 subtree2Builder.getBuilderForSubMessageField(FIELD_BB); 167 TextFormatParseInfoTree root = rootBuilder.build(); 168 assertThat(root.getNestedTrees(REPEATED_NESTED_MESSAGE)).hasSize(2); 169 assertThat(root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(0).getNestedTrees(FIELD_BB)) 170 .hasSize(2); 171 assertThat(root.getNestedTrees(REPEATED_NESTED_MESSAGE).get(1).getNestedTrees(FIELD_BB)) 172 .hasSize(1); 173 } 174 } 175