2009-10-07 11:23:14 +02:00
|
|
|
/*
|
2009-10-08 22:51:40 +02:00
|
|
|
* Copyright (C) 2009 Google Inc.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson;
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
2022-12-22 15:04:16 +01:00
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
2010-11-01 23:31:48 +01:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.List;
|
2022-12-22 15:04:16 +01:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2009-10-07 11:23:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the {@link FieldAttributes} class.
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
public class FieldAttributesTest {
|
2009-10-07 11:23:14 +02:00
|
|
|
private FieldAttributes fieldAttributes;
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2011-11-21 06:42:30 +01:00
|
|
|
fieldAttributes = new FieldAttributes(Foo.class.getField("bar"));
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 00:49:25 +02:00
|
|
|
@SuppressWarnings("unused")
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-17 16:59:10 +01:00
|
|
|
public void testNullField() {
|
2009-10-07 11:23:14 +02:00
|
|
|
try {
|
2011-11-21 06:42:30 +01:00
|
|
|
new FieldAttributes(null);
|
2009-10-07 11:23:14 +02:00
|
|
|
fail("Field parameter can not be null");
|
2010-01-10 01:32:21 +01:00
|
|
|
} catch (NullPointerException expected) { }
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-17 16:59:10 +01:00
|
|
|
public void testDeclaringClass() {
|
|
|
|
assertThat(fieldAttributes.getDeclaringClass()).isAssignableTo(Foo.class);
|
2010-11-01 23:31:48 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-17 16:59:10 +01:00
|
|
|
public void testModifiers() {
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.STATIC)).isFalse();
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.FINAL)).isFalse();
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.ABSTRACT)).isFalse();
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.VOLATILE)).isFalse();
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.PROTECTED)).isFalse();
|
2009-10-07 11:23:14 +02:00
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.PUBLIC)).isTrue();
|
|
|
|
assertThat(fieldAttributes.hasModifier(Modifier.TRANSIENT)).isTrue();
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-17 16:59:10 +01:00
|
|
|
public void testName() {
|
|
|
|
assertThat(fieldAttributes.getName()).isEqualTo("bar");
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-17 16:59:10 +01:00
|
|
|
public void testDeclaredTypeAndClass() {
|
2009-10-07 11:23:14 +02:00
|
|
|
Type expectedType = new TypeToken<List<String>>() {}.getType();
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(fieldAttributes.getDeclaredType()).isEqualTo(expectedType);
|
|
|
|
assertThat(fieldAttributes.getDeclaredClass()).isAssignableTo(List.class);
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class Foo {
|
2009-10-09 03:35:45 +02:00
|
|
|
@SuppressWarnings("unused")
|
2009-10-07 11:23:14 +02:00
|
|
|
public transient List<String> bar;
|
|
|
|
}
|
|
|
|
}
|