KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > parser > LengthParserTest


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.parser;
19
20 import java.io.*;
21
22 import org.apache.batik.test.*;
23
24 /**
25  * To test the length parser.
26  *
27  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: LengthParserTest.java,v 1.3 2004/08/18 07:16:42 vhardy Exp $
29  */

30 public class LengthParserTest extends AbstractTest {
31
32     protected String JavaDoc sourceLength;
33     protected String JavaDoc destinationLength;
34
35     protected StringBuffer JavaDoc buffer;
36     protected String JavaDoc resultLength;
37
38     /**
39      * Creates a new LengthParserTest.
40      * @param slength The length to parse.
41      * @param dlength The length after serialization.
42      */

43     public LengthParserTest(String JavaDoc slength, String JavaDoc dlength) {
44         sourceLength = slength;
45         destinationLength = dlength;
46     }
47
48     public TestReport runImpl() throws Exception JavaDoc {
49         LengthParser pp = new LengthParser();
50         pp.setLengthHandler(new TestHandler());
51
52         try {
53             pp.parse(new StringReader(sourceLength));
54         } catch (ParseException e) {
55             DefaultTestReport report = new DefaultTestReport(this);
56             report.setErrorCode("parse.error");
57             report.addDescriptionEntry("exception.text", e.getMessage());
58             report.setPassed(false);
59             return report;
60         }
61
62         if (!destinationLength.equals(resultLength)) {
63             DefaultTestReport report = new DefaultTestReport(this);
64             report.setErrorCode("invalid.parsing.events");
65             report.addDescriptionEntry("expected.text", destinationLength);
66             report.addDescriptionEntry("generated.text", resultLength);
67             report.setPassed(false);
68             return report;
69         }
70
71         return reportSuccess();
72     }
73
74     class TestHandler extends DefaultLengthHandler {
75         public TestHandler() {}
76
77         public void startLength() throws ParseException {
78             buffer = new StringBuffer JavaDoc();
79         }
80         
81         public void lengthValue(float v) throws ParseException {
82             buffer.append(v);
83         }
84
85         public void em() throws ParseException {
86             buffer.append("em");
87         }
88
89         public void ex() throws ParseException {
90             buffer.append("ex");
91         }
92
93         public void in() throws ParseException {
94             buffer.append("in");
95         }
96
97         public void cm() throws ParseException {
98             buffer.append("cm");
99         }
100
101         public void mm() throws ParseException {
102             buffer.append("mm");
103         }
104
105         public void pc() throws ParseException {
106             buffer.append("pc");
107         }
108
109         public void pt() throws ParseException {
110             buffer.append("pt");
111         }
112
113         public void px() throws ParseException {
114             buffer.append("px");
115         }
116
117         public void percentage() throws ParseException {
118             buffer.append("%");
119         }
120
121         public void endLength() throws ParseException {
122             resultLength = buffer.toString();
123         }
124     }
125 }
126
Popular Tags