1919import com .google .protobuf .DescriptorProtos .FeatureSetDefaults ;
2020import com .google .protobuf .DescriptorProtos .FeatureSetDefaults .FeatureSetEditionDefault ;
2121import com .google .protobuf .DescriptorProtos .FieldDescriptorProto ;
22+ import com .google .protobuf .DescriptorProtos .FieldOptions ;
2223import com .google .protobuf .DescriptorProtos .FileDescriptorProto ;
2324import com .google .protobuf .DescriptorProtos .FileOptions ;
2425import com .google .protobuf .DescriptorProtos .MethodDescriptorProto ;
5354import protobuf_unittest .UnittestProto .TestReservedFields ;
5455import protobuf_unittest .UnittestProto .TestService ;
5556import protobuf_unittest .UnittestRetention ;
56- import proto3_unittest .UnittestProto3 ;
5757import protobuf_unittest .UnittestProto3Extensions .Proto3FileExtensions ;
5858import java .util .Collections ;
5959import java .util .List ;
@@ -1256,32 +1256,106 @@ public void testLegacyGroupTransform() {
12561256 }
12571257
12581258 @ Test
1259- public void testLegacyInferRequired () {
1260- FieldDescriptor field = UnittestProto .TestRequired .getDescriptor ().findFieldByName ("a" );
1259+ public void testLegacyInferRequired () throws Exception {
1260+ FileDescriptor file =
1261+ FileDescriptor .buildFrom (
1262+ FileDescriptorProto .newBuilder ()
1263+ .setName ("some/filename/some.proto" )
1264+ .setSyntax ("proto2" )
1265+ .addMessageType (
1266+ DescriptorProto .newBuilder ()
1267+ .setName ("Foo" )
1268+ .addField (
1269+ FieldDescriptorProto .newBuilder ()
1270+ .setName ("a" )
1271+ .setNumber (1 )
1272+ .setType (FieldDescriptorProto .Type .TYPE_INT32 )
1273+ .setLabel (FieldDescriptorProto .Label .LABEL_REQUIRED )
1274+ .build ())
1275+ .build ())
1276+ .build (),
1277+ new FileDescriptor [0 ]);
1278+ FieldDescriptor field = file .findMessageTypeByName ("Foo" ).findFieldByName ("a" );
12611279 assertThat (field .features .getFieldPresence ())
12621280 .isEqualTo (DescriptorProtos .FeatureSet .FieldPresence .LEGACY_REQUIRED );
12631281 }
12641282
12651283 @ Test
1266- public void testLegacyInferGroup () {
1267- FieldDescriptor field =
1268- UnittestProto .TestAllTypes .getDescriptor ().findFieldByName ("optionalgroup" );
1284+ public void testLegacyInferGroup () throws Exception {
1285+ FileDescriptor file =
1286+ FileDescriptor .buildFrom (
1287+ FileDescriptorProto .newBuilder ()
1288+ .setName ("some/filename/some.proto" )
1289+ .setSyntax ("proto2" )
1290+ .addMessageType (
1291+ DescriptorProto .newBuilder ()
1292+ .setName ("Foo" )
1293+ .addNestedType (
1294+ DescriptorProto .newBuilder ().setName ("OptionalGroup" ).build ())
1295+ .addField (
1296+ FieldDescriptorProto .newBuilder ()
1297+ .setName ("optionalgroup" )
1298+ .setNumber (1 )
1299+ .setType (FieldDescriptorProto .Type .TYPE_GROUP )
1300+ .setLabel (FieldDescriptorProto .Label .LABEL_OPTIONAL )
1301+ .setTypeName ("Foo.OptionalGroup" )
1302+ .build ())
1303+ .build ())
1304+ .build (),
1305+ new FileDescriptor [0 ]);
1306+ FieldDescriptor field = file .findMessageTypeByName ("Foo" ).findFieldByName ("optionalgroup" );
12691307 assertThat (field .features .getMessageEncoding ())
12701308 .isEqualTo (DescriptorProtos .FeatureSet .MessageEncoding .DELIMITED );
12711309 }
12721310
12731311 @ Test
1274- public void testLegacyInferProto2Packed () {
1275- FieldDescriptor field =
1276- UnittestProto .TestPackedTypes .getDescriptor ().findFieldByName ("packed_int32" );
1312+ public void testLegacyInferProto2Packed () throws Exception {
1313+ FileDescriptor file =
1314+ FileDescriptor .buildFrom (
1315+ FileDescriptorProto .newBuilder ()
1316+ .setName ("some/filename/some.proto" )
1317+ .setSyntax ("proto2" )
1318+ .addMessageType (
1319+ DescriptorProto .newBuilder ()
1320+ .setName ("Foo" )
1321+ .addField (
1322+ FieldDescriptorProto .newBuilder ()
1323+ .setName ("a" )
1324+ .setNumber (1 )
1325+ .setLabel (FieldDescriptorProto .Label .LABEL_REPEATED )
1326+ .setType (FieldDescriptorProto .Type .TYPE_INT32 )
1327+ .setOptions (FieldOptions .newBuilder ().setPacked (true ).build ())
1328+ .build ())
1329+ .build ())
1330+ .build (),
1331+ new FileDescriptor [0 ]);
1332+ FieldDescriptor field = file .findMessageTypeByName ("Foo" ).findFieldByName ("a" );
12771333 assertThat (field .features .getRepeatedFieldEncoding ())
12781334 .isEqualTo (DescriptorProtos .FeatureSet .RepeatedFieldEncoding .PACKED );
12791335 }
12801336
12811337 @ Test
1282- public void testLegacyInferProto3Expanded () {
1283- FieldDescriptor field =
1284- UnittestProto3 .TestUnpackedTypes .getDescriptor ().findFieldByName ("repeated_int32" );
1338+ public void testLegacyInferProto3Expanded () throws Exception {
1339+ FileDescriptor file =
1340+ FileDescriptor .buildFrom (
1341+ FileDescriptorProto .newBuilder ()
1342+ .setName ("some/filename/some.proto" )
1343+ .setSyntax ("proto3" )
1344+ .addMessageType (
1345+ DescriptorProto .newBuilder ()
1346+ .setName ("Foo" )
1347+ .addField (
1348+ FieldDescriptorProto .newBuilder ()
1349+ .setName ("a" )
1350+ .setNumber (1 )
1351+ .setType (FieldDescriptorProto .Type .TYPE_INT32 )
1352+ .setLabel (FieldDescriptorProto .Label .LABEL_REPEATED )
1353+ .setOptions (FieldOptions .newBuilder ().setPacked (false ).build ())
1354+ .build ())
1355+ .build ())
1356+ .build (),
1357+ new FileDescriptor [0 ]);
1358+ FieldDescriptor field = file .findMessageTypeByName ("Foo" ).findFieldByName ("a" );
12851359 assertThat (field .features .getRepeatedFieldEncoding ())
12861360 .isEqualTo (DescriptorProtos .FeatureSet .RepeatedFieldEncoding .EXPANDED );
12871361 }
@@ -1302,9 +1376,16 @@ public void testLegacyInferProto2Utf8Validation() throws Exception {
13021376 }
13031377
13041378 @ Test
1305- public void testProto2Defaults () {
1306- FieldDescriptor proto2Field = TestAllTypes .getDescriptor ().findFieldByName ("optional_int32" );
1307- DescriptorProtos .FeatureSet features = proto2Field .features ;
1379+ public void testProto2Defaults () throws Exception {
1380+ FileDescriptor proto2File =
1381+ FileDescriptor .buildFrom (
1382+ FileDescriptorProto .newBuilder ()
1383+ .setName ("some/filename/some.proto" )
1384+ .setPackage ("protobuf_unittest" )
1385+ .setSyntax ("proto2" )
1386+ .build (),
1387+ new FileDescriptor [0 ]);
1388+ DescriptorProtos .FeatureSet features = proto2File .features ;
13081389 assertThat (features .getFieldPresence ())
13091390 .isEqualTo (DescriptorProtos .FeatureSet .FieldPresence .EXPLICIT );
13101391 assertThat (features .getEnumType ()).isEqualTo (DescriptorProtos .FeatureSet .EnumType .CLOSED );
@@ -1323,10 +1404,16 @@ public void testProto2Defaults() {
13231404 }
13241405
13251406 @ Test
1326- public void testProto3Defaults () {
1327- FieldDescriptor proto3Field =
1328- UnittestProto3 .TestAllTypes .getDescriptor ().findFieldByName ("optional_int32" );
1329- DescriptorProtos .FeatureSet features = proto3Field .features ;
1407+ public void testProto3Defaults () throws Exception {
1408+ FileDescriptor proto3File =
1409+ FileDescriptor .buildFrom (
1410+ FileDescriptorProto .newBuilder ()
1411+ .setName ("some/filename/some.proto" )
1412+ .setPackage ("proto3_unittest" )
1413+ .setSyntax ("proto3" )
1414+ .build (),
1415+ new FileDescriptor [0 ]);
1416+ DescriptorProtos .FeatureSet features = proto3File .features ;
13301417 assertThat (features .getFieldPresence ())
13311418 .isEqualTo (DescriptorProtos .FeatureSet .FieldPresence .IMPLICIT );
13321419 assertThat (features .getEnumType ()).isEqualTo (DescriptorProtos .FeatureSet .EnumType .OPEN );
0 commit comments