Back to Repositories

Testing JSON Tree Conversion Implementation in google/gson

This test suite validates the JSON tree conversion functionality in Google’s Gson library, focusing on the toJsonTree methods and their handling of Java objects to JSON element transformations.

Test Coverage Overview

The test suite provides comprehensive coverage of Gson’s JSON tree conversion capabilities.

Key areas tested include:
  • Basic object to JSON tree conversion
  • Type-specific JSON tree conversion
  • JSON tree to string serialization
  • Null value handling in JSON trees
The suite particularly focuses on testing primitive type conversions and inheritance scenarios.

Implementation Analysis

The testing approach employs JUnit framework with systematic verification of JSON structure and content.

Key implementation patterns include:
  • Setup using @Before annotation for Gson instance initialization
  • Custom assertion utilities for JSON content validation
  • Type-based conversion testing using inheritance
  • Primitive and complex object conversion verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Google Truth assertion library
  • Custom BagOfPrimitives test class
  • Gson library for JSON processing
  • Custom assertion methods for JSON validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable approaches include:
  • Isolated test setup using @Before
  • Comprehensive edge case coverage
  • Clear test method naming
  • Custom assertion utilities for readable verification
  • Proper test organization with inheritance testing

google/gson

gson/src/test/java/com/google/gson/functional/JsonTreeTest.java

            
/*
 * Copyright (C) 2009 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.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;

/**
 * Functional tests for {@link Gson#toJsonTree(Object)} and {@link Gson#toJsonTree(Object,
 * java.lang.reflect.Type)}
 *
 * @author Inderjeet Singh
 * @author Joel Leitch
 */
public class JsonTreeTest {
  private Gson gson;

  @Before
  public void setUp() throws Exception {
    gson = new Gson();
  }

  @Test
  public void testToJsonTree() {
    BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, "foo");
    JsonElement json = gson.toJsonTree(bag);
    assertThat(json.isJsonObject()).isTrue();
    JsonObject obj = json.getAsJsonObject();
    Set<Entry<String, JsonElement>> children = obj.entrySet();
    assertThat(children).hasSize(4);
    assertContains(obj, new JsonPrimitive(10L));
    assertContains(obj, new JsonPrimitive(5));
    assertContains(obj, new JsonPrimitive(false));
    assertContains(obj, new JsonPrimitive("foo"));
  }

  @Test
  public void testToJsonTreeObjectType() {
    SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
    JsonElement json = gson.toJsonTree(bag, BagOfPrimitives.class);
    assertThat(json.isJsonObject()).isTrue();
    JsonObject obj = json.getAsJsonObject();
    Set<Entry<String, JsonElement>> children = obj.entrySet();
    assertThat(children).hasSize(4);
    assertContains(obj, new JsonPrimitive(10L));
    assertContains(obj, new JsonPrimitive(5));
    assertContains(obj, new JsonPrimitive(false));
    assertContains(obj, new JsonPrimitive("foo"));
  }

  @Test
  public void testJsonTreeToString() {
    SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
    String json1 = gson.toJson(bag);
    JsonElement jsonElement = gson.toJsonTree(bag, SubTypeOfBagOfPrimitives.class);
    String json2 = gson.toJson(jsonElement);
    assertThat(json2).isEqualTo(json1);
  }

  @Test
  public void testJsonTreeNull() {
    BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, null);
    JsonObject jsonElement = (JsonObject) gson.toJsonTree(bag, BagOfPrimitives.class);
    assertThat(jsonElement.has("stringValue")).isFalse();
  }

  private static void assertContains(JsonObject json, JsonPrimitive child) {
    for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
      JsonElement node = entry.getValue();
      if (node.isJsonPrimitive()) {
        if (node.getAsJsonPrimitive().equals(child)) {
          return;
        }
      }
    }
    throw new AssertionError("Does not contain " + child);
  }

  private static class SubTypeOfBagOfPrimitives extends BagOfPrimitives {
    @SuppressWarnings("unused")
    float f = 1.2F;

    public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) {
      super(l, i, b, string);
      this.f = f;
    }
  }
}