public void testImport() throws Exception
    {
        File f = getFile("unittest.proto");
        assertTrue(f.exists());

        Proto proto = new Proto(f);
        ProtoUtil.loadFrom(f, proto);

        Proto iProto = proto.getImportedProto(getFile("google/protobuf/unittest_import.proto"));
        assertNotNull(iProto);
        assertEquals("protobuf_unittest_import", iProto.getPackageName());
        assertEquals("com.google.protobuf.test", iProto.getJavaPackageName());
        assertTrue(iProto.getMessages().size() == 1);
        assertTrue(iProto.getEnumGroups().size() == 1);

        EnumGroup importEnum = iProto.getEnumGroup("ImportEnum");
        assertNotNull(importEnum);
        assertTrue(importEnum.values.size() == 3);
        assertTrue(importEnum.getValue("IMPORT_FOO").number == 7);
        assertTrue(importEnum.getValue("IMPORT_BAR").number == 8);
        assertTrue(importEnum.getValue("IMPORT_BAZ").number == 9);

        Message importMessage = iProto.getMessage("ImportMessage");
        assertNotNull(importMessage);

        assertTrue(importMessage.getFields().size() == 1);
        Field.Int32 import_message_d = (Field.Int32) importMessage.getField("d");
        assertTrue(import_message_d != null);
        assertTrue(import_message_d.modifier == Modifier.OPTIONAL);
        assertTrue(import_message_d.number == 1);
        assertTrue(import_message_d.defaultValue == null);

        // unittest.proto

        assertEquals("protobuf_unittest", proto.getPackageName());
        assertEquals(proto.getJavaPackageName(), proto.getPackageName());

        assertTrue(proto.getEnumGroups().size() == 3);

        EnumGroup foreignEnum = proto.getEnumGroup("ForeignEnum");
        assertNotNull(foreignEnum);
        assertTrue(foreignEnum.getValues().size() == 3);

        EnumGroup testEnumWithDupValue = proto.getEnumGroup("TestEnumWithDupValue");
        assertNotNull(testEnumWithDupValue);
        assertTrue(testEnumWithDupValue.getValues().size() == 5);
        assertEquals("FOO2", testEnumWithDupValue.getSortedValues().get(0).name);
        assertTrue(testEnumWithDupValue.getSortedValues().get(0).number == 1);
        assertEquals("FOO1", testEnumWithDupValue.getSortedValues().get(1).name);
        assertTrue(testEnumWithDupValue.getSortedValues().get(1).number == 1);
        assertEquals("BAR2", testEnumWithDupValue.getSortedValues().get(2).name);
        assertTrue(testEnumWithDupValue.getSortedValues().get(2).number == 2);
        assertEquals("BAR1", testEnumWithDupValue.getSortedValues().get(3).name);
        assertTrue(testEnumWithDupValue.getSortedValues().get(3).number == 2);
        assertEquals("BAZ", testEnumWithDupValue.getSortedValues().get(4).name);
        assertTrue(testEnumWithDupValue.getSortedValues().get(4).number == 3);

        EnumGroup testSparseEnum = proto.getEnumGroup("TestSparseEnum");
        assertNotNull(testSparseEnum);
        assertTrue(testSparseEnum.getValues().size() == 7);
        assertTrue(testSparseEnum.getSortedValues().get(0).name.equals("SPARSE_E"));
        assertTrue(testSparseEnum.getSortedValues().get(0).number == -53452);
        assertTrue(testSparseEnum.getSortedValues().get(1).name.equals("SPARSE_D"));
        assertTrue(testSparseEnum.getSortedValues().get(1).number == -15);
        assertTrue(testSparseEnum.getSortedValues().get(2).name.equals("SPARSE_F"));
        assertTrue(testSparseEnum.getSortedValues().get(2).number == 0);
        assertTrue(testSparseEnum.getSortedValues().get(3).name.equals("SPARSE_G"));
        assertTrue(testSparseEnum.getSortedValues().get(3).number == 2);
        assertTrue(testSparseEnum.getSortedValues().get(4).name.equals("SPARSE_A"));
        assertTrue(testSparseEnum.getSortedValues().get(4).number == 123);
        assertTrue(testSparseEnum.getSortedValues().get(5).name.equals("SPARSE_B"));
        assertTrue(testSparseEnum.getSortedValues().get(5).number == 62374);
        assertTrue(testSparseEnum.getSortedValues().get(6).name.equals("SPARSE_C"));
        assertTrue(testSparseEnum.getSortedValues().get(6).number == 12589234);

        Message testAllTypes = proto.getMessage("TestAllTypes");
        assertNotNull(testAllTypes);
        assertTrue(testAllTypes.getNestedMessages().size() == 1);
        assertTrue(testAllTypes.getNestedEnumGroups().size() == 1);

        Field<?> defaultStringPiece = testAllTypes.getField("default_string_piece");
        Field<?> defaultCord = testAllTypes.getField("default_cord");

        assertNotNull(defaultStringPiece);
        assertEquals("STRING_PIECE", defaultStringPiece.getOption("ctype"));
        assertEquals("abc", defaultStringPiece.getOption("default"));
        assertEquals("abc", defaultStringPiece.defaultValue);

        assertNotNull(defaultCord);
        assertEquals("CORD", defaultCord.getOption("ctype"));
        assertEquals("123", defaultCord.getOption("default"));
        assertEquals("123", defaultCord.defaultValue);

        Message nestedMessage = testAllTypes.getNestedMessage("NestedMessage");
        assertNotNull(nestedMessage);
        EnumGroup nestedEnum = testAllTypes.getNestedEnumGroup("NestedEnum");
        assertNotNull(nestedEnum);

        Message foreignMessage = proto.getMessage("ForeignMessage");
        assertNotNull(foreignMessage);

        EnumField optional_nested_enum = testAllTypes.getField("optional_nested_enum",
                EnumField.class);
        assertNotNull(optional_nested_enum);
        assertTrue(nestedEnum == optional_nested_enum.getEnumGroup());

        EnumField optional_foreign_enum = testAllTypes.getField("optional_foreign_enum",
                EnumField.class);
        assertNotNull(optional_foreign_enum);
        assertTrue(foreignEnum == optional_foreign_enum.getEnumGroup());

        EnumField optional_import_enum = testAllTypes.getField("optional_import_enum",
                EnumField.class);
        assertNotNull(optional_import_enum);
        assertTrue(importEnum == optional_import_enum.getEnumGroup());

        MessageField optional_nested_message = testAllTypes.getField("optional_nested_message",
                MessageField.class);
        assertNotNull(optional_nested_message);
        assertTrue(nestedMessage == optional_nested_message.getMessage());

        MessageField optional_foreign_message = testAllTypes.getField("optional_foreign_message",
                MessageField.class);
        assertNotNull(optional_foreign_message);
        assertTrue(foreignMessage == optional_foreign_message.getMessage());

        MessageField optional_import_message = testAllTypes.getField("optional_import_message",
                MessageField.class);
        assertNotNull(optional_import_message);
        assertTrue(importMessage == optional_import_message.getMessage());

        Message testRequiredForeign = proto.getMessage("TestRequiredForeign");
        assertNotNull(testRequiredForeign);
        assertTrue(testRequiredForeign.getFields().size() == 3);

        MessageField test_required_foreign_optional_message = testRequiredForeign.getField("optional_message",
                MessageField.class);
        assertNotNull(test_required_foreign_optional_message);
        assertTrue(test_required_foreign_optional_message.modifier == Modifier.OPTIONAL);

        MessageField test_required_foreign_repeated_message = testRequiredForeign.getField("repeated_message",
                MessageField.class);
        assertNotNull(test_required_foreign_repeated_message);
        assertTrue(test_required_foreign_repeated_message.modifier == Modifier.REPEATED);

        Field.Int32 dummy = testRequiredForeign.getField("dummy", Field.Int32.class);
        assertNotNull(dummy);
        assertTrue(dummy.modifier == Modifier.OPTIONAL && dummy.number == 3);

        Message testForeignNested = proto.getMessage("TestForeignNested");
        assertNotNull(testForeignNested);
        MessageField foreign_nested = testForeignNested.getField("foreign_nested",
                MessageField.class);
        assertNotNull(foreign_nested);
        assertTrue(nestedMessage == foreign_nested.getMessage());

        Message testEmptyMessage = proto.getMessage("TestEmptyMessage");
        assertNotNull(testEmptyMessage);
        assertTrue(testEmptyMessage.getFields().size() == 0);
        assertTrue(testEmptyMessage.getNestedEnumGroups().size() == 0);
        assertTrue(testEmptyMessage.getNestedMessages().size() == 0);

        Message testReallyLargeTagNumber = proto.getMessage("TestReallyLargeTagNumber");
        assertNotNull(testReallyLargeTagNumber);

        Field.Int32 a = testReallyLargeTagNumber.getField("a", Field.Int32.class);
        assertNotNull(a);
        assertTrue(a.number == 1);
        Field.Int32 bb = testReallyLargeTagNumber.getField("bb", Field.Int32.class);
        assertNotNull(bb);
        assertTrue(bb.number == 268435455);

        Message testRecursiveMessage = proto.getMessage("TestRecursiveMessage");
        assertNotNull(testRecursiveMessage);

        MessageField testRecursiveMessage_a = testRecursiveMessage.getField("a",
                MessageField.class);
        assertTrue(testRecursiveMessage == testRecursiveMessage_a.getMessage());

        Message testMutualRecursionA = proto.getMessage("TestMutualRecursionA");
        assertNotNull(testMutualRecursionA);
        Message testMutualRecursionB = proto.getMessage("TestMutualRecursionB");
        assertNotNull(testMutualRecursionB);

        MessageField testMutualRecursionA_bb = testMutualRecursionA.getField("bb",
                MessageField.class);
        assertNotNull(testMutualRecursionA_bb);
        MessageField testMutualRecursionB_a = testMutualRecursionB.getField("a",
                MessageField.class);
        assertNotNull(testMutualRecursionB_a);

        assertTrue(testMutualRecursionA == testMutualRecursionB_a.getMessage());
        assertTrue(testMutualRecursionB == testMutualRecursionA_bb.getMessage());

        Message testNestedMessageHasBits = proto.getMessage("TestNestedMessageHasBits");
        assertNotNull(testNestedMessageHasBits);
        Message tnmhb_nestedMessage = testNestedMessageHasBits.getNestedMessage("NestedMessage");
        assertNotNull(tnmhb_nestedMessage);

        MessageField tnmhb_optional_nested_message = testNestedMessageHasBits.getField("optional_nested_message",
                MessageField.class);
        assertNotNull(tnmhb_optional_nested_message);
        assertTrue(tnmhb_nestedMessage == tnmhb_optional_nested_message.getMessage());

        MessageField nestedmessage_repeated_foreignmessage = tnmhb_nestedMessage.getField(
                "nestedmessage_repeated_foreignmessage",
                MessageField.class);
        assertNotNull(nestedmessage_repeated_foreignmessage);
        assertTrue(foreignMessage == nestedmessage_repeated_foreignmessage.getMessage());

        Message testFieldOrderings = proto.getMessage("TestFieldOrderings");
        assertNotNull(testFieldOrderings);
        assertTrue(testFieldOrderings.getFields().size() == 3);
        assertEquals(testFieldOrderings.sortedFields.get(0).name, "my_int");
        assertEquals(testFieldOrderings.sortedFields.get(1).name, "my_string");
        assertEquals(testFieldOrderings.sortedFields.get(2).name, "my_float");

        Message testExtremeDefaultValues = proto.getMessage("TestExtremeDefaultValues");
        assertNotNull(testExtremeDefaultValues);

        Field.UInt32 large_uint32 = testExtremeDefaultValues.getField("large_uint32",
                Field.UInt32.class);
        assertNotNull(large_uint32);
        assertTrue((large_uint32.getDefaultValue().intValue() & 0xFFFFFFFF) == 0xFFFFFFFF);

        Field.UInt64 large_uint64 = testExtremeDefaultValues.getField("large_uint64",
                Field.UInt64.class);
        assertNotNull(large_uint64);
        assertTrue(-1 == large_uint64.getDefaultValue().longValue());

        Field.Int32 small_int32 = testExtremeDefaultValues.getField("small_int32",
                Field.Int32.class);
        assertNotNull(small_int32);
        assertTrue(small_int32.getDefaultValue().intValue() == -0x7FFFFFFF);

        Field.Int64 small_int64 = testExtremeDefaultValues.getField("small_int64",
                Field.Int64.class);
        assertNotNull(small_int64);
        assertTrue(-Long.MAX_VALUE == small_int64.getDefaultValue().longValue());

        Message testAllExtensions = proto.getMessage("TestAllExtensions");
        assertNotNull(proto.getExtensions());
        assertTrue(proto.getExtensions().size() > 0);
        Extension extension = proto.getExtensions().iterator().next();
        assertEquals(testAllExtensions, extension.extendedMessage);
        assertNotNull(extension.getFields());
        assertTrue(extension.getFields().size() > 0);

        Message testNestedExtension = proto.getMessage("TestNestedExtension");
        assertNotNull(testNestedExtension.getNestedExtensions());
        assertEquals(1, testNestedExtension.getNestedExtensions().size());
        extension = testNestedExtension.getNestedExtensions().iterator().next();
        assertTrue(extension.isNested());

        Message testMultipleExtensionRanges = proto.getMessage("TestMultipleExtensionRanges");
        assertNotNull(testMultipleExtensionRanges);
        assertTrue(3 == testMultipleExtensionRanges.extensionRanges.size());
        int[] first = testMultipleExtensionRanges.extensionRanges.get(0);
        int[] second = testMultipleExtensionRanges.extensionRanges.get(1);
        int[] third = testMultipleExtensionRanges.extensionRanges.get(2);

        assertTrue(first[0] == first[1] && first[0] == 42);
        assertTrue(second[0] == 4143 && second[1] == 4243);
        assertTrue(third[0] == 65536 && third[1] == 536870911);
    }
Various tests for the proto parser.

@author David Yu
@created Dec 18, 2009
protostuff-parser/src/test/java/io/protostuff/parser/ProtoParserTest.java