View Javadoc
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   import static org.junit.Assert.assertFalse;
6   import static org.junit.Assert.assertTrue;
7   
8   import java.util.Map;
9   
10  import javax.xml.parsers.DocumentBuilderFactory;
11  
12  import net.avcompris.binding.annotation.XPath;
13  import net.avcompris.binding.dom.DomBinder;
14  import net.avcompris.binding.dom.impl.DefaultDomBinder;
15  
16  import org.junit.Test;
17  import org.w3c.dom.Document;
18  
19  /**
20   * tests that match the APT documentation.
21   * 
22   * @author David Andrianavalontsalama
23   */
24  public class NamingConventionExamplesTest {
25  
26  	@Test
27  	public void testNamingConventions_example001() throws Exception {
28  
29  		final Document document = DocumentBuilderFactory.newInstance()
30  				.newDocumentBuilder().parse(
31  						createTmpFileFromCommentsAroundThisMethod());
32  
33  		final DomBinder binder = new DefaultDomBinder();
34  
35  		final Books books = binder.bind(document, Books.class);
36  
37  		//<books>
38  		//   <book title="Treasure Island" lang="en">
39  		//       <author> Robert    Louis Stevenson</author>
40  		//   </book>
41  		//   <book title="L'Île au trésor" lang="fr">
42  		//   </book>
43  		//</books>
44  
45  		assertEquals("Treasure Island", books.getBooks()[0].getTitle());
46  		assertEquals(" Robert    Louis Stevenson", books.getBooks()[0]
47  				.getAuthor());
48  		assertFalse(books.getBooks()[0].isNullAuthor());
49  		assertEquals(1, books.getBooks()[0].sizeOfAuthor());
50  		assertEquals(26, books.getBooks()[0].getAuthorLength());
51  		assertEquals(26, books.getBooks()[0].getAuthorLengthSafe());
52  		assertEquals("Robert Louis Stevenson", books.getBooks()[0]
53  				.getAuthorNormalized());
54  
55  		assertEquals("L'Île au trésor", books.getBooks()[1].getTitle());
56  		assertEquals("", books.getBooks()[1].getAuthor());
57  		assertTrue(books.getBooks()[1].isNullAuthor());
58  		assertEquals(0, books.getBooks()[1].sizeOfAuthor());
59  		assertEquals(0, books.getBooks()[1].getAuthorLengthSafe());
60  		assertEquals("", books.getBooks()[1].getAuthorNormalized());
61  	}
62  
63  	@Test(expected = IllegalArgumentException.class)
64  	public void testNamingConventions_example001_crash() throws Exception {
65  
66  		final Document document = DocumentBuilderFactory
67  				.newInstance()
68  				.newDocumentBuilder()
69  				.parse(
70  						createTmpFileFromCommentsAroundThisMethod("testNamingConventions_example001"));
71  
72  		final DomBinder binder = new DefaultDomBinder();
73  
74  		final Books books = binder.bind(document, Books.class);
75  
76  		books.getBooks()[1].getAuthorLength(); // kaboom!
77  	}
78  
79  	@XPath("/books")
80  	interface Books {
81  
82  		@XPath("book")
83  		Book[] getBooks();
84  
85  		interface Book {
86  
87  			@XPath("@title")
88  			String getTitle();
89  
90  			@XPath("@lang")
91  			String getLang();
92  
93  			@XPath("author")
94  			String getAuthor();
95  
96  			boolean isNullAuthor();
97  
98  			int sizeOfAuthor();
99  
100 			@XPath(value = "author", function = "normalize-space()")
101 			String getAuthorNormalized();
102 
103 			@XPath(value = "author", function = "string-length()")
104 			int getAuthorLength();
105 
106 			@XPath(value = "author", function = "string-length()", failFunction = "0")
107 			int getAuthorLengthSafe();
108 		}
109 	}
110 
111 	@Test
112 	public void testNamingConventions_example002() throws Exception {
113 
114 		final Document document = DocumentBuilderFactory.newInstance()
115 				.newDocumentBuilder().parse(
116 						createTmpFileFromCommentsAroundThisMethod());
117 
118 		final DomBinder binder = new DefaultDomBinder();
119 
120 		final Map<String, BooksWithMap.Book> books = binder.bind(document,
121 				BooksWithMap.class).getBooks();
122 
123 		//<books>
124 		//   <book title="Treasure Island" lang="en">
125 		//       <author> Robert    Louis Stevenson</author>
126 		//   </book>
127 		//   <book title="L'Île au trésor" lang="fr">
128 		//       <author> R. L.           Stevenson</author>
129 		//   </book>
130 		//</books>
131 
132 		assertEquals(2, books.size());
133 		assertEquals("Treasure Island", books.get("Treasure Island").getTitle());
134 
135 		final Map<String, BooksWithMap.Book> booksComplicated = books.values()
136 				.iterator().next().getParent().getBooksComplicated();
137 
138 		assertEquals(2, booksComplicated.size());
139 		assertEquals("Treasure Island", booksComplicated.get("1.en").getTitle());
140 		assertEquals("L'Île au trésor", booksComplicated.get("2.fr").getTitle());
141 	}
142 
143 	@XPath("/books")
144 	public interface BooksWithMap {
145 
146 		@XPath(value = "book", //
147 		mapKeysXPath = "@title", mapKeysType = String.class, //
148 		mapValuesXPath = ".", mapValuesType = Book.class)
149 		Map<String, Book> getBooks();
150 
151 		@XPath(value = "book", //
152 		mapKeysXPath = ".", //
153 		mapKeysFunction = "concat(1 + count(preceding-sibling::book), '.', @lang)", //
154 		mapKeysType = String.class, //
155 		mapValuesXPath = ".", mapValuesType = Book.class)
156 		Map<String, Book> getBooksComplicated();
157 
158 		interface Book {
159 
160 			@XPath("@title")
161 			String getTitle();
162 
163 			@XPath("@lang")
164 			String getLang();
165 
166 			@XPath("author")
167 			String getAuthor();
168 
169 			@XPath("..")
170 			BooksWithMap getParent();
171 		}
172 	}
173 }