Back to Repositories

Testing Java Version Detection Implementation in google/gson

This test suite validates Java version detection and parsing functionality in the Gson library, focusing on accurate identification of major Java versions from various version string formats. The tests ensure compatibility across different Java releases and vendor-specific version patterns.

Test Coverage Overview

The test suite provides comprehensive coverage of Java version parsing functionality across multiple Java releases (6 through 10) and vendor-specific formats.

Key areas tested include:
  • Current runtime Java version verification
  • Standard version string parsing
  • Vendor-specific version formats (Oracle, OpenJDK, Debian)
  • Early access and internal build version strings
  • Legacy and modern version number patterns

Implementation Analysis

The testing approach uses JUnit to systematically verify the JavaVersion class’s version parsing capabilities. Each test method focuses on a specific Java version or version string format, utilizing Google Truth assertions for clear and expressive test validations.

The implementation employs a modular pattern with individual test cases for each major Java version, including edge cases and vendor-specific formats.

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Google Truth assertion library
  • Test cases borrowed from Presto database testing suite
  • Covers Java versions 6 through 10
  • Includes validation of various version string patterns

Best Practices Demonstrated

The test suite exemplifies several testing best practices including clear test method naming, comprehensive version coverage, and thorough documentation.

Notable practices include:
  • Single responsibility principle in test methods
  • Comprehensive documentation of test cases
  • Reference to external version format specifications
  • Systematic coverage of edge cases
  • Clear separation of test scenarios

google/gson

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

            
/*
 * Copyright (C) 2017 The Gson authors
 *
 * 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 org.junit.Test;

/**
 * Unit and functional tests for {@link JavaVersion}
 *
 * @author Inderjeet Singh
 */
public class JavaVersionTest {
  // Borrowed some of test strings from
  // https://github.com/prestodb/presto/blob/master/presto-main/src/test/java/com/facebook/presto/server/TestJavaVersion.java

  @Test
  public void testGetMajorJavaVersion() {
    // Gson currently requires at least Java 8
    assertThat(JavaVersion.getMajorJavaVersion()).isAtLeast(8);
  }

  @Test
  public void testJava6() {
    // http://www.oracle.com/technetwork/java/javase/version-6-141920.html
    assertThat(JavaVersion.parseMajorJavaVersion("1.6.0")).isEqualTo(6);
  }

  @Test
  public void testJava7() {
    // http://www.oracle.com/technetwork/java/javase/jdk7-naming-418744.html
    assertThat(JavaVersion.parseMajorJavaVersion("1.7.0")).isEqualTo(7);
  }

  @Test
  public void testJava8() {
    assertThat(JavaVersion.parseMajorJavaVersion("1.8")).isEqualTo(8);
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0")).isEqualTo(8);
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0_131")).isEqualTo(8);
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0_60-ea")).isEqualTo(8);
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0_111-internal")).isEqualTo(8);

    // openjdk8 per https://github.com/AdoptOpenJDK/openjdk-build/issues/93
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0-internal")).isEqualTo(8);
    assertThat(JavaVersion.parseMajorJavaVersion("1.8.0_131-adoptopenjdk")).isEqualTo(8);
  }

  @Test
  public void testJava9() {
    // Legacy style
    assertThat(JavaVersion.parseMajorJavaVersion("9.0.4")).isEqualTo(9); // Oracle JDK 9
    // Debian as reported in https://github.com/google/gson/issues/1310
    assertThat(JavaVersion.parseMajorJavaVersion("9-Debian")).isEqualTo(9);

    // New style
    assertThat(JavaVersion.parseMajorJavaVersion("9-ea+19")).isEqualTo(9);
    assertThat(JavaVersion.parseMajorJavaVersion("9+100")).isEqualTo(9);
    assertThat(JavaVersion.parseMajorJavaVersion("9.0.1+20")).isEqualTo(9);
    assertThat(JavaVersion.parseMajorJavaVersion("9.1.1+20")).isEqualTo(9);
  }

  @Test
  public void testJava10() {
    assertThat(JavaVersion.parseMajorJavaVersion("10.0.1")).isEqualTo(10); // Oracle JDK 10.0.1
  }

  @Test
  public void testUnknownVersionFormat() {
    assertThat(JavaVersion.parseMajorJavaVersion("Java9")).isEqualTo(6); // unknown format
  }
}