package org.jdom.test.cases; import junit.framework.*; import org.jdom.Attribute; import org.jdom.Namespace; import org.jdom.DataConversionException; public class TestCase_Attribute extends TestCase { public TestCase_Attribute( String name ) { super(name); } public void setUp(){ } public void tearDown(){ } public void testConstructor_Attribute__String_String(){ Attribute attr = new Attribute("test", "value"); assert("incorrect attribute name", attr.getName().equals("test")); assert("incorrect attribute value", attr.getValue().equals("value")); Namespace ns = attr.getNamespace(); assert("incorrect default Namespace", ns == Namespace.NO_NAMESPACE); } public void testConstructor_Attribute__String_String_Namespace(){ Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place"); Attribute attr = new Attribute("test", "value", ns); assert("incorrect attribute name", attr.getName().equals("test")); assert("incorrect attribute value", attr.getValue().equals("value")); assert("incorrect prefix", attr.getNamespacePrefix().equals("prefx")); assert("incorrect qualified name", attr.getQualifiedName().equals("prefx:test")); assert("incorrect Namespace URI", attr.getNamespaceURI().equals("http://some.other.place")); assert("incorrect Namespace", ns == attr.getNamespace()); } public void testConstructor_Attribute__String_String_String_String(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect attribute name", attr.getName().equals("test")); assert("incorrect attribute value", attr.getValue().equals("value")); assert("incorrect prefix", attr.getNamespacePrefix().equals("prefx")); assert("incorrect qualified name", attr.getQualifiedName().equals("prefx:test")); assert("incorrect Namespace URI", attr.getNamespaceURI().equals("http://some.other.place")); Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place"); assert("incorrect Namespace", ns == attr.getNamespace()); } public void testMethod_boolean_equals__Object(){ Attribute attr = new Attribute("test", "value"); Object ob = (Object)attr; assert("object not equal to attribute", attr.equals(ob)); } /* don't noop a test public void testMethod_void_wait(){ } */ /* don't noop a test public void testMethod_void_wait__long(){ } */ /* don't noop a test public void testMethod_void_wait__long_int(){ } */ public void testMethod_boolean_getBooleanValue(){ Attribute attr = new Attribute("test", "true"); try { assert("incorrect boolean true value", attr.getBooleanValue()); attr.setValue("false"); assert("incorrect boolean false value", !attr.getBooleanValue()); attr.setValue("TRUE"); assert("incorrect boolean TRUE value", attr.getBooleanValue()); attr.setValue("FALSE"); assert("incorrect boolean FALSE value", !attr.getBooleanValue()); } catch (DataConversionException e) { assert("couldn't convert boolean value", false); } try { attr.setValue("foo"); assert("incorrectly returned boolean from non boolean value", attr.getBooleanValue()); } catch (DataConversionException e) { assert(true); } } public void testMethod_boolean_getBooleanValue__boolean(){ Attribute attr = new Attribute("test", "true"); assert("incorrect boolean value", attr.getBooleanValue(true)); assert("incorrect boolean value", attr.getBooleanValue(false)); attr.setValue("false"); assert("incorrect boolean false value", !attr.getBooleanValue(true)); assert("incorrect boolean false value", !attr.getBooleanValue(false)); attr.setValue("foo"); assert("incorrectly returned boolean from default", attr.getBooleanValue(true)); assert("incorrectly returned boolean from default", !attr.getBooleanValue(false)); } public void testMethod_char_getCharValue(){ Attribute attr = new Attribute("test", "t"); char ch = 't'; try { assertEquals("incorrect char conversion", attr.getCharValue(), ch); } catch (DataConversionException e) { assert("couldn't convert to char", false); } } public void testMethod_char_getCharValue__char(){ Attribute attr = new Attribute("test", "true"); char ch = 't'; assertEquals("incorrect char conversion", attr.getCharValue('x'), ch); attr.setValue(""); assertEquals("incorrect char conversion", attr.getCharValue('t'), ch); assert("incorrect char conversion", !(attr.getCharValue('x') == ch)); } public void testMethod_Class_getClass(){ Attribute attr = new Attribute("test", "value"); assert("incorrect class returned from getClass", attr.getClass().getName().equals("org.jdom.Attribute")); } public void testMethod_double_getDoubleValue(){ Attribute attr = new Attribute("test", "10000000d"); double dbl = 10000000d; try { assert("incorrect double conversion", attr.getDoubleValue() == dbl); } catch (DataConversionException e) { assert("couldn't convert to double", false); } //test an invalid double attr.setValue("10000000.aq"); try { attr.getDoubleValue(); assert("incorrect double conversion from non double", false); } catch (DataConversionException e) { assert("couldn't convert to double", true); } } public void testMethod_double_getDoubleValue__double() { Attribute attr = new Attribute("test", "1000d"); double dbl = 1000d; assert("incorrect double conversion", attr.getDoubleValue(0) == dbl); //test an invalid double attr.setValue("10000000.aq"); assert("incorrect double default", attr.getDoubleValue(0) == 0); assert("incorrect double default failure", !(attr.getDoubleValue(1) == 0)); } public void testMethod_float_getFloatValue(){ Attribute attr = new Attribute("test", "1.00000009999e+10f"); float flt = 1.00000009999e+10f; try { assert("incorrect float conversion", attr.getFloatValue() == flt); } catch (DataConversionException e) { assert("couldn't convert to float", false); } //test an invalid float attr.setValue("1.00000009999e"); try { attr.getFloatValue(); assert("incorrect float conversion from non float", false); } catch (DataConversionException e) { assert("couldn't convert to float", true); } } public void testMethod_float_getFloatValue__float() { Attribute attr = new Attribute("test", "1000e-99f"); float dbl = 1000e-99f; assert("incorrect float conversion", attr.getFloatValue(0) == dbl); //test an invalid float attr.setValue("10000000.aq"); assert("incorrect float default", attr.getFloatValue(0f) == 0f); assert("incorrect float default failure", !(attr.getFloatValue(1f) == 0f)); } public void testMethod_int_getIntValue(){ Attribute attr = new Attribute("test", "1000"); int intval = 1000; try { assert("incorrect int conversion", attr.getIntValue() == intval); } catch (DataConversionException e) { assert("couldn't convert to int", false); } //test an invalid int attr.setValue("10000000.aq"); try { attr.getIntValue(); assert("incorrect int conversion from non int", false); } catch (DataConversionException e) { assert("couldn't convert to int", true); } } public void testMethod_int_getIntValue__int() { Attribute attr = new Attribute("test", "1000"); int intval = 1000; assert("incorrect int conversion", attr.getIntValue(0) == intval); //test an invalid int attr.setValue("10000000.aq"); assert("incorrect int default", attr.getIntValue(0) == 0); assert("incorrect int default failure", !(attr.getIntValue(1) == 0)); } public void testMethod_int_hashCode(){ //not sure what to test! Attribute attr = new Attribute("test", "value"); //only an exception would be a problem int i = attr.hashCode(); assert("no hasCode", i>0); } public void testMethod_long_getLongValue() { Attribute attr = new Attribute("test", "1000000"); long longval = 1000000; try { assert("incorrect long conversion", attr.getLongValue() == longval); } catch (DataConversionException e) { assert("couldn't convert to long", false); } //test an invalid long attr.setValue("100000000000000000000000000"); try { attr.getLongValue(); assert("incorrect long conversion from non long", false); } catch (DataConversionException e) { assert("couldn't convert to long", true); } } public void testMethod_long_getLongValue__long() { Attribute attr = new Attribute("test", "1000000000"); long longval = 1000000000l; assert("incorrect long conversion", attr.getLongValue(0) == longval); //test an invalid long attr.setValue("100000000000000000000000000000"); assert("incorrect long default", attr.getLongValue(0l) == 0l); assert("incorrect long default failure", !(attr.getLongValue(1l) == 0l)); } public void testMethod_Namespace_getNamespace(){ Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place"); Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect Namespace", attr.getNamespace().equals(ns)); } public void testMethod_Object_clone(){ Attribute attr = new Attribute("test", "value"); Attribute attr2 = (Attribute)attr.clone(); assert("incorrect name in clone", attr2.getName().equals("test")); assert("incorrect value in clone", attr2.getValue().equals("value")); attr = new Attribute("test", "prefx", "http://some.other.place", "value"); attr2 = (Attribute)attr.clone(); assert("incorrect name in clone", attr2.getName().equals("test")); assert("incorrect value in clone", attr2.getValue().equals("value")); assert("incorrect prefix in clone", attr2.getNamespacePrefix().equals("prefx")); assert("incorrect qualified name in clone", attr2.getQualifiedName().equals("prefx:test")); assert("incorrect Namespace URI in clone", attr2.getNamespaceURI().equals("http://some.other.place")); } public void testMethod_String_getName(){ Attribute attr = new Attribute("test", "value"); assert("incorrect attribute name", attr.getName().equals("test")); } public void testMethod_String_getNamespacePrefix(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect prefix", attr.getNamespacePrefix().equals("prefx")); } public void testMethod_String_getNamespaceURI(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect URI", attr.getNamespaceURI().equals("http://some.other.place")); } public void testMethod_String_getQualifiedName(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect qualified name", attr.getQualifiedName().equals("prefx:test")); } public void testMethod_String_getValue(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect value", attr.getValue().equals("value")); } public void testMethod_String_getValue__String(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect value", attr.getValue("foo").equals("value")); attr = new Attribute("test", ""); assert("incorrect value", attr.getValue("foo").equals("foo")); } public void testMethod_String_toSerializedForm(){ Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place"); Attribute attr = new Attribute("test", "value", ns); String serialized = attr.toSerializedForm(); assert("incorrect serialized form", serialized.equals("prefx:test=\"value\"")); } /* don't noop a test public void testMethod_void_notify(){ } */ /* don't noop a test public void testMethod_void_notifyAll(){ } */ public void testMethod_String_toString(){ //expected value Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place"); Attribute attr = new Attribute("test", "value", ns); String str= attr.toString(); assert("incorrect toString form", str.equals("[Attribute: prefx:test=\"value\"]")); } public void testMethod_void_setValue__String(){ Attribute attr = new Attribute("test", "prefx", "http://some.other.place", "value"); assert("incorrect value before set", attr.getValue().equals("value")); attr.setValue("foo"); assert("incorrect value after set", attr.getValue().equals("foo")); } }