Back to Repositories

Testing Stream Writer Implementation in Google Gson

This test suite validates the functionality of the Streams utility class in the Google Gson library, specifically focusing on the writerForAppendable method and its various string manipulation capabilities. The tests ensure robust handling of character streams and string operations essential for JSON processing.

Test Coverage Overview

The test suite provides comprehensive coverage of the writerForAppendable functionality, examining character and string manipulation operations.

  • Tests single character appending and writing
  • Validates Unicode character handling
  • Verifies string segment operations
  • Confirms null value handling behavior
  • Tests array-based writing operations

Implementation Analysis

The testing approach employs JUnit 4 framework with Google Truth assertions for precise verification of string manipulation outcomes. The implementation systematically tests Writer interface methods including append(), write(), and their overloaded variants.

The test structure follows a sequential pattern, building up a complex string through various operations and validating the final output against an expected value.

Technical Details

  • JUnit 4 test framework
  • Google Truth assertion library
  • StringBuilder for output verification
  • Custom Writer implementation testing
  • IOException handling
  • Unicode character support validation

Best Practices Demonstrated

The test exhibits several testing best practices for stream handling and string manipulation.

  • Comprehensive edge case coverage including null handling
  • Clear test method organization
  • Explicit exception testing
  • Validation of state preservation after close()
  • Unicode character handling verification

google/gson

gson/src/test/java/com/google/gson/internal/StreamsTest.java

            
/*
 * Copyright (C) 2022 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.internal;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import java.io.IOException;
import java.io.Writer;
import org.junit.Test;

public class StreamsTest {
  @Test
  public void testWriterForAppendable() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    Writer writer = Streams.writerForAppendable(stringBuilder);

    writer.append('a');
    writer.append('\u1234');
    writer.append("test");
    writer.append(null); // test custom null handling mandated by `append`
    writer.append("abcdef", 2, 4);
    writer.append(null, 1, 3); // test custom null handling mandated by `append`
    writer.append(',');

    writer.write('a');
    writer.write('\u1234');
    // Should only consider the 16 low-order bits
    writer.write(0x4321_1234);
    writer.append(',');

    writer.write("chars".toCharArray());
    assertThrows(NullPointerException.class, () -> writer.write((char[]) null));

    writer.write("chars".toCharArray(), 1, 2);
    assertThrows(NullPointerException.class, () -> writer.write((char[]) null, 1, 2));
    writer.append(',');

    writer.write("string");
    assertThrows(NullPointerException.class, () -> writer.write((String) null));

    writer.write("string", 1, 2);
    assertThrows(NullPointerException.class, () -> writer.write((String) null, 1, 2));

    String actualOutput = stringBuilder.toString();
    assertThat(actualOutput).isEqualTo("a\u1234testnullcdul,a\u1234\u1234,charsha,stringtr");

    writer.flush();
    writer.close();

    // flush() and close() calls should have had no effect
    assertThat(stringBuilder.toString()).isEqualTo(actualOutput);
  }
}