1 /*--------------------------------------------------------------------------
2 * Copyright 2009 Taro L. Saito
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 //--------------------------------------
19 // ObjectLensTest.java
20 // Since: May 19, 2009 12:23:25 PM
24 //--------------------------------------
25 package org.xerial.lens;
27 import static org.junit.Assert.*;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.TreeMap;
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;
39 public class ObjectLensTest
42 private static Logger _logger = Logger.getLogger(ObjectLensTest.class);
45 public void setUp() throws Exception
49 public void tearDown() throws Exception
53 public void pickPairedName() throws Exception
55 Pair<String, String> p = ObjectLens.pickRelationName("Invoice_Order");
56 assertEquals("invoice", p.getFirst());
57 assertEquals("order", p.getSecond());
59 p = ObjectLens.pickRelationName("LineItem_Order");
60 assertEquals("lineitem", p.getFirst());
61 assertEquals("order", p.getSecond());
63 p = ObjectLens.pickRelationName("gene_JSON");
64 assertEquals("gene", p.getFirst());
65 assertEquals("json", p.getSecond());
70 public void pickPropertyName() throws Exception
72 String c = ObjectLens.pickPropertyName("addSomething");
73 assertEquals("something", c);
75 c = ObjectLens.pickPropertyName("addSomethingImportant");
76 assertEquals("somethingimportant", c);
78 c = ObjectLens.pickPropertyName("add");
84 public void canonicalNameTest() throws Exception
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"));
93 public static class ExtMap extends TreeMap<Integer, String>
98 private static final long serialVersionUID = 1L;
100 public String name = "ext-map";
103 public boolean equals(Object o)
105 if (!(o instanceof ExtMap))
108 ExtMap other = (ExtMap) o;
110 return name.equals(other.name) && super.equals(other);
116 public void mapTest() throws Exception
118 ExtMap extMap = new ExtMap();
119 extMap.put(1, "hello");
120 extMap.put(10, "world");
121 String json = ObjectLens.toJSON(extMap);
124 ExtMap extMap2 = Lens.loadJSON(ExtMap.class, new StringReader(json));
125 _logger.debug(ObjectLens.toJSON(extMap2));
127 assertEquals(extMap, extMap2);
130 public static class ExtList extends ArrayList<Integer>
135 private static final long serialVersionUID = 1L;
136 public String name = "ext-list";
139 public boolean equals(Object o)
141 if (!(o instanceof ExtList))
144 ExtList other = (ExtList) o;
146 return name.equals(other.name) && super.equals(other);
152 public void arrayTest() throws Exception
154 ExtList extList = new ExtList();
159 String json = ObjectLens.toJSON(extList);
162 ExtList extList2 = Lens.loadJSON(ExtList.class, new StringReader(json));
163 _logger.debug(ObjectLens.toJSON(extList2));
165 assertEquals(extList, extList2);