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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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());
}
}
}