OSDN Git Service

58a16d4623d59963833346b787d2e035dfa91c0c
[xerial/xerial-core.git] / src / test / java / org / xerial / lens / ObjectMapperTest.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 // ObjectMapperTest.java
20 // Since: May 19, 2009 1:33:50 PM
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.xerial.lens;
26
27 import static org.junit.Assert.*;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.xerial.lens.LensTest.Gene;
37 import org.xerial.lens.ObjectLensTest.PropReader;
38 import org.xerial.silk.SilkParser;
39 import org.xerial.util.FileResource;
40 import org.xerial.util.log.Logger;
41
42 public class ObjectMapperTest {
43     private static Logger _logger = Logger.getLogger(ObjectMapperTest.class);
44
45     @Before
46     public void setUp() throws Exception {}
47
48     @After
49     public void tearDown() throws Exception {}
50
51     public static class CoordinateData {
52         public String group;
53         public String name;
54         public String species;
55         public String revision;
56
57         @Override
58         public String toString() {
59             return String.format("group=%s, name=%s, species=%s, revision=%s", group, name,
60                     species, revision);
61         }
62
63     }
64
65     // query: (gene, name, start, end, strand)
66     public static class GeneData {
67         public final int id;
68         public final String name;
69         public final long start;
70         public final long end;
71         public final String strand;
72
73         private StringBuilder sequence = new StringBuilder();
74
75         public GeneData(int id, String name, long start, long end, String strand) {
76             this.id = id;
77             this.name = name;
78             this.start = start;
79             this.end = end;
80             this.strand = strand;
81         }
82
83         public void appendSequence(String seq) {
84             sequence.append(seq);
85         }
86
87         @Override
88         public String toString() {
89             return String.format("id=%d, name=%s, start=%s, end=%s, strand=%s, sequence=%s", id,
90                     name, start, end, strand, sequence.toString());
91         }
92     }
93
94     public static class GeneDB {
95         public final String description;
96
97         public GeneDB(String description) {
98             this.description = description;
99         }
100
101         public void addCoordinate_Gene(CoordinateData c, GeneData g) {
102             _logger.debug(String.format("c(%s), g(%s)", c, g));
103         }
104
105         public void addCoordinate(CoordinateData c) {
106             _logger.debug(c);
107         }
108
109         public void addGene(GeneData g) {
110             _logger.debug(g);
111         }
112
113     }
114
115     @Test
116     public void map() throws Exception {
117         ObjectMapper mapper = new ObjectMapper(GeneDB.class);
118         GeneDB gdb = mapper.map(GeneDB.class, new SilkParser(FileResource.find(
119                 ObjectMapperTest.class, "gene.silk")));
120
121         assertEquals("gene data", gdb.description);
122     }
123
124     public static enum MARK {
125         SPADE, HEART, CLOVER, DIAMOND
126     }
127
128     public static class EnumData {
129         public List<MARK> mark;
130     }
131
132     @Test
133     public void enumBind() throws Exception {
134         EnumData ret = Lens.loadSilk(EnumData.class, FileResource.find(ObjectMapperTest.class,
135                 "enum.silk"));
136         assertEquals(6, ret.mark.size());
137
138     }
139
140     public static class Read {
141         public long viewstart;
142         public long viewend;
143     }
144
145     @Test
146     public void primitiveTypeBind() throws Exception {
147         Read r = Lens.loadSilk(Read.class, FileResource.find(ObjectMapperTest.class, "long.silk"));
148         assertEquals(1721L, r.viewstart);
149         assertEquals(2871L, r.viewend);
150     }
151
152     public static class MyGene extends Gene {
153
154     }
155
156     public static class Rename {
157         ArrayList<MyGene> genes = new ArrayList<MyGene>();
158
159         public void addCoordinate_Gene(Coordinate c, MyGene g) {
160             genes.add(g);
161         }
162
163     }
164
165     @Test
166     public void rename() throws Exception {
167         Rename r = Lens.loadSilk(Rename.class, FileResource.find(ObjectMapperTest.class,
168                 "gene.silk"));
169         assertEquals(2, r.genes.size());
170
171     }
172
173     @Test
174     public void property() throws Exception {
175         PropReader p = Lens.loadSilk(PropReader.class, FileResource.open(ObjectMapperTest.class,
176                 "property.silk"));
177
178         assertEquals(2, p.prop.size());
179         assertEquals("hello", p.prop.get("db.name"));
180         assertEquals("sqlite", p.prop.get("db.type"));
181     }
182
183     public static class MapField {
184         public Map<Integer, String> id_name;
185     }
186
187     @Test
188     public void mapPutter() throws Exception {
189         MapField m = Lens.loadSilk(MapField.class, FileResource.open(ObjectMapperTest.class,
190                 "map.silk"));
191         assertNotNull(m.id_name);
192         assertEquals(2, m.id_name.size());
193         String n1 = m.id_name.get(1);
194         String n2 = m.id_name.get(2);
195         assertEquals("leo", n1);
196         assertEquals("yui", n2);
197     }
198
199     public static class Person {
200         public int id;
201         public String name;
202
203         @Override
204         public String toString() {
205             return Lens.toJSON(this);
206         }
207     }
208
209     public static class ClassRoom {
210         public String name;
211
212         @Override
213         public String toString() {
214             return Lens.toJSON(this);
215         }
216     }
217
218     public static class ComplexMapField {
219         public Map<Person, ClassRoom> classes;
220     }
221
222     @Test
223     public void complexMapField() throws Exception {
224         ComplexMapField m = Lens.loadSilk(ComplexMapField.class, FileResource.open(
225                 ObjectMapperTest.class, "map2.silk"));
226         assertEquals(2, m.classes.size());
227
228         _logger.debug(Lens.toJSON(m.classes));
229     }
230 }