OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3329 ae02f08e...
[xerial/xerial-core.git] / src / test / java / org / xerial / lens / ObjectLensTest.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2009 Taro L. Saito
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *--------------------------------------------------------------------------*/
16 //--------------------------------------
17 // XerialJ
18 //
19 // ObjectLensTest.java
20 // Since: May 19, 2009 12:23:25 PM
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.xerial.lens;
26
27 import static org.junit.Assert.*;
28
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.TreeMap;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.xerial.util.Pair;
37 import org.xerial.util.log.Logger;
38
39 public class ObjectLensTest
40 {
41
42     private static Logger _logger = Logger.getLogger(ObjectLensTest.class);
43
44     @Before
45     public void setUp() throws Exception
46     {}
47
48     @After
49     public void tearDown() throws Exception
50     {}
51
52     @Test
53     public void pickPairedName() throws Exception
54     {
55         Pair<String, String> p = ObjectLens.pickRelationName("Invoice_Order");
56         assertEquals("invoice", p.getFirst());
57         assertEquals("order", p.getSecond());
58
59         p = ObjectLens.pickRelationName("LineItem_Order");
60         assertEquals("lineitem", p.getFirst());
61         assertEquals("order", p.getSecond());
62
63         p = ObjectLens.pickRelationName("gene_JSON");
64         assertEquals("gene", p.getFirst());
65         assertEquals("json", p.getSecond());
66
67     }
68
69     @Test
70     public void pickPropertyName() throws Exception
71     {
72         String c = ObjectLens.pickPropertyName("addSomething");
73         assertEquals("something", c);
74
75         c = ObjectLens.pickPropertyName("addSomethingImportant");
76         assertEquals("somethingimportant", c);
77
78         c = ObjectLens.pickPropertyName("add");
79         assertEquals("", c);
80
81     }
82
83     @Test
84     public void canonicalNameTest() throws Exception
85     {
86         assertEquals("itemrgb", ObjectLens.getCanonicalParameterName("itemRgb"));
87         assertEquals("itemref", ObjectLens.getCanonicalParameterName("item_ref"));
88         assertEquals("helloworld", ObjectLens.getCanonicalParameterName("Hello World"));
89         assertEquals("helloworld", ObjectLens.getCanonicalParameterName("Hello-World"));
90
91     }
92
93     public static class ExtMap extends TreeMap<Integer, String>
94     {
95         /**
96          * 
97          */
98         private static final long serialVersionUID = 1L;
99
100         public String name = "ext-map";
101
102         @Override
103         public boolean equals(Object o)
104         {
105             if (!(o instanceof ExtMap))
106                 return false;
107
108             ExtMap other = (ExtMap) o;
109
110             return name.equals(other.name) && super.equals(other);
111
112         }
113     }
114
115     @Test
116     public void mapTest() throws Exception
117     {
118         ExtMap extMap = new ExtMap();
119         extMap.put(1, "hello");
120         extMap.put(10, "world");
121         String json = ObjectLens.toJSON(extMap);
122         _logger.debug(json);
123
124         ExtMap extMap2 = Lens.loadJSON(ExtMap.class, new StringReader(json));
125         _logger.debug(ObjectLens.toJSON(extMap2));
126
127         assertEquals(extMap, extMap2);
128     }
129
130     public static class ExtList extends ArrayList<Integer>
131     {
132         /**
133          * 
134          */
135         private static final long serialVersionUID = 1L;
136         public String name = "ext-list";
137
138         @Override
139         public boolean equals(Object o)
140         {
141             if (!(o instanceof ExtList))
142                 return false;
143
144             ExtList other = (ExtList) o;
145
146             return name.equals(other.name) && super.equals(other);
147
148         }
149     }
150
151     @Test
152     public void arrayTest() throws Exception
153     {
154         ExtList extList = new ExtList();
155
156         extList.add(10);
157         extList.add(14);
158
159         String json = ObjectLens.toJSON(extList);
160         _logger.debug(json);
161
162         ExtList extList2 = Lens.loadJSON(ExtList.class, new StringReader(json));
163         _logger.debug(ObjectLens.toJSON(extList2));
164
165         assertEquals(extList, extList2);
166
167     }
168
169 }