package net.exoego.queen.xml;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.TypeInfo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
public class XAttrTest {
@Test
public void testXAttributeIsAnAttr() throws Exception {
assertThat(new XAttr("attr1", "value1"), isA(Attr.class));
}
@Test
public void testConstructor() {
final XAttr xAttr = new XAttr("name", "value");
assertThat(xAttr.getName(), is("name"));
assertThat(xAttr.getLocalName(), is("name"));
assertThat(xAttr.getValue(), is("value"));
}
@Test
public void testWithNamespaceNone() {
final XAttr xAttr = new XAttr(XNamespace.NONE, "space", "preserve");
assertThat(xAttr.getName(), is("space"));
assertThat(xAttr.getLocalName(), is("space"));
assertThat(xAttr.getValue(), is("preserve"));
assertThat(xAttr.getBaseURI(), is(nullValue()));
assertThat(xAttr.getNamespaceURI(), is(nullValue()));
assertThat(xAttr.toString(), is("space=\"preserve\""));
assertThat(xAttr.isNamespaceDeclaration(), is(false));
}
@Test
public void testWithNamespaceXML() {
final XAttr xAttr = new XAttr(XNamespace.XML, "space", "preserve");
assertThat(xAttr.getName(), is("xml:space"));
assertThat(xAttr.getLocalName(), is("space"));
assertThat(xAttr.getValue(), is("preserve"));
assertThat(xAttr.getBaseURI(), is(nullValue()));
assertThat(xAttr.getNamespaceURI(), is("http://www.w3.org/XML/1998/namespace"));
assertThat(xAttr.toString(), is("xml:space=\"preserve\""));
assertThat(xAttr.isNamespaceDeclaration(), is(false));
}
@Test
public void testWithNamespaceXMLNS() {
final XAttr xAttr = new XAttr(XNamespace.XMLNS, "space", "preserve");
assertThat(xAttr.getName(), is("space"));
assertThat(xAttr.getLocalName(), is(nullValue()));
assertThat(xAttr.getValue(), is("preserve"));
assertThat(xAttr.getBaseURI(), is(nullValue()));
assertThat(xAttr.getNamespaceURI(), is(nullValue()));
assertThat(xAttr.toString(), is("space=\"preserve\""));
assertThat(xAttr.isNamespaceDeclaration(), is(true));
}
@Test
public void testGetNodeType() throws Exception {
assertThat(new XAttr("name", "value").getType(), CoreMatchers.is(XType.Attribute));
}
@Test
public void testEscape() {
assertThat(new XAttr("abc", "<>&'\" must be escaped").toString(),
is("abc=\"<>&'" must be escaped\""));
}
@Test
public void testClone() {
final XAttr origin = new XAttr("abc", 123);
origin.appendChild(new XText("test"));
final XAttr clone = origin.cloneNode(true);
testEquality(origin, clone);
}
@Test
public void testCloneConstructor() {
final XAttr origin = new XAttr("abc", 123);
origin.appendChild(new XText("test"));
final XAttr clone = new XAttr(origin);
testEquality(origin, clone);
}
private void testEquality(final XAttr origin, final XAttr clone) {
assertThat(origin.isEqualNode(clone), is(true));
assertThat(origin.isSameNode(clone), is(false));
assertThat(origin == clone, is(false));
assertThat(origin.getName(), is(clone.getName()));
assertThat(origin.getValue(), is(clone.getValue()));
}
@Test
public void testGetSpecified() throws Exception {
assertThat(new XAttr("a", "").getSpecified(), is(true));
}
@Test
public void testSetValue() throws Exception {
final XAttr attr = new XAttr("a", 1);
assertThat(attr.getValue(), is("1"));
attr.setValue("2");
assertThat(attr.getValue(), is("2"));
}
@Test
public void testGetOwnerElement() throws Exception {
final XAttr attr = new XAttr("a", "");
assertThat(attr.getOwnerElement(), is(nullValue()));
final XElement e = new XElement("node");
e.setAttributeNode(attr);
assertThat(attr.getOwnerElement().isEqualNode(e), is(true));
}
@Test
public void testGetSchemaTypeInfo() throws Exception {
final XAttr attr = new XAttr("a", "");
final TypeInfo a = attr.getSchemaTypeInfo();
assertThat(a.getTypeName(), is(nullValue()));
assertThat(a.getTypeNamespace(), is(nullValue()));
}
}