📄 basicdynabeantestcase.java
字号:
* Test getSimpleProperty on a int property. */ public void testGetSimpleInt() { try { Object value = bean.get("intProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Integer)); assertEquals("Got correct value", ((Integer) value).intValue(), (int) 123); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test getSimpleProperty on a long property. */ public void testGetSimpleLong() { try { Object value = bean.get("longProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Long)); assertEquals("Got correct value", ((Long) value).longValue(), (long) 321); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test getSimpleProperty on a short property. */ public void testGetSimpleShort() { try { Object value = bean.get("shortProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Short)); assertEquals("Got correct value", ((Short) value).shortValue(), (short) 987); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test getSimpleProperty on a String property. */ public void testGetSimpleString() { try { Object value = bean.get("stringProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof String)); assertEquals("Got correct value", (String) value, "This is a string"); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test <code>contains()</code> method for mapped properties. */ public void testMappedContains() { try { assertTrue("Can see first key", bean.contains("mappedProperty", "First Key")); } catch (Throwable t) { fail("Exception: " + t); } try { assertTrue("Can not see unknown key", !bean.contains("mappedProperty", "Unknown Key")); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test <code>remove()</code> method for mapped properties. */ public void testMappedRemove() { try { assertTrue("Can see first key", bean.contains("mappedProperty", "First Key")); bean.remove("mappedProperty", "First Key"); assertTrue("Can not see first key", !bean.contains("mappedProperty", "First Key")); } catch (Throwable t) { fail("Exception: " + t); } try { assertTrue("Can not see unknown key", !bean.contains("mappedProperty", "Unknown Key")); bean.remove("mappedProperty", "Unknown Key"); assertTrue("Can not see unknown key", !bean.contains("mappedProperty", "Unknown Key")); } catch (Throwable t) { fail("Exception: " + t); } } /** * Test serialization and deserialization. */ public void testSerialization() { // Serialize the test bean ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(bean); oos.flush(); oos.close(); } catch (Exception e) { fail("Exception during serialization: " + e); } // Deserialize the test bean try { bean = null; ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); bean = (DynaBean) ois.readObject(); bais.close(); } catch (Exception e) { fail("Exception during deserialization: " + e); } // Confirm property values testGetDescriptorArguments(); testGetDescriptorBoolean(); testGetDescriptorDouble(); testGetDescriptorFloat(); testGetDescriptorInt(); testGetDescriptorLong(); testGetDescriptorSecond(); testGetDescriptorShort(); testGetDescriptorString(); testGetDescriptors(); testGetIndexedArguments(); testGetIndexedValues(); testGetMappedArguments(); testGetMappedValues(); testGetSimpleArguments(); testGetSimpleBoolean(); testGetSimpleDouble(); testGetSimpleFloat(); testGetSimpleInt(); testGetSimpleLong(); testGetSimpleShort(); testGetSimpleString(); testMappedContains(); testMappedRemove(); // Ensure that we can create a new instance of the same DynaClass try { bean = bean.getDynaClass().newInstance(); } catch (Exception e) { fail("Exception creating new instance: " + e); } testGetDescriptorArguments(); testGetDescriptorBoolean(); testGetDescriptorDouble(); testGetDescriptorFloat(); testGetDescriptorInt(); testGetDescriptorLong(); testGetDescriptorSecond(); testGetDescriptorShort(); testGetDescriptorString(); testGetDescriptors(); } /** * Corner cases on setIndexedProperty invalid arguments. */ public void testSetIndexedArguments() { try { bean.set("intArray", -1, new Integer(0)); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IndexOutOfBoundsException"); } } /** * Positive and negative tests on setIndexedProperty valid arguments. */ public void testSetIndexedValues() { Object value = null; try { bean.set("intArray", 0, new Integer(1)); value = (Integer) bean.get("intArray", 0); assertNotNull("Returned new value 0", value); assertTrue("Returned Integer new value 0", value instanceof Integer); assertEquals("Returned correct new value 0", 1, ((Integer) value).intValue()); } catch (Throwable t) { fail("Threw " + t); } try { bean.set("intIndexed", 1, new Integer(11)); value = (Integer) bean.get("intIndexed", 1); assertNotNull("Returned new value 1", value); assertTrue("Returned Integer new value 1", value instanceof Integer); assertEquals("Returned correct new value 1", 11, ((Integer) value).intValue()); } catch (Throwable t) { fail("Threw " + t); } try { bean.set("listIndexed", 2, "New Value 2"); value = (String) bean.get("listIndexed", 2); assertNotNull("Returned new value 2", value); assertTrue("Returned String new value 2", value instanceof String); assertEquals("Returned correct new value 2", "New Value 2", (String) value); } catch (Throwable t) { fail("Threw " + t); } try { bean.set("stringArray", 3, "New Value 3"); value = (String) bean.get("stringArray", 3); assertNotNull("Returned new value 3", value); assertTrue("Returned String new value 3", value instanceof String); assertEquals("Returned correct new value 3", "New Value 3", (String) value); } catch (Throwable t) { fail("Threw " + t); } try { bean.set("stringIndexed", 4, "New Value 4"); value = (String) bean.get("stringIndexed", 4); assertNotNull("Returned new value 4", value); assertTrue("Returned String new value 4", value instanceof String); assertEquals("Returned correct new value 4", "New Value 4", (String) value); } catch (Throwable t) { fail("Threw " + t); } } /** * Positive and negative tests on setMappedProperty valid arguments. */ public void testSetMappedValues() { try { bean.set("mappedProperty", "First Key", "New First Value"); assertEquals("Can replace old value", "New First Value", (String) bean.get("mappedProperty", "First Key")); } catch (Throwable t) { fail("Finding fourth value threw " + t); } try { bean.set("mappedProperty", "Fourth Key", "Fourth Value"); assertEquals("Can set new value", "Fourth Value", (String) bean.get("mappedProperty", "Fourth Key")); } catch (Throwable t) { fail("Finding fourth value threw " + t); } } /** * Test setSimpleProperty on a boolean property. */ public void testSetSimpleBoolean() { try { boolean oldValue = ((Boolean) bean.get("booleanProperty")).booleanValue(); boolean newValue = !oldValue; bean.set("booleanProperty", new Boolean(newValue)); assertTrue("Matched new value", newValue == ((Boolean) bean.get("booleanProperty")).booleanValue()); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a double property. */ public void testSetSimpleDouble() { try { double oldValue = ((Double) bean.get("doubleProperty")).doubleValue(); double newValue = oldValue + 1.0; bean.set("doubleProperty", new Double(newValue)); assertEquals("Matched new value", newValue, ((Double) bean.get("doubleProperty")).doubleValue(), (double) 0.005); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a float property. */ public void testSetSimpleFloat() { try { float oldValue = ((Float) bean.get("floatProperty")).floatValue(); float newValue = oldValue + (float) 1.0; bean.set("floatProperty", new Float(newValue)); assertEquals("Matched new value", newValue, ((Float) bean.get("floatProperty")).floatValue(), (float) 0.005); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a int property. */ public void testSetSimpleInt() { try { int oldValue = ((Integer) bean.get("intProperty")).intValue(); int newValue = oldValue + 1; bean.set("intProperty", new Integer(newValue)); assertEquals("Matched new value", newValue, ((Integer) bean.get("intProperty")).intValue()); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a long property. */ public void testSetSimpleLong() { try { long oldValue = ((Long) bean.get("longProperty")).longValue(); long newValue = oldValue + 1; bean.set("longProperty", new Long(newValue)); assertEquals("Matched new value", newValue, ((Long) bean.get("longProperty")).longValue()); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a short property. */ public void testSetSimpleShort() { try { short oldValue = ((Short) bean.get("shortProperty")).shortValue(); short newValue = (short) (oldValue + 1); bean.set("shortProperty", new Short(newValue)); assertEquals("Matched new value", newValue, ((Short) bean.get("shortProperty")).shortValue()); } catch (Throwable e) { fail("Exception: " + e); } } /** * Test setSimpleProperty on a String property. */ public void testSetSimpleString() { try { String oldValue = (String) bean.get("stringProperty"); String newValue = oldValue + " Extra Value"; bean.set("stringProperty", newValue); assertEquals("Matched new value", newValue, (String) bean.get("stringProperty")); } catch (Throwable e) { fail("Exception: " + e); } } // ------------------------------------------------------ Protected Methods /** * Create and return a <code>DynaClass</code> instance for our test * <code>DynaBean</code>. */ protected DynaClass createDynaClass() { int intArray[] = new int[0]; String stringArray[] = new String[0]; DynaClass dynaClass = new BasicDynaClass ("TestDynaClass", null, new DynaProperty[]{ new DynaProperty("booleanProperty", Boolean.TYPE), new DynaProperty("booleanSecond", Boolean.TYPE), new DynaProperty("doubleProperty", Double.TYPE), new DynaProperty("floatProperty", Float.TYPE), new DynaProperty("intArray", intArray.getClass()), new DynaProperty("intIndexed", intArray.getClass()), new DynaProperty("intProperty", Integer.TYPE), new DynaProperty("listIndexed", List.class), new DynaProperty("longProperty", Long.TYPE), new DynaProperty("mappedProperty", Map.class), new DynaProperty("mappedIntProperty", Map.class), new DynaProperty("nullProperty", String.class), new DynaProperty("shortProperty", Short.TYPE), new DynaProperty("stringArray", stringArray.getClass()), new DynaProperty("stringIndexed", stringArray.getClass()), new DynaProperty("stringProperty", String.class), }); return (dynaClass); } /** * Base for testGetDescriptorXxxxx() series of tests. * * @param name Name of the property to be retrieved * @param type Expected class type of this property */ protected void testGetDescriptorBase(String name, Class type) { try { DynaProperty descriptor = bean.getDynaClass().getDynaProperty(name); assertNotNull("Got descriptor", descriptor); assertEquals("Got correct type", type, descriptor.getType()); } catch (Throwable t) { fail("Threw an exception: " + t); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -