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.assertWithMessage; 11 12 import protobuf_unittest.UnittestProto.TestDeprecatedFields; 13 import java.lang.reflect.AnnotatedElement; 14 import java.lang.reflect.Method; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 /** Test field deprecation. */ 20 @RunWith(JUnit4.class) 21 public class DeprecatedFieldTest { 22 private final String[] deprecatedGetterNames = {"hasDeprecatedInt32", "getDeprecatedInt32"}; 23 24 private final String[] deprecatedBuilderGetterNames = { 25 "hasDeprecatedInt32", "getDeprecatedInt32", "clearDeprecatedInt32" 26 }; 27 28 private final String[] deprecatedBuilderSetterNames = {"setDeprecatedInt32"}; 29 30 @Test testDeprecatedField()31 public void testDeprecatedField() throws Exception { 32 Class<?> deprecatedFields = TestDeprecatedFields.class; 33 Class<?> deprecatedFieldsBuilder = TestDeprecatedFields.Builder.class; 34 for (String name : deprecatedGetterNames) { 35 Method method = deprecatedFields.getMethod(name); 36 assertWithMessage("Method %s should be deprecated", name).that(isDeprecated(method)).isTrue(); 37 } 38 for (String name : deprecatedBuilderGetterNames) { 39 Method method = deprecatedFieldsBuilder.getMethod(name); 40 assertWithMessage("Method %s should be deprecated", name).that(isDeprecated(method)).isTrue(); 41 } 42 for (String name : deprecatedBuilderSetterNames) { 43 Method method = deprecatedFieldsBuilder.getMethod(name, int.class); 44 assertWithMessage("Method %s should be deprecated", name).that(isDeprecated(method)).isTrue(); 45 } 46 } 47 48 @Test testDeprecatedFieldInOneof()49 public void testDeprecatedFieldInOneof() throws Exception { 50 Class<?> oneofCase = TestDeprecatedFields.OneofFieldsCase.class; 51 String name = "DEPRECATED_INT32_IN_ONEOF"; 52 java.lang.reflect.Field enumValue = oneofCase.getField(name); 53 assertWithMessage("Enum value %s should be deprecated.", name) 54 .that(isDeprecated(enumValue)) 55 .isTrue(); 56 } 57 isDeprecated(AnnotatedElement annotated)58 private boolean isDeprecated(AnnotatedElement annotated) { 59 return annotated.isAnnotationPresent(Deprecated.class); 60 } 61 } 62