OSDN Git Service

Removed BeanException class: http://code.google.com/p/xerial/issues/detail?id=26
[xerial/xerial-core.git] / src / test / java / org / xerial / util / bean / JSONStreamWalkerTest.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2008 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 // JSONStreamWalkerTest.java
20 // Since: Jan 23, 2008 10:42:40 AM
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.xerial.util.bean;
26
27 import java.io.IOException;
28 import java.io.Reader;
29
30 import org.antlr.runtime.ANTLRReaderStream;
31 import org.antlr.runtime.Token;
32 import org.junit.Test;
33 import org.xerial.core.XerialException;
34 import org.xerial.json.JSONEvent;
35 import org.xerial.json.JSONException;
36 import org.xerial.json.JSONPullParser;
37 import org.xerial.json.impl.JSONLexer;
38 import org.xerial.util.FileResource;
39 import org.xerial.util.StopWatch;
40 import org.xerial.util.bean.sample.Gene;
41 import org.xerial.util.log.Logger;
42 import org.xerial.util.tree.TreeVisitor;
43 import org.xerial.util.tree.TreeWalker;
44
45 public class JSONStreamWalkerTest
46 {
47     private static Logger _logger = Logger.getLogger(JSONStreamWalkerTest.class);
48
49     class MyVisitor implements TreeVisitor
50     {
51
52         public void finish(TreeWalker walker) throws XerialException {
53         // TODO Auto-generated method stub
54
55         }
56
57         public void init(TreeWalker walker) throws XerialException {
58         // TODO Auto-generated method stub
59
60         }
61
62         public void leaveNode(String nodeName, TreeWalker walker) throws XerialException {
63         // TODO Auto-generated method stub
64
65         }
66
67         public void visitNode(String nodeName, String nodeValue, TreeWalker walker) throws XerialException {
68         // TODO Auto-generated method stub
69
70         }
71
72         public void text(String nodeName, String nodeValue, TreeWalker walker) throws XerialException {
73         // TODO Auto-generated method stub
74
75         }
76
77     }
78
79     private StopWatch stopWatch = new StopWatch();
80
81     public Reader getSampleData() throws IOException {
82         return FileResource.open(JSONStreamWalkerTest.class, "chr1.json");
83     }
84
85     @Test
86     public void walk() throws IOException, XerialException {
87         JSONStreamWalker walker = new JSONStreamWalker(getSampleData());
88
89         stopWatch.reset();
90         walker.walk(new MyVisitor());
91         _logger.debug("walk time: " + stopWatch.getElapsedTime());
92     }
93
94     @Test
95     public void lexerPerf() throws IOException {
96         JSONLexer lexer = new JSONLexer(new ANTLRReaderStream(getSampleData()));
97         stopWatch.reset();
98         while ((lexer.nextToken() != Token.EOF_TOKEN)) {
99
100         }
101         _logger.debug("lexical analysis time: " + stopWatch.getElapsedTime());
102
103     }
104
105     @Test
106     public void pullParserPerf() throws IOException, JSONException {
107         JSONPullParser parser = new JSONPullParser(getSampleData());
108         stopWatch.reset();
109         while (parser.next() != JSONEvent.EndJSON) {
110
111         }
112         _logger.debug("pull parsing time: " + stopWatch.getElapsedTime());
113     }
114
115     @Test
116     public void loadJSONPerf() throws XerialException, IOException {
117         stopWatch.reset();
118         BeanUtil.loadJSON(getSampleData(), Gene.class, new BeanHandler<Gene>() {
119
120             public void handle(Gene bean) throws Exception {
121             // TODO Auto-generated method stub
122
123             }
124         });
125         _logger.debug("loadJSON time: " + stopWatch.getElapsedTime());
126     }
127
128 }