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 // ObjectMapperTest.java
20 // Since: May 19, 2009 1:33:50 PM
24 //--------------------------------------
25 package org.xerial.lens;
27 import static org.junit.Assert.*;
29 import java.util.ArrayList;
30 import java.util.List;
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;
42 public class ObjectMapperTest {
43 private static Logger _logger = Logger.getLogger(ObjectMapperTest.class);
46 public void setUp() throws Exception {}
49 public void tearDown() throws Exception {}
51 public static class CoordinateData {
54 public String species;
55 public String revision;
58 public String toString() {
59 return String.format("group=%s, name=%s, species=%s, revision=%s", group, name,
65 // query: (gene, name, start, end, strand)
66 public static class GeneData {
68 public final String name;
69 public final long start;
70 public final long end;
71 public final String strand;
73 private StringBuilder sequence = new StringBuilder();
75 public GeneData(int id, String name, long start, long end, String strand) {
83 public void appendSequence(String seq) {
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());
94 public static class GeneDB {
95 public final String description;
97 public GeneDB(String description) {
98 this.description = description;
101 public void addCoordinate_Gene(CoordinateData c, GeneData g) {
102 _logger.debug(String.format("c(%s), g(%s)", c, g));
105 public void addCoordinate(CoordinateData c) {
109 public void addGene(GeneData g) {
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")));
121 assertEquals("gene data", gdb.description);
124 public static enum MARK {
125 SPADE, HEART, CLOVER, DIAMOND
128 public static class EnumData {
129 public List<MARK> mark;
133 public void enumBind() throws Exception {
134 EnumData ret = Lens.loadSilk(EnumData.class, FileResource.find(ObjectMapperTest.class,
136 assertEquals(6, ret.mark.size());
140 public static class Read {
141 public long viewstart;
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);
152 public static class MyGene extends Gene {
156 public static class Rename {
157 ArrayList<MyGene> genes = new ArrayList<MyGene>();
159 public void addCoordinate_Gene(Coordinate c, MyGene g) {
166 public void rename() throws Exception {
167 Rename r = Lens.loadSilk(Rename.class, FileResource.find(ObjectMapperTest.class,
169 assertEquals(2, r.genes.size());
174 public void property() throws Exception {
175 PropReader p = Lens.loadSilk(PropReader.class, FileResource.open(ObjectMapperTest.class,
178 assertEquals(2, p.prop.size());
179 assertEquals("hello", p.prop.get("db.name"));
180 assertEquals("sqlite", p.prop.get("db.type"));
183 public static class MapField {
184 public Map<Integer, String> id_name;
188 public void mapPutter() throws Exception {
189 MapField m = Lens.loadSilk(MapField.class, FileResource.open(ObjectMapperTest.class,
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);
199 public static class Person {
204 public String toString() {
205 return Lens.toJSON(this);
209 public static class ClassRoom {
213 public String toString() {
214 return Lens.toJSON(this);
218 public static class ComplexMapField {
219 public Map<Person, ClassRoom> classes;
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());
228 _logger.debug(Lens.toJSON(m.classes));