/**
* @Created Nov 24, 2010 9:47:55 AM
*
* @author cry30
*/
package com.philip.journal.common;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.philip.journal.common.mock.BeanUtilsSubject;
import com.philip.journal.core.JournalTestCommon;
/**
* Test class for {@link com.philip.journal.common.BeanUtils}.
*/
public class BeanUtilsTest extends JournalTestCommon {
/** RTFC. */
private static final int TEST_INT = 667;
/** RTFC. */
private static final String TEST_STRING = "TestString";
/** Refactored commonly used case. */
private static final String CASE_NULL_ARG = "Case 1: Null argument.";
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPrimitive(java.lang.Class)} .
*
* Case 1: Expected result.
*/
@Test
public void testGetPrimitive1() {
final Map<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>();
map.put(Byte.class, Byte.TYPE);
map.put(Short.class, Short.TYPE);
map.put(Integer.class, Integer.TYPE);
map.put(Long.class, Long.TYPE);
map.put(Float.class, Float.TYPE);
map.put(Double.class, Double.TYPE);
map.put(Boolean.class, Boolean.TYPE);
map.put(Character.class, Character.TYPE);
for (final Class<?> next : map.keySet()) {
Assert.assertEquals("Expected output test failed.", map.get(next), BeanUtils.getPrimitive(next));
}
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPrimitive(java.lang.Class)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPrimitive2() {
BeanUtils.getPrimitive(null);
Assert.fail("Failed.");
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPrimitive(java.lang.Class)}.
*
* Case 3: No Wrapper.
*/
@Test
public void testGetPrimitive3() {
final Object nonWrapper = new Object();
Assert.assertEquals("No-wrapper test failed.", nonWrapper.getClass(),
BeanUtils.getPrimitive(nonWrapper.getClass()));
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getGetterMethodName(java.lang.String)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetGetterMethodName1() {
BeanUtils.getGetterMethodName(null);
Assert.fail("IllegalArgumentException is expected thrown.");
}
/** Case 2: Empty String argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetGetterMethodName2() {
BeanUtils.getGetterMethodName("");
Assert.fail("IllegalArgumentException is expected thrown.");
}
/** Case 3: Normal expected scenario. */
@Test
public void testGetGetterMethodName3() {
String output2 = BeanUtils.getGetterMethodName("m");
Assert.assertEquals("Normal scenario test failed.", "getM", output2);
output2 = BeanUtils.getGetterMethodName("mXXXHadooken");
Assert.assertEquals("Normal scenario test failed.", "getMXXXHadooken", output2);
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getSetterMethodName(java.lang.String)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetSetterMethodName1() {
BeanUtils.getSetterMethodName(null);
}
/** Case 2: Empty String argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetSetterMethodName2() {
BeanUtils.getSetterMethodName("");
}
/** Case 3: Normal expected scenario. */
@Test
public void testGetSetterMethodName3() {
String output2 = BeanUtils.getSetterMethodName("m");
Assert.assertEquals("1-length field test failed.", "setM", output2);
output2 = BeanUtils.getSetterMethodName("mXXXHadooken");
Assert.assertEquals("Normall method test failed.", "setMXXXHadooken", output2);
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#getProperties(java.lang.Object, java.lang.String[])} .
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPropertiesObjectStringArray1() {
BeanUtils.getProperties(null, null);
failExpectedException(IllegalArgumentException.class);
}
/**
* Requires @link com.philip.journal.common.CollectionUtils#equals(java.util.List, java.util.List)}.
*
* Test method for
* {@link com.philip.journal.common.BeanUtils#getProperties(java.lang.Object, java.lang.String[])} .
*
* Case 2: All properties.
*/
@Test
public void testGetPropertiesObjectStringArray2() {
final BeanUtilsSubject input1 = new BeanUtilsSubject();
final String[] output1 = BeanUtils.getProperties(input1, null);
final List<String> outputList1 = Arrays.asList(output1);
final List<String> expected1 = Arrays.asList(new String[] {
BeanUtilsSubject.FIELD1,
BeanUtilsSubject.FIELD2,
BeanUtilsSubject.FIELD3 });
Assert.assertSame("Case 2: All properties.", true, CollectionUtils.equals(expected1, outputList1));
}
/**
* Requires @link com.philip.journal.common.CollectionUtils#equals(java.util.List, java.util.List)}.
*
* Test method for
* {@link com.philip.journal.common.BeanUtils#getProperties(java.lang.Object, java.lang.String[])} .
*
* Case 3: With exclusion.
*/
@Test
public void testGetPropertiesObjectStringArray3() {
final BeanUtilsSubject input2 = new BeanUtilsSubject();
final String[] output2 = BeanUtils.getProperties(input2, new String[] { BeanUtilsSubject.FIELD2 });
final List<String> outputList2 = Arrays.asList(output2);
final List<String> expected2 = Arrays.asList(new String[] {
BeanUtilsSubject.FIELD3,
BeanUtilsSubject.FIELD1 });
Assert.assertSame("Case 3: With exclusion.", true, CollectionUtils.equals(expected2, outputList2));
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPropertyName(java.lang.reflect.Method)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameMethod1() {
BeanUtils.getPropertyName((java.lang.reflect.Method) null);
Assert.fail(CASE_NULL_ARG);
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPropertyName(java.lang.reflect.Method)}.
*
* Case 2: Expected result.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameMethod2() {
java.lang.reflect.Method meth;
try {
meth = Object.class.getMethod("hashCode", new Class[0]);
Assert.assertEquals("Case 2: Expected result.", "hashCode", BeanUtils.getPropertyName(meth));
} catch (final SecurityException e) {
failUnexpectedException(e);
} catch (final NoSuchMethodException e) {
failUnexpectedException(e);
}
}
/**
* Test method for {@link com.philip.journal.common.BeanUtils#getPropertyName(java.lang.String)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameString1() {
BeanUtils.getPropertyName((String) null);
Assert.fail(CASE_NULL_ARG);
}
/**
* Case 2.1: Getter lower case. <br/>
* Case 2.2: Getter Case variation.<br/>
* Case 2.3: Setter lower case. <br/>
* Case 2.4: Setter Case variation.
*/
@Test
public void testGetPropertyNameString2() {
Assert.assertEquals("Case 2.1: Lower case.", "name", BeanUtils.getPropertyName("getName"));
Assert.assertEquals("Case 2.2: Case variation.", "nAmE", BeanUtils.getPropertyName("getNAmE"));
Assert.assertEquals("Case 2.3: Setter lower case.", "name", BeanUtils.getPropertyName("setName"));
Assert.assertEquals("Case 2.4: Setter Case variation", "nAmE", BeanUtils.getPropertyName("getNAmE"));
}
/** Case 3: Method not found. */
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameString3() {
BeanUtils.getPropertyName("illegalMethod");
Assert.fail("Case 3: Method not found.");
}
/** Case 4: Method name too short. */
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameString4() {
BeanUtils.getPropertyName("get");
Assert.fail("Case 4: Method name too short.");
}
/** Case 5: Method name is an empty String. */
@Test(expected = IllegalArgumentException.class)
public void testGetPropertyNameString5() {
BeanUtils.getPropertyName("");
Assert.fail("Case 5: Method name is an empty String.");
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#getProperty(java.lang.Object, java.lang.String)}.
*
* Case 1: Null argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetProperty1() {
BeanUtils.getProperty(null, null);
Assert.fail(CASE_NULL_ARG);
}
/** Case 2: Null Property argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetProperty2() {
final BeanUtilsSubject input = new BeanUtilsSubject();
input.setField1("test0");
BeanUtils.getProperty(input, null);
Assert.fail("Case 2: Null Property argument.");
}
/** Case 3: Invalid property argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetProperty3() {
BeanUtils.getProperty(new BeanUtilsSubject(), "x412!");
Assert.fail("Case 3: Invalid property argument.");
}
/** Case 4: Non-existing property. */
@Test(expected = IllegalArgumentException.class)
public void testGetProperty4() {
BeanUtils.getProperty(new BeanUtilsSubject(), "field4");
Assert.fail("Case 4: Non-existing property.");
}
/**
* Case 5.1: Expected String type.<br/>
* Case 5.2: Expected primitive type.<br/>
* Case 5.3: Expected Map type.
*/
@Test
public void testGetProperty5() {
final BeanUtilsSubject input4 = new BeanUtilsSubject();
input4.setField1(TEST_STRING);
Assert.assertEquals("Case 5.1: Expected String type.", TEST_STRING,
BeanUtils.getProperty(input4, BeanUtilsSubject.FIELD1));
Assert.assertEquals("Case 5.2: Expected primitive type.", 0,
BeanUtils.getProperty(input4, BeanUtilsSubject.FIELD2));
Assert.assertSame("Case 5.3: Expected Map type.", null,
BeanUtils.getProperty(input4, BeanUtilsSubject.FIELD3));
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#getPropertyType(java.lang.Object, java.lang.String)}.
*
* Case 1: Null both argument.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetReturnType1() {
BeanUtils.getPropertyType(null, null);
Assert.fail("Case 1: Null both argument.");
}
/** Case 2: Null property argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetReturnType2() {
BeanUtils.getPropertyType(new Object(), null);
Assert.fail("Case 2: Null property argument.");
}
/** Case 3: Invalid property argument. */
@Test(expected = IllegalArgumentException.class)
public void testGetReturnType3() {
BeanUtils.getPropertyType(new Object(), "!@#");
Assert.fail("Case 3: Invalid property argument.");
}
/**
* Case 4.1: Expected String type.<br/>
* Case 4.2: Expected primitive type.<br/>
* Case 4.3: Expected Map type.
*/
@Test
public void testGetReturnType4() {
final BeanUtilsSubject input3 = new BeanUtilsSubject();
Assert.assertEquals("Case 4.1: Expected String type.", String.class,
BeanUtils.getPropertyType(input3, BeanUtilsSubject.FIELD1));
Assert.assertEquals("Case 4.2: Expected primitive type.", Integer.TYPE,
BeanUtils.getPropertyType(input3, BeanUtilsSubject.FIELD2));
Assert.assertEquals("Case 4.3: Expected Map type.", Map.class,
BeanUtils.getPropertyType(input3, BeanUtilsSubject.FIELD3));
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#invokeMethodSilent(java.lang.Object, java.lang.String, java.lang.Class[], java.lang.Object[])}
* .
*
* Case 1: Expected.
*/
@Test
public void testInvokeMethodSilent1() {
try {
BeanUtils.invokeMethodSilent(new BeanUtilsSubject(), "exception", null, null);
} catch (final Exception e) {
Assert.fail("Case 1: Expected.");
}
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#invokeMethod(java.lang.Object, java.lang.String, java.lang.Class[], java.lang.Object[])}
* .
*
* Case 1: Null all arguments.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvokeMethod1() {
try {
BeanUtils.invokeMethod(null, null, null, null);
Assert.fail("Case 1: Null all arguments.");
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/** Case 2: Null 2nd,3rd,4th param. */
@Test(expected = IllegalArgumentException.class)
public void testInvokeMethod2() {
try {
BeanUtils.invokeMethod(new Object(), null, null, null);
Assert.fail("Case 2: Null 2nd,3rd,4th param.");
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/** Case 3: Invalid method name parameter. */
@Test(expected = IllegalArgumentException.class)
public void testInvokeMethod3() {
try {
BeanUtils.invokeMethod(new Object(), "!@#", null, null);
Assert.fail("Case 3: Invalid method name parameter.");
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/** Case 4: Null return for void method. */
@Test
public void testInvokeMethod4() {
final BeanUtilsSubject input34 = new BeanUtilsSubject();
final String input34a = "input3a";
try {
Assert.assertNull("Case 4: Null return for void method.", BeanUtils.invokeMethod(input34, "setField1",
new Class[] { String.class }, new Object[] { input34a }));
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/** Case 5: Reference return type. */
@Test
public void testInvokeMethod5() {
final BeanUtilsSubject input = new BeanUtilsSubject();
final String inputVal = "input3a";
input.setField1(inputVal);
try {
Assert.assertEquals("Case 5: Reference return type.", inputVal,
BeanUtils.invokeMethod(input, "getField1", new Class[0], new Object[0]));
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/** Case 6: Primitive return type. */
@Test
public void testInvokeMethod6() {
final BeanUtilsSubject input5 = new BeanUtilsSubject();
try {
Assert.assertEquals("Primitive type method test failed. Equals", 0,
BeanUtils.invokeMethod(input5, "getField2", new Class[0], new Object[0]));
} catch (final InvocationTargetException e) {
failUnexpectedException(e);
}
}
/**
* Case 7: Expected exception.
*
* @throws InvocationTargetException expected exception thrown.
*/
@Test(expected = InvocationTargetException.class)
public void testInvokeMethod7() throws InvocationTargetException {
final BeanUtilsSubject input6 = new BeanUtilsSubject();
BeanUtils.invokeMethod(input6, "exception", null, null);
Assert.fail("Case 7: Expected exception.");
}
/**
* Test method for
* {@link com.philip.journal.common.BeanUtils#convertToMap(java.lang.Object, java.lang.String[])}.
*
* Case 1: Null argument.
*/
@Test
public void testConvertToMap1() {
Assert.assertNull(CASE_NULL_ARG, BeanUtils.convertToMap(null, null));
}
/**
* Case 2.1: Exclusion property count. <br/>
* Case 2.2: Primitive property type. <br/>
* Case 2.3: Map property type.
*/
@Test
public void testConvertToMap2() {
final BeanUtilsSubject inputX = new BeanUtilsSubject();
inputX.setField2(TEST_INT);
inputX.setField3(new HashMap<String, Class<?>>());
final Map<String, Object> output2 = BeanUtils.convertToMap(inputX, new String[] { BeanUtilsSubject.FIELD1 });
Assert.assertEquals("Case 2.1: Exclusion property count.", 2, output2.size());
Assert.assertEquals("Case 2.2: Primitive property type.", TEST_INT, output2.get(BeanUtilsSubject.FIELD2));
Assert.assertTrue("Case 2.3: Map property type.", output2.get(BeanUtilsSubject.FIELD3) instanceof Map);
}
/**
* Case 3.1: String property type. <br/>
* Case 3.2: Primitive property type. <br/>
* Case 3.3: Map property type.
*/
@Test
public void testConvertToMap3() {
final BeanUtilsSubject inputX = new BeanUtilsSubject();
inputX.setField2(TEST_INT);
inputX.setField3(new HashMap<String, Class<?>>());
final Map<String, Object> output3 = BeanUtils.convertToMap(inputX, null);
Assert.assertNull("Case 3.1: String property type.", output3.get(BeanUtilsSubject.FIELD1));
Assert.assertEquals("Case 3.2: Primitive property type.", TEST_INT, output3.get(BeanUtilsSubject.FIELD2));
Assert.assertTrue("Case 3.3: Map property type.", output3.get(BeanUtilsSubject.FIELD3) instanceof Map);
}
}