Back to Repositories

Testing Inner Class Exclusion Strategy Implementation in Google Gson

This test suite validates Gson’s inner class exclusion strategy functionality, focusing on the behavior of serialization for inner classes versus static nested classes. It ensures proper handling of class exclusion policies in the Google Gson library.

Test Coverage Overview

The test suite provides comprehensive coverage of Gson’s inner class exclusion mechanism.

Key areas tested include:
  • Inner class object exclusion verification
  • Inner class field exclusion validation
  • Static nested class inclusion verification
  • Static nested class field handling

Implementation Analysis

The testing approach utilizes JUnit framework with Truth assertions to validate the Excluder class behavior. The implementation follows a structured pattern of testing both class-level and field-level exclusion rules, using helper methods for consistent assertion checking across different test cases.

The tests specifically verify:
  • Class exclusion rules for serialization and deserialization
  • Field exclusion patterns for both inner and static nested classes

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Google Truth assertion library
  • Gson’s Excluder utility class
  • Java Reflection API for field access

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear separation of test cases for different scenarios
  • Helper methods for reducing code duplication
  • Consistent assertion patterns
  • Proper test isolation
  • Comprehensive coverage of both positive and negative cases

google/gson

gson/src/test/java/com/google/gson/InnerClassExclusionStrategyTest.java

            
/*
 * Copyright (C) 2008 Google Inc.
 *
 * 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;

import static com.google.common.truth.Truth.assertThat;

import com.google.gson.internal.Excluder;
import java.lang.reflect.Field;
import org.junit.Test;

/**
 * Unit test for GsonBuilder.EXCLUDE_INNER_CLASSES.
 *
 * @author Joel Leitch
 */
public class InnerClassExclusionStrategyTest {
  public InnerClass innerClass = new InnerClass();
  public StaticNestedClass staticNestedClass = new StaticNestedClass();
  private Excluder excluder = Excluder.DEFAULT.disableInnerClassSerialization();

  private void assertIncludesClass(Class<?> c) {
    assertThat(excluder.excludeClass(c, true)).isFalse();
    assertThat(excluder.excludeClass(c, false)).isFalse();
  }

  private void assertExcludesClass(Class<?> c) {
    assertThat(excluder.excludeClass(c, true)).isTrue();
    assertThat(excluder.excludeClass(c, false)).isTrue();
  }

  private void assertIncludesField(Field f) {
    assertThat(excluder.excludeField(f, true)).isFalse();
    assertThat(excluder.excludeField(f, false)).isFalse();
  }

  private void assertExcludesField(Field f) {
    assertThat(excluder.excludeField(f, true)).isTrue();
    assertThat(excluder.excludeField(f, false)).isTrue();
  }

  @Test
  public void testExcludeInnerClassObject() {
    Class<?> clazz = innerClass.getClass();
    assertExcludesClass(clazz);
  }

  @Test
  public void testExcludeInnerClassField() throws Exception {
    Field f = getClass().getField("innerClass");
    assertExcludesField(f);
  }

  @Test
  public void testIncludeStaticNestedClassObject() {
    Class<?> clazz = staticNestedClass.getClass();
    assertIncludesClass(clazz);
  }

  @Test
  public void testIncludeStaticNestedClassField() throws Exception {
    Field f = getClass().getField("staticNestedClass");
    assertIncludesField(f);
  }

  @SuppressWarnings("ClassCanBeStatic")
  class InnerClass {}

  static class StaticNestedClass {}
}