Back to Repositories

Testing RuntimeTypeAdapter Factory Polymorphic Serialization in google/gson

This test suite validates the RuntimeTypeAdapterFactory functionality in the Google Gson library, focusing on polymorphic type handling and JSON serialization/deserialization of class hierarchies. The tests ensure proper type discrimination and object reconstruction during JSON processing.

Test Coverage Overview

The test suite provides comprehensive coverage of the RuntimeTypeAdapterFactory implementation, focusing on polymorphic type handling with Shape class hierarchies. Key functionality includes:
  • Automatic subclass serialization and deserialization
  • Type field name handling and validation
  • Subtype registration and mapping
  • Error handling for unregistered types

Implementation Analysis

The testing approach utilizes JUnit framework with a focus on functional testing patterns. The implementation demonstrates:
  • Custom TypeAdapterFactory implementation
  • Integration with Gson’s type handling system
  • JsonAdapter annotation usage
  • Delegate adapter pattern implementation

Technical Details

Testing infrastructure includes:
  • Gson library for JSON processing
  • JUnit test framework
  • Custom Shape class hierarchy for testing
  • Truth assertion library for test validation
  • RuntimeTypeAdapterFactory implementation in the extras package

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test case organization and naming
  • Comprehensive type handling validation
  • Proper error case coverage
  • Clean separation of test fixtures and logic
  • Effective use of inheritance in test structures

google/gson

gson/src/test/java/com/google/gson/functional/RuntimeTypeAdapterFactoryFunctionalTest.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.functional;

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

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;

/** Functional tests for the RuntimeTypeAdapterFactory feature in extras. */
public final class RuntimeTypeAdapterFactoryFunctionalTest {

  private final Gson gson = new Gson();

  /**
   * This test also ensures that {@link TypeAdapterFactory} registered through {@link JsonAdapter}
   * work correctly for {@link Gson#getDelegateAdapter(TypeAdapterFactory, TypeToken)}.
   */
  @Test
  public void testSubclassesAutomaticallySerialized() {
    Shape shape = new Circle(25);
    String json = gson.toJson(shape);
    shape = gson.fromJson(json, Shape.class);
    assertThat(((Circle) shape).radius).isEqualTo(25);

    shape = new Square(15);
    json = gson.toJson(shape);
    shape = gson.fromJson(json, Shape.class);
    assertThat(((Square) shape).side).isEqualTo(15);
    assertThat(shape.type).isEqualTo(ShapeType.SQUARE);
  }

  @JsonAdapter(Shape.JsonAdapterFactory.class)
  static class Shape {
    final ShapeType type;

    Shape(ShapeType type) {
      this.type = type;
    }

    private static final class JsonAdapterFactory extends RuntimeTypeAdapterFactory<Shape> {
      public JsonAdapterFactory() {
        super(Shape.class, "type");
        registerSubtype(Circle.class, ShapeType.CIRCLE.toString());
        registerSubtype(Square.class, ShapeType.SQUARE.toString());
      }
    }
  }

  public enum ShapeType {
    SQUARE,
    CIRCLE
  }

  private static final class Circle extends Shape {
    final int radius;

    Circle(int radius) {
      super(ShapeType.CIRCLE);
      this.radius = radius;
    }
  }

  private static final class Square extends Shape {
    final int side;

    Square(int side) {
      super(ShapeType.SQUARE);
      this.side = side;
    }
  }

  // Copied from the extras package
  static class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
    private final Class<?> baseType;
    private final String typeFieldName;
    private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<>();
    private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<>();

    protected RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName) {
      if (typeFieldName == null || baseType == null) {
        throw new NullPointerException();
      }
      this.baseType = baseType;
      this.typeFieldName = typeFieldName;
    }

    /**
     * Creates a new runtime type adapter for {@code baseType} using {@code typeFieldName} as the
     * type field name. Type field names are case sensitive.
     */
    public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
      return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName);
    }

    /**
     * Creates a new runtime type adapter for {@code baseType} using {@code "type"} as the type
     * field name.
     */
    public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
      return new RuntimeTypeAdapterFactory<>(baseType, "type");
    }

    /**
     * Registers {@code type} identified by {@code label}. Labels are case sensitive.
     *
     * @throws IllegalArgumentException if either {@code type} or {@code label} have already been
     *     registered on this type adapter.
     */
    @CanIgnoreReturnValue
    public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
      if (type == null || label == null) {
        throw new NullPointerException();
      }
      if (subtypeToLabel.containsKey(type) || labelToSubtype.containsKey(label)) {
        throw new IllegalArgumentException("types and labels must be unique");
      }
      labelToSubtype.put(label, type);
      subtypeToLabel.put(type, label);
      return this;
    }

    /**
     * Registers {@code type} identified by its {@link Class#getSimpleName simple name}. Labels are
     * case sensitive.
     *
     * @throws IllegalArgumentException if either {@code type} or its simple name have already been
     *     registered on this type adapter.
     */
    @CanIgnoreReturnValue
    public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
      return registerSubtype(type, type.getSimpleName());
    }

    @Override
    public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
      if (type.getRawType() != baseType) {
        return null;
      }

      Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
      Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
      for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
      }

      return new TypeAdapter<>() {
        @Override
        public R read(JsonReader in) {
          JsonElement jsonElement = Streams.parse(in);
          JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
          if (labelJsonElement == null) {
            throw new JsonParseException(
                "cannot deserialize "
                    + baseType
                    + " because it does not define a field named "
                    + typeFieldName);
          }
          String label = labelJsonElement.getAsString();
          @SuppressWarnings("unchecked") // registration requires that subtype extends T
          TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
          if (delegate == null) {
            throw new JsonParseException(
                "cannot deserialize "
                    + baseType
                    + " subtype named "
                    + label
                    + "; did you forget to register a subtype?");
          }
          return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
          Class<?> srcType = value.getClass();
          String label = subtypeToLabel.get(srcType);
          @SuppressWarnings("unchecked") // registration requires that subtype extends T
          TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
          if (delegate == null) {
            throw new JsonParseException(
                "cannot serialize "
                    + srcType.getName()
                    + "; did you forget to register a subtype?");
          }
          JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
          if (!jsonObject.has(typeFieldName)) {
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
              clone.add(e.getKey(), e.getValue());
            }
            jsonObject = clone;
          }
          Streams.write(jsonObject, out);
        }
      };
    }
  }
}