My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package fourty2ia.builder;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static org.junit.Assert.*;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import java.io.StringWriter;

public class XmlDocumentBuilderTest {

@Test
public void testBeginEndElement() throws Exception {
XmlDocumentBuilder builder = new XmlDocumentBuilder();

Document doc = builder
.beginElement("foo")
.beginElement("bar")
.endElement()
.endElement()
.build();

assertNotNull(doc);
Element root = doc.getDocumentElement();
assertNotNull(root);
assertEquals("foo",root.getNodeName());
assertEquals(1,root.getChildNodes().getLength());
assertEquals("bar",root.getChildNodes().item(0).getNodeName());
}

@Test(expected = IllegalStateException.class)
public void testTooManyEnds() throws Exception {
XmlDocumentBuilder builder = new XmlDocumentBuilder();

builder.beginElement("foo")
// oh oh, missing begin
.endElement()
.endElement()
.build();
}

@Test
public void testAttr() throws Exception {
XmlDocumentBuilder builder = new XmlDocumentBuilder();

Document doc = builder
.beginElement("foo")
.attr("bar","whazzup")
.attr("number",1)
.endElement()
.build();

assertNotNull(doc);
Element root = doc.getDocumentElement();
assertNotNull(root);
assertEquals("whazzup",root.getAttribute("bar"));
assertEquals("1",root.getAttribute("number"));
assertFalse(root.hasAttribute("notHere"));
}

@Test
public void testComplexDocument() throws Exception {
XmlDocumentBuilder builder = new XmlDocumentBuilder();
Document doc = builder
.beginElement("html")
.beginElement("head")
.endElement()
.beginElement("body")
.beginElement("h1")
.text("42 Incorrect Answers")
.endElement()
.text("Just click this ")
.beginElement("a")
.attr("href","http://42-incorrect-answers.blogspot.com/")
.text("link")
.endElement()
.endElement()
.endElement()
.build();
assertEquals(
"<html><head/><body><h1>42 Incorrect Answers</h1>" +
"Just click this <a href=\"http://42-incorrect-answers.blogspot.com/\">" +
"link</a></body></html>",
convertDocToString(doc));
}

private String convertDocToString(Document doc) throws TransformerException {

TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// we don't want <META http-equiv="Content-Type"... to ease testing
trans.setOutputProperty(OutputKeys.METHOD,"xml");

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
return sw.toString();
}
}

Change log

r4 by l.girndt on Jul 21, 2009   Diff
initial example for the builder blog entry
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3780 bytes, 108 lines
Powered by Google Project Hosting