OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / test / java / org / xerial / silk / SilkWriterTest.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2009 Taro L. Saito\r
3  *\r
4  *  Licensed under the Apache License, Version 2.0 (the "License");\r
5  *  you may not use this file except in compliance with the License.\r
6  *  You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  *  Unless required by applicable law or agreed to in writing, software\r
11  *  distributed under the License is distributed on an "AS IS" BASIS,\r
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  *  See the License for the specific language governing permissions and\r
14  *  limitations under the License.\r
15  *--------------------------------------------------------------------------*/\r
16 //--------------------------------------\r
17 // XerialJ\r
18 //\r
19 // SilkWriterTest.java\r
20 // Since: Jul 28, 2009 12:08:53 PM\r
21 //\r
22 // $URL$\r
23 // $Author$\r
24 //--------------------------------------\r
25 package org.xerial.silk;\r
26 \r
27 import static org.junit.Assert.*;\r
28 \r
29 import java.io.StringReader;\r
30 import java.io.StringWriter;\r
31 import java.util.ArrayList;\r
32 import java.util.List;\r
33 \r
34 import org.junit.After;\r
35 import org.junit.Before;\r
36 import org.junit.Test;\r
37 import org.xerial.lens.Lens;\r
38 import org.xerial.lens.relation.TupleIndex;\r
39 import org.xerial.silk.SilkWriter.FormatConfig;\r
40 import org.xerial.util.FileResource;\r
41 import org.xerial.util.StringUtil;\r
42 import org.xerial.util.log.Logger;\r
43 \r
44 public class SilkWriterTest {\r
45     private static Logger _logger = Logger.getLogger(SilkWriterTest.class);\r
46 \r
47     @Before\r
48     public void setUp() throws Exception {}\r
49 \r
50     @After\r
51     public void tearDown() throws Exception {}\r
52 \r
53     @Test\r
54     public void toSilk() throws Exception {\r
55         StringWriter buf = new StringWriter();\r
56         SilkWriter w = new SilkWriter(buf);\r
57 \r
58         FormatConfig fc = new FormatConfig();\r
59         fc.insertSpaceAfterColon = true;\r
60         w.setFormatConfig(fc);\r
61 \r
62         w.commentLine("utgb config format");\r
63         SilkWriter config = w.node("config").attribute("version", "1.0");\r
64         config.leaf("projectName", "utgb-shell");\r
65         config.leaf("version", "2.0");\r
66         config.leaf("group", "utgb");\r
67         w.commentLine("database connection settings");\r
68 \r
69         SilkWriter dbNode = w.node("database").attribute("dbms", "sqlite").attribute("scope",\r
70                 "debug");\r
71         dbNode.leaf("address", "db/sample.db");\r
72 \r
73         SilkWriter t = w.tabDataSchema("gene").attribute("id").attribute("start").attribute("end");\r
74         t.dataLine("g1\t10\t20000");\r
75         t.dataLine("g2\t1000\t30000");\r
76 \r
77         SilkWriter g = w.node("gene").attribute("id", "g1");\r
78         g.multilineData("sequence");\r
79         g.dataLine("ACCGGCCGCCC");\r
80         g.dataLine("CCGCAGGGAAA");\r
81         w.endDocument();\r
82 \r
83         String s = buf.toString();\r
84         _logger.info(s);\r
85 \r
86         TreeWalkLog l1 = new TreeWalkLog();\r
87         TreeWalkLog l2 = new TreeWalkLog();\r
88 \r
89         SilkWalker s1 = new SilkWalker(FileResource\r
90                 .open(SilkWriterTest.class, "writer-result.silk"));\r
91         SilkWalker s2 = new SilkWalker(new StringReader(s));\r
92 \r
93         s1.walk(l1);\r
94         s2.walk(l2);\r
95 \r
96         boolean cmp = TreeWalkLog.compare(l1, l2);\r
97         assertTrue(cmp);\r
98     }\r
99 \r
100     static class A {\r
101         public String name;\r
102         public boolean flag = true;\r
103         public List<B> b = new ArrayList<B>();\r
104     }\r
105 \r
106     static class B {\r
107         public int id;\r
108         public String type = "B";\r
109         public List<String> item = new ArrayList<String>();\r
110 \r
111         public B(int id) {\r
112             this.id = id;\r
113         }\r
114 \r
115     }\r
116 \r
117     @Test\r
118     public void toSilkObject() throws Exception {\r
119         StringWriter buf = new StringWriter();\r
120         SilkWriter w = new SilkWriter(buf);\r
121         w = w.node("silk");\r
122 \r
123         A a = new A();\r
124         a.name = "leo";\r
125         a.b.add(new B(10));\r
126         B b = new B(20);\r
127         a.b.add(b);\r
128 \r
129         b.item.add("hello");\r
130         b.item.add("world");\r
131 \r
132         a.b.add(new B(30));\r
133 \r
134         w.toSilk(a);\r
135         w.endDocument();\r
136 \r
137         _logger.info(buf.toString());\r
138     }\r
139 \r
140     @Test\r
141     public void tupleIndex() throws Exception {\r
142         TupleIndex i = new TupleIndex(1);\r
143         String s = Lens.toSilk(i);\r
144         _logger.info(s);\r
145     }\r
146 \r
147     @Test\r
148     public void array() throws Exception {\r
149         ArrayList<String> input = new ArrayList<String>();\r
150         input.add("hello");\r
151         input.add("world");\r
152         String s = Lens.toSilk(input);\r
153         _logger.info(s);\r
154     }\r
155 \r
156     static class Person {\r
157         public int id;\r
158         public String name;\r
159 \r
160         public Person(int id, String name) {\r
161             this.id = id;\r
162             this.name = name;\r
163         }\r
164 \r
165     }\r
166 \r
167     @Test\r
168     public void objectArray() throws Exception {\r
169 \r
170         /*\r
171          * expect\r
172          * <pre>\r
173          * -(id:1, name:leo)\r
174          * -(id:2, name:yui)\r
175          * </pre>\r
176          */\r
177         ArrayList<Person> p = new ArrayList<Person>();\r
178         p.add(new Person(1, "leo"));\r
179         p.add(new Person(2, "yui"));\r
180         String s = Lens.toSilk(p);\r
181         _logger.info(s);\r
182     }\r
183 \r
184     @Test\r
185     public void sanitize() throws Exception {\r
186 \r
187         String[] v = new String[] { "this is a node(0)'s value", "this is a node, value",\r
188                 "this is a node |value|", "this is a {node} value", "this is a node*",\r
189                 "invalid value:" };\r
190 \r
191         for (String each : v) {\r
192             String s = SilkWriter.sanitizeInLineNodeValue(each);\r
193             assertEquals(StringUtil.doubleQuote(each), s);\r
194         }\r
195 \r
196         String[] safe = new String[] { "this is a node value", "safe value 1.2.4" };\r
197 \r
198         for (String each : safe) {\r
199             String s = SilkWriter.sanitizeInLineNodeValue(each);\r
200             assertEquals(each, s);\r
201         }\r
202 \r
203     }\r
204 \r
205     @Test\r
206     public void escapeText() throws Exception {\r
207         String s = SilkWriter.escapeText("-A(id:1)\n  -B");\r
208 \r
209         String[] l = s.split("\r?\n");\r
210 \r
211         assertEquals("\\-A(id:1)", l[0]);\r
212         assertEquals("  \\-B", l[1]);\r
213 \r
214     }\r
215 \r
216     @Test\r
217     public void escapeBackSlash() throws Exception {\r
218         String e = SilkWriter.escapeText("\\-already escaped");\r
219         assertEquals("\\-already escaped", e);\r
220 \r
221     }\r
222 \r
223 }\r