Back to Repositories

Testing JsonArray List Operations in Google Gson

This test suite implements comprehensive validation for JsonArray’s asList() functionality in the Gson library, utilizing Google’s collection testing framework to verify List interface compliance and element handling. The suite ensures proper JSON element manipulation and List view consistency.

Test Coverage Overview

The test suite provides extensive coverage of JsonArray’s List view functionality.

Key areas tested include:
  • Element addition and removal operations
  • Indexed access and modifications
  • Null element handling
  • Type restriction enforcement for JsonElement
  • List view synchronization with underlying JsonArray

Implementation Analysis

The implementation leverages Google’s collection testing framework through ListTestSuiteBuilder to generate comprehensive test scenarios. The TestListGenerator interface implementation provides sample JSON elements and handles list creation operations.

Notable patterns include:
  • Dynamic test suite generation using builder pattern
  • Sample element generation covering different JsonElement types
  • Custom list creation logic maintaining JsonArray consistency

Technical Details

Testing infrastructure includes:
  • JUnit test runner with AllTests configuration
  • Google Common’s collection testing framework
  • Custom TestListGenerator implementation
  • Feature-based test suite configuration
  • Support for CollectionSize and CollectionFeature specifications

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive feature coverage through systematic test generation
  • Clear separation of test data generation and test execution
  • Explicit feature declaration for tested capabilities
  • Complementary testing approach with static test cases
  • Proper test suite naming and organization

google/gson

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

            
package com.google.gson;

import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.TestListGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.ListFeature;
import java.util.List;
import junit.framework.Test;
import org.junit.runner.RunWith;
import org.junit.runners.AllTests;

/**
 * Dynamic {@link ListTestSuiteBuilder List test suite} for {@link JsonArray#asList()}. This
 * complements {@link JsonArrayAsListTest}, which can cover some cases which are not covered here,
 * e.g. making sure changes in the {@code List} view are visible in the {@code JsonArray}.
 */
@RunWith(AllTests.class)
public class JsonArrayAsListSuiteTest {
  private static class ListGenerator implements TestListGenerator<JsonElement> {
    @Override
    public SampleElements<JsonElement> samples() {
      return new SampleElements<JsonElement>(
          JsonNull.INSTANCE,
          new JsonPrimitive(true),
          new JsonPrimitive("test"),
          new JsonArray(),
          new JsonObject());
    }

    @Override
    public JsonElement[] createArray(int length) {
      return new JsonElement[length];
    }

    @Override
    public Iterable<JsonElement> order(List<JsonElement> insertionOrder) {
      return insertionOrder;
    }

    @Override
    public List<JsonElement> create(Object... elements) {
      JsonArray array = new JsonArray();
      // This is not completely accurate: Because there is no way to directly construct JsonArray or
      // its List view with existing elements, this has to add the elements individually with
      // `List#add`
      var list = array.asList();
      for (Object element : elements) {
        list.add((JsonElement) element);
      }
      return list;
    }
  }

  // Special method recognized by JUnit's `AllTests` runner
  public static Test suite() {
    return ListTestSuiteBuilder.using(new ListGenerator())
        .withFeatures(
            CollectionSize.ANY,
            CollectionFeature.ALLOWS_NULL_QUERIES,
            CollectionFeature.RESTRICTS_ELEMENTS, // List only allows JsonElement
            CollectionFeature.SUPPORTS_ADD,
            ListFeature.REMOVE_OPERATIONS,
            ListFeature.SUPPORTS_ADD_WITH_INDEX,
            ListFeature.SUPPORTS_SET)
        .named("JsonArray#asList")
        .createTestSuite();
  }
}