Back to Repositories

Testing Property Mapping Configuration in HikariCP

A comprehensive test suite for the PropertyElf utility in HikariCP, focusing on property-based object configuration and validation. These tests verify the proper mapping of property values to object attributes and handle error cases for invalid class specifications.

Test Coverage Overview

The test suite provides thorough coverage of PropertyElf’s property-to-object mapping functionality.

Key areas tested include:
  • String property mapping to object fields
  • Primitive type conversion (short)
  • Array type handling (char arrays)
  • Class instantiation from string properties
  • Error handling for invalid class specifications

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate property mapping outcomes. The implementation employs a mock TestObject class to verify various data type conversions and object instantiation patterns.

Framework features utilized:
  • JUnit @Test annotations
  • Multiple assertion types (assertEquals, assertArrayEquals, assertNotSame)
  • Exception testing with try-catch blocks

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Custom mock objects (TestObject)
  • Java Properties API
  • Runtime exception handling
  • Property-based configuration testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolation of test cases
  • Comprehensive assertion coverage
  • Proper exception handling validation
  • Clear test method naming
  • Efficient test setup with Properties initialization
  • Verification of both positive and negative test cases

brettwooldridge/hikaricp

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

            
package com.zaxxer.hikari.util;

import org.junit.Test;
import com.zaxxer.hikari.mocks.TestObject;

import java.util.Properties;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;

public class PropertyElfTest
{
   @Test
   public void setTargetFromProperties() throws Exception
   {
      Properties properties = new Properties();
      properties.setProperty("string", "aString");
      properties.setProperty("testObject", "com.zaxxer.hikari.mocks.TestObject");
      properties.setProperty("shortRaw", "1");
      properties.setProperty("charArray", "aCharArray");
      TestObject testObject = new TestObject();
      PropertyElf.setTargetFromProperties(testObject, properties);
      assertEquals("aString", testObject.getString());
      assertEquals((short) 1, testObject.getShortRaw());
      assertArrayEquals("aCharArray".toCharArray(), testObject.getCharArray());
      assertEquals(com.zaxxer.hikari.mocks.TestObject.class, testObject.getTestObject().getClass());
      assertNotSame(testObject, testObject.getTestObject());
   }

   @Test
   public void setTargetFromPropertiesNotAClass() throws Exception
   {
      Properties properties = new Properties();
      properties.setProperty("string", "aString");
      properties.setProperty("testObject", "it is not a class");
      TestObject testObject = new TestObject();
      try {
         PropertyElf.setTargetFromProperties(testObject, properties);
         fail("Could never come here");
      }
      catch (RuntimeException e) {
         assertEquals("argument type mismatch", e.getCause().getMessage());
      }
   }
}