1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 // SilkWriterTest.java
\r
20 // Since: Jul 28, 2009 12:08:53 PM
\r
24 //--------------------------------------
\r
25 package org.xerial.silk;
\r
27 import static org.junit.Assert.*;
\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
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
44 public class SilkWriterTest {
\r
45 private static Logger _logger = Logger.getLogger(SilkWriterTest.class);
\r
48 public void setUp() throws Exception {}
\r
51 public void tearDown() throws Exception {}
\r
54 public void toSilk() throws Exception {
\r
55 StringWriter buf = new StringWriter();
\r
56 SilkWriter w = new SilkWriter(buf);
\r
58 FormatConfig fc = new FormatConfig();
\r
59 fc.insertSpaceAfterColon = true;
\r
60 w.setFormatConfig(fc);
\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
69 SilkWriter dbNode = w.node("database").attribute("dbms", "sqlite").attribute("scope",
\r
71 dbNode.leaf("address", "db/sample.db");
\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
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
83 String s = buf.toString();
\r
86 TreeWalkLog l1 = new TreeWalkLog();
\r
87 TreeWalkLog l2 = new TreeWalkLog();
\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
96 boolean cmp = TreeWalkLog.compare(l1, l2);
\r
101 public String name;
\r
102 public boolean flag = true;
\r
103 public List<B> b = new ArrayList<B>();
\r
108 public String type = "B";
\r
109 public List<String> item = new ArrayList<String>();
\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
125 a.b.add(new B(10));
\r
129 b.item.add("hello");
\r
130 b.item.add("world");
\r
132 a.b.add(new B(30));
\r
137 _logger.info(buf.toString());
\r
141 public void tupleIndex() throws Exception {
\r
142 TupleIndex i = new TupleIndex(1);
\r
143 String s = Lens.toSilk(i);
\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
156 static class Person {
\r
158 public String name;
\r
160 public Person(int id, String name) {
\r
168 public void objectArray() throws Exception {
\r
173 * -(id:1, name:leo)
\r
174 * -(id:2, name:yui)
\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
185 public void sanitize() throws Exception {
\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
191 for (String each : v) {
\r
192 String s = SilkWriter.sanitizeInLineNodeValue(each);
\r
193 assertEquals(StringUtil.doubleQuote(each), s);
\r
196 String[] safe = new String[] { "this is a node value", "safe value 1.2.4" };
\r
198 for (String each : safe) {
\r
199 String s = SilkWriter.sanitizeInLineNodeValue(each);
\r
200 assertEquals(each, s);
\r
206 public void escapeText() throws Exception {
\r
207 String s = SilkWriter.escapeText("-A(id:1)\n -B");
\r
209 String[] l = s.split("\r?\n");
\r
211 assertEquals("\\-A(id:1)", l[0]);
\r
212 assertEquals(" \\-B", l[1]);
\r
217 public void escapeBackSlash() throws Exception {
\r
218 String e = SilkWriter.escapeText("\\-already escaped");
\r
219 assertEquals("\\-already escaped", e);
\r