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 junit.framework.TestCase; 34 35 /** 36 * Tests that proto2 api generation doesn't cause compile errors when compiling protocol buffers 37 * that have names that would otherwise conflict if not fully qualified (like @Deprecated 38 * and @Override). 39 * 40 * @author jonp@google.com (Jon Perlow) 41 */ 42 public class TestBadIdentifiers extends TestCase { 43 testCompilation()44 public void testCompilation() { 45 // If this compiles, it means the generation was correct. 46 TestBadIdentifiersProto.Deprecated.newBuilder(); 47 TestBadIdentifiersProto.Override.newBuilder(); 48 } 49 testGetDescriptor()50 public void testGetDescriptor() { 51 TestBadIdentifiersProto.getDescriptor(); 52 TestBadIdentifiersProto.Descriptor.getDefaultInstance().getDescriptor(); 53 TestBadIdentifiersProto.Descriptor.getDefaultInstance().getDescriptorForType(); 54 TestBadIdentifiersProto.Descriptor.NestedDescriptor.getDefaultInstance().getDescriptor(); 55 TestBadIdentifiersProto.Descriptor.NestedDescriptor.getDefaultInstance().getDescriptorForType(); 56 } 57 testConflictingFieldNames()58 public void testConflictingFieldNames() throws Exception { 59 TestBadIdentifiersProto.TestConflictingFieldNames message = 60 TestBadIdentifiersProto.TestConflictingFieldNames.getDefaultInstance(); 61 // Make sure generated accessors are properly named. 62 assertEquals(0, message.getInt32Field1Count()); 63 assertEquals(0, message.getEnumField2Count()); 64 assertEquals(0, message.getStringField3Count()); 65 assertEquals(0, message.getBytesField4Count()); 66 assertEquals(0, message.getMessageField5Count()); 67 68 assertEquals(0, message.getInt32FieldCount11()); 69 assertEquals(0, message.getEnumFieldCount12().getNumber()); 70 assertEquals("", message.getStringFieldCount13()); 71 assertEquals(ByteString.EMPTY, message.getBytesFieldCount14()); 72 assertEquals(0, message.getMessageFieldCount15().getSerializedSize()); 73 74 assertEquals(0, message.getInt32Field21Count()); 75 assertEquals(0, message.getEnumField22Count()); 76 assertEquals(0, message.getStringField23Count()); 77 assertEquals(0, message.getBytesField24Count()); 78 assertEquals(0, message.getMessageField25Count()); 79 80 assertEquals(0, message.getInt32Field1List().size()); 81 assertEquals(0, message.getInt32FieldList31()); 82 83 assertEquals(0, message.getInt64FieldCount()); 84 assertEquals( 85 0L, 86 message 87 .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldCount) 88 .longValue()); 89 assertEquals( 90 0L, 91 message 92 .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldList) 93 .longValue()); 94 95 assertEquals("", message.getFieldName32()); 96 assertEquals("", message.getFieldName33()); 97 assertEquals(0, message.get2Conflict34()); 98 assertEquals(0, message.get2Conflict35()); 99 100 } 101 testNumberFields()102 public void testNumberFields() throws Exception { 103 TestBadIdentifiersProto.TestLeadingNumberFields message = 104 TestBadIdentifiersProto.TestLeadingNumberFields.getDefaultInstance(); 105 // Make sure generated accessors are properly named. 106 assertFalse(message.has30DayImpressions()); 107 assertEquals(0, message.get30DayImpressions()); 108 assertEquals(0, message.get60DayImpressionsCount()); 109 assertEquals(0, message.get60DayImpressionsList().size()); 110 111 assertFalse(message.has2Underscores()); 112 assertEquals("", message.get2Underscores()); 113 assertEquals(0, message.get2RepeatedUnderscoresCount()); 114 assertEquals(0, message.get2RepeatedUnderscoresList().size()); 115 116 assertFalse(message.has32()); 117 assertEquals(0, message.get32()); 118 assertEquals(0, message.get64Count()); 119 assertEquals(0, message.get64List().size()); 120 121 } 122 } 123