📄 testattribute.java
字号:
}
/**
* check that the attribute can return the correct parent element
*/
public void test_TCM__OrgJdomElement_getParent() {
final Attribute attribute = new Attribute("test", "value");
assertNull("attribute returned parent when there was none", attribute.getParent());
final Element element = new Element("element");
element.setAttribute(attribute);
assertNotNull("no parent element found", attribute.getParent());
assertEquals("invalid parent element", attribute.getParent(), element);
}
/**
* check that the attribute can return the correct document
*/
public void test_TCM__OrgJdomDocument_getDocument() {
final Attribute attribute = new Attribute("test", "value");
assertNull("attribute returned document when there was none", attribute.getDocument());
final Element element = new Element("element");
element.setAttribute(attribute);
assertNull("attribute returned document when there was none", attribute.getDocument());
final Document document = new Document(element);
assertEquals("invalid document", attribute.getDocument(), document);
}
/**
* Test that an independantly created Namespace and one
* retrieved from an Attribute create with the same namespace
* parameters are the same namespace.
*/
public void test_TCM__OrgJdomNamespace_getNamespace() {
final Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place");
final Attribute attr = new Attribute("test", "value", ns);
final Namespace ns2 = Namespace.getNamespace("prefx", "http://some.other.place");
assertTrue("incorrect Namespace", attr.getNamespace().equals(ns2));
}
/**
* Test that an Attribute returns it's correct name.
*/
public void test_TCM__String_getName() {
final Attribute attr = new Attribute("test", "value");
assertTrue("incorrect attribute name", attr.getName().equals("test"));
}
/**
* Test that an Attribute returns the correct Namespace prefix.
*/
public void test_TCM__String_getNamespacePrefix() {
final Namespace namespace = Namespace.getNamespace("prefx", "http://some.other.place");
final Attribute attribute = new Attribute("test", "value", namespace);
assertTrue("incorrect prefix", attribute.getNamespacePrefix().equals("prefx"));
}
/**
* Test that an Attribute returns the correct Namespace URI.
*/
public void test_TCM__String_getNamespaceURI() {
final Namespace namespace = Namespace.getNamespace("prefx", "http://some.other.place");
final Attribute attribute = new Attribute("test", "value", namespace);
assertTrue("incorrect URI", attribute.getNamespaceURI().equals("http://some.other.place"));
}
/**
* Tests that an Attribute returns the correct Qualified Name.
*/
public void test_TCM__String_getQualifiedName() {
final String prefix = "prefx";
final String uri = "http://some.other.place";
final Namespace namespace = Namespace.getNamespace(prefix, uri);
final String attributeName = "test";
final String attributeQName = prefix + ':' + attributeName;
final String attributeValue = "value";
final Attribute qulifiedAttribute = new Attribute(attributeName, attributeValue, namespace);
assertEquals("incorrect qualified name", qulifiedAttribute.getQualifiedName(), attributeQName);
final Attribute attribute = new Attribute(attributeName, attributeValue);
assertEquals("incorrect qualified name", attribute.getQualifiedName(), attributeName);
}
/**
* Test that Attribute returns the correct serialized form.
*/
public void test_TCM__String_getSerializedForm() {
/* noop because the method is deprecated
Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place");
Attribute attr = new Attribute("test", "value", ns);
String serialized = attr.getSerializedForm();
assertTrue("incorrect serialized form", serialized.equals("prefx:test=\"value\""));
*/
}
/**
* Test that an Attribute returns the correct value.
*/
public void test_TCM__String_getValue() {
final Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place");
final Attribute attr = new Attribute("test", "value", ns);
assertTrue("incorrect value", attr.getValue().equals("value"));
}
/**
* Test that the toString function works according to the
* JDOM spec.
*/
public void test_TCM__String_toString() {
//expected value
final Namespace ns = Namespace.getNamespace("prefx", "http://some.other.place");
final Attribute attr = new Attribute("test", "value", ns);
String str= attr.toString();
assertTrue("incorrect toString form", str.equals("[Attribute: prefx:test=\"value\"]"));
}
/**
* Test that the detach() function works according to the JDOM spec.
*
* @see Attribute#detach()
*/
public void test_TCM___detach() {
final Element element = new Element("element");
element.setAttribute("name", "value");
final Attribute attribute = element.getAttribute("name");
// should never occur, just to be sure
assertNotNull("no attribute found", attribute);
assertNotNull("no parent for attribute found", attribute.getParent());
attribute.detach();
assertNull("attribute has still a parent", attribute.getParent());
}
public void testSerialization() {
serialization_default();
}
private void serialization_default() {
final String attributeName = "test";
final String attributeValue = "value";
final Attribute attribute = new Attribute(attributeName, attributeValue);
final Attribute serializedAttribute = deSerialize(attribute);
assertTrue("incorrect name in serialized attribute", serializedAttribute.getName().equals(attributeName));
assertTrue("incorrect value in serialized attribute", serializedAttribute.getValue().equals(attributeValue));
assertEquals("incoorect attribute type in serialized attribute", serializedAttribute.getAttributeType(), Attribute.UNDECLARED_TYPE);
assertEquals("incorrect Namespace in serialized attribute", serializedAttribute.getNamespace(), Namespace.NO_NAMESPACE);
}
private void serialization_Namespace() {
final String prefix = "prefx";
final String uri = "http://some.other.place";
final Namespace namespace = Namespace.getNamespace(prefix, uri);
final String attributeName = "test";
final String attributeValue = "value";
final Attribute attribute = new Attribute(attributeName, attributeValue, namespace);
final Attribute serializedAttribute = deSerialize(attribute);
assertTrue("incorrect name in serialized attribute", serializedAttribute.getName().equals(attributeName));
assertTrue("incorrect value in serialized attribute", serializedAttribute.getValue().equals(attributeValue));
assertEquals("incoorect attribute type in serialized attribute", serializedAttribute.getAttributeType(), Attribute.UNDECLARED_TYPE);
assertTrue("incorrect prefix in serialized attribute", serializedAttribute.getNamespacePrefix().equals(prefix));
assertTrue("incorrect qualified name in serialized attribute", serializedAttribute.getQualifiedName().equals(prefix + ':' + attributeName));
assertTrue("incorrect Namespace URI in serialized attribute", serializedAttribute.getNamespaceURI().equals(uri));
assertEquals("incorrect Namespace in serialized attribute", serializedAttribute.getNamespace(), namespace);
}
private Attribute deSerialize(final Attribute attribute) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
try {
objectOutputStream.writeObject(attribute);
}
catch(final IOException ioException) {
fail("unable to serialize object" + ioException);
}
finally {
try {
objectOutputStream.close();
}
catch(final IOException ioException) {
fail("failed to close object stream while serializing object" + ioException);
}
}
}
catch(final IOException ioException) {
fail("unable to serialize object" + ioException);
}
finally {
try {
outputStream.close();
}
catch(final IOException ioException) {
fail("failed to close output stream while serializing object" + ioException);
}
}
final byte[] bytes = outputStream.toByteArray();
final InputStream inputStream = new ByteArrayInputStream(bytes);
try {
final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
try {
return (Attribute) objectInputStream.readObject();
}
finally {
try {
objectInputStream.close();
}
catch(final IOException ioException) {
fail("failed to close object stream while deserializing object" + ioException);
}
}
}
catch(final IOException ioException) {
fail("unable to deserialize object" + ioException);
}
catch(final ClassNotFoundException classNotFoundException) {
fail("unable to deserialize object" + classNotFoundException);
}
finally {
try{
inputStream.close();
}
catch(final IOException ioException) {
fail("failed to close output stream while serializing object" + ioException);
}
}
return null;
}
public void testInfinity() throws DataConversionException {
Attribute infinity = new Attribute("name", "Infinity");
Attribute neginfinity = new Attribute("name", "-Infinity");
Attribute inf = new Attribute("name", "INF");
Attribute neginf = new Attribute("name", "-INF");
assertEquals(infinity.getDoubleValue(), Double.POSITIVE_INFINITY);
assertEquals(neginfinity.getDoubleValue(), Double.NEGATIVE_INFINITY);
assertEquals(inf.getDoubleValue(), Double.POSITIVE_INFINITY);
assertEquals(neginf.getDoubleValue(), Double.NEGATIVE_INFINITY);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -