1 package net.avcompris.binding.dom.siteExamples;
2
3 import static com.avcompris.util.junit.JUnitUtils.createTmpFileFromCommentsAroundThisMethod;
4 import static org.junit.Assert.assertEquals;
5
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import net.avcompris.binding.annotation.Namespaces;
9 import net.avcompris.binding.annotation.XPath;
10 import net.avcompris.binding.dom.DomBinder;
11 import net.avcompris.binding.dom.impl.DefaultDomBinder;
12
13 import org.junit.Test;
14 import org.w3c.dom.Document;
15
16
17
18
19
20
21 public class NamespacesExamplesTest {
22
23 @Test
24 public void testGettingStarted_example001() throws Exception {
25
26 final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
27 .newInstance();
28 documentBuilderFactory.setNamespaceAware(true);
29 final Document document = documentBuilderFactory.newDocumentBuilder()
30 .parse(createTmpFileFromCommentsAroundThisMethod());
31
32 final DomBinder binder = new DefaultDomBinder();
33
34 final Book book = binder.bind(document, Book.class);
35
36 assertEquals("Treasure Island", book.getTitle());
37 assertEquals(1883, book.getPublishYear());
38 assertEquals(2, book.getAuthorNames().length);
39 assertEquals("Robert Louis Stevenson", book.getAuthorNames()[0]);
40 assertEquals("R. L. Stevenson", book.getAuthorNames()[1]);
41 assertEquals(2, book.getReferences().length);
42 assertEquals("en", book.getReferences()[0].getLang());
43 assertEquals("fr", book.getReferences()[1].getLang());
44 }
45
46
47
48
49
50
51
52
53
54
55
56 @Namespaces({
57 "xmlns:m=http://mybook", "xmlns:people=a/b/c/"
58 })
59 @XPath("/m:book")
60 public interface Book {
61
62 @XPath("@title")
63 String getTitle();
64
65 @XPath("m:publishedIn")
66 int getPublishYear();
67
68 @XPath("m:author/people:name")
69 String[] getAuthorNames();
70
71 @XPath("m:reference")
72 MyBookReference[] getReferences();
73
74 interface MyBookReference {
75
76 @XPath("@url")
77 String getUrl();
78
79 @XPath("@lang")
80 String getLang();
81 }
82 }
83 }