Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capability to instantiate an object based on the String class name (#…
…1074) It is usefull when you want to set the MetricsTackerFactory from a property.
- Loading branch information
1 parent
471e27e
commit 5d1ed1c
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.zaxxer.hikari.mocks; | ||
|
||
public class TestObject | ||
{ | ||
private TestObject testObject; | ||
private String string; | ||
|
||
public void setTestObject(TestObject testObject) | ||
{ | ||
this.testObject = testObject; | ||
} | ||
|
||
public void setString(String string) | ||
{ | ||
this.string = string; | ||
} | ||
|
||
public TestObject getTestObject() | ||
{ | ||
return testObject; | ||
} | ||
|
||
public String getString() | ||
{ | ||
return string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.zaxxer.hikari.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import com.zaxxer.hikari.mocks.TestObject; | ||
|
||
import java.util.Properties; | ||
|
||
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"); | ||
TestObject testObject = new TestObject(); | ||
PropertyElf.setTargetFromProperties(testObject, properties); | ||
assertEquals("aString", testObject.getString()); | ||
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()); | ||
} | ||
} | ||
} |