Skip to content

Commit fc0683a

Browse files
committed
Add typed access test for ElementAdapter.
1 parent 8afde96 commit fc0683a

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

httprpc-client/src/test/java/org/httprpc/xml/ElementAdapterTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.httprpc.xml;
1616

17+
import org.httprpc.beans.BeanAdapter;
1718
import org.junit.jupiter.api.Test;
1819
import org.w3c.dom.Document;
1920
import org.xml.sax.SAXException;
@@ -30,7 +31,6 @@
3031

3132
public class ElementAdapterTest {
3233
@Test
33-
@SuppressWarnings("unchecked")
3434
public void testElementAdapter() throws ParserConfigurationException, SAXException, IOException {
3535
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
3636

@@ -46,6 +46,12 @@ public void testElementAdapter() throws ParserConfigurationException, SAXExcepti
4646

4747
ElementAdapter elementAdapter = new ElementAdapter(document.getDocumentElement());
4848

49+
testUntypedAccess(elementAdapter);
50+
testTypedAccess(elementAdapter);
51+
}
52+
53+
@SuppressWarnings("unchecked")
54+
private void testUntypedAccess(ElementAdapter elementAdapter) {
4955
assertEquals("A", elementAdapter.get("@a"));
5056

5157
Map<String, ?> map = (Map<String, ?>)elementAdapter.get("map");
@@ -75,7 +81,36 @@ public void testElementAdapter() throws ParserConfigurationException, SAXExcepti
7581

7682
assertEquals("3", item3.get("@d"));
7783
assertEquals("ghi", item3.toString());
84+
}
85+
86+
private void testTypedAccess(ElementAdapter elementAdapter) {
87+
TestInterface testInterface = BeanAdapter.adapt(elementAdapter, TestInterface.class);
88+
89+
assertEquals("A", testInterface.getA());
90+
91+
TestInterface.MapInterface map = testInterface.getMap();
92+
93+
assertEquals("B", map.getB1());
94+
assertEquals("two", map.getB2());
95+
96+
TestInterface.MapInterface.ListInterface list = map.getList();
97+
98+
assertEquals("C", list.getC());
99+
100+
List<String> stringItems = list.getStringItems();
101+
102+
assertEquals(3, stringItems.size());
103+
104+
assertEquals("abc", stringItems.get(0));
105+
assertEquals("déf", stringItems.get(1));
106+
assertEquals("ghi", stringItems.get(2));
107+
108+
List<Map<String, ?>> mapItems = list.getMapItems();
109+
110+
assertEquals(3, mapItems.size());
78111

79-
// TODO Test interface binding
112+
assertEquals("1", mapItems.get(0).get("@d"));
113+
assertEquals("2", mapItems.get(1).get("@d"));
114+
assertEquals("3", mapItems.get(2).get("@d"));
80115
}
81116
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.xml;
16+
17+
import org.httprpc.beans.Key;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public interface TestInterface {
23+
interface MapInterface {
24+
interface ListInterface {
25+
@Key("@c")
26+
String getC();
27+
28+
@Key("item*")
29+
List<String> getStringItems();
30+
31+
@Key("item*")
32+
List<Map<String, ?>> getMapItems();
33+
}
34+
35+
@Key("@b")
36+
String getB1();
37+
38+
@Key("b")
39+
String getB2();
40+
41+
ListInterface getList();
42+
}
43+
44+
@Key("@a")
45+
String getA();
46+
47+
MapInterface getMap();
48+
}

0 commit comments

Comments
 (0)