Back to Repositories

Testing Transaction Isolation Level Processing in HikariCP

This test suite validates the transaction isolation level functionality in HikariCP’s UtilityElf class. It ensures proper handling of both string-based and integer-based isolation level specifications while verifying error conditions.

Test Coverage Overview

The test suite provides comprehensive coverage of the UtilityElf’s transaction isolation level functionality.

Key areas tested include:
  • Valid transaction isolation level string conversion
  • Numeric isolation level parsing
  • Invalid input handling for both string and integer values
  • SQL Server specific isolation level support

Implementation Analysis

The testing approach employs JUnit’s annotation-based test structure with explicit expected exception handling. Each test case follows a clear Arrange-Act-Assert pattern, with focused assertions validating specific isolation level scenarios.

The implementation leverages JUnit’s @Test annotations for both positive and negative test cases, demonstrating proper exception handling patterns.

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Assert methods for equality verification
  • Expected exception testing using @Test annotation
  • Integration with HikariCP’s UtilityElf class
  • SQL Server specific isolation level testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices including clear test method naming, proper exception handling verification, and isolation of test cases.

Notable practices include:
  • Descriptive test method names using ‘should’ prefix
  • Separate test cases for positive and negative scenarios
  • Explicit exception testing
  • Clean and focused test methods

brettwooldridge/hikaricp

src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java

            
/*
 * Copyright (C) 2013, 2019 Brett Wooldridge
 *
 * 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.zaxxer.hikari.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class UtilityElfTest
{
   @Test
   public void shouldReturnValidTransactionIsolationLevel()
   {
      //Act
      int expectedLevel = UtilityElf.getTransactionIsolation("TRANSACTION_SQL_SERVER_SNAPSHOT_ISOLATION_LEVEL");

      //Assert
      assertEquals(expectedLevel, 4096);
   }

   @Test(expected = IllegalArgumentException.class)
   public void shouldThrowWhenInvalidTransactionNameGiven()
   {
      //Act
      UtilityElf.getTransactionIsolation("INVALID_TRANSACTION");
   }

   @Test
   public void shouldReturnTransationIsolationLevelFromInteger()
   {
      int expectedLevel = UtilityElf.getTransactionIsolation("4096");
      assertEquals(expectedLevel, 4096);
   }

   @Test(expected = IllegalArgumentException.class)
   public void shouldThrowWhenInvalidTransactionIntegerGiven()
   {
      //Act
      UtilityElf.getTransactionIsolation("9999");
   }
}