KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > DigestFilterTestCase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 /* $Id: DigestFilterTestCase.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop;
21
22 import java.io.IOException JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.fop.util.DigestFilter;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35
36 /**
37  * Test case for digesting SAX filter.
38  *
39  */

40 public class DigestFilterTestCase extends TestCase {
41
42     private SAXParserFactory JavaDoc parserFactory;
43
44     /** @see junit.framework.TestCase#setUp() */
45     protected void setUp() {
46         parserFactory = SAXParserFactory.newInstance();
47         parserFactory.setNamespaceAware(true);
48     }
49
50     private boolean compareDigest(byte[] a, byte[] b) {
51         if (a.length != b.length) {
52             return false;
53         }
54         for (int i = 0; i < a.length; i++) {
55             if (a[i] != b[i]) {
56                 return false;
57             }
58         }
59         return true;
60     }
61
62     private String JavaDoc digestToString(byte[] digest) {
63         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(2 * digest.length);
64         for (int i = 0; i < digest.length; i++) {
65             int val = digest[i];
66             int hi = (val >> 4) & 0xF;
67             int lo = val & 0xF;
68             if (hi < 10) {
69                 buffer.append((char) (hi + 0x30));
70             } else {
71                 buffer.append((char) (hi + 0x61 - 10));
72             }
73             if (lo < 10) {
74                 buffer.append((char) (lo + 0x30));
75             } else {
76                 buffer.append((char) (lo + 0x61 - 10));
77             }
78         }
79         return buffer.toString();
80     }
81
82     private byte[] runTest(String JavaDoc input)
83         throws
84             NoSuchAlgorithmException JavaDoc,
85             ParserConfigurationException JavaDoc,
86             SAXException JavaDoc,
87             IOException JavaDoc {
88         XMLReader JavaDoc parser = parserFactory.newSAXParser().getXMLReader();
89         DigestFilter digestFilter = new DigestFilter("MD5");
90         digestFilter.setParent(parser);
91         digestFilter.setFeature("http://xml.org/sax/features/namespaces", true);
92         parser.setContentHandler(digestFilter);
93         InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(input));
94         parser.parse(inputSource);
95         return digestFilter.getDigestValue();
96     }
97
98     public final void testLineFeed()
99         throws
100             NoSuchAlgorithmException JavaDoc,
101             ParserConfigurationException JavaDoc,
102             SAXException JavaDoc,
103             IOException JavaDoc {
104         byte[] lfDigest = runTest("<a>\n</a>");
105         byte[] crlfDigest = runTest("<a>\r\n</a>");
106         assertTrue(
107             "LF: "
108                 + digestToString(lfDigest)
109                 + " CRLF: "
110                 + digestToString(crlfDigest),
111             compareDigest(lfDigest, crlfDigest));
112     }
113
114     public final void testAttributeOrder()
115         throws
116             NoSuchAlgorithmException JavaDoc,
117             ParserConfigurationException JavaDoc,
118             SAXException JavaDoc,
119             IOException JavaDoc {
120         byte[] sortDigest = runTest("<a a1='1' a2='2' a3='3'/>");
121         byte[] permutationDigest = runTest("<a a2='2' a3='3' a1='1'/>");
122         assertTrue(
123             "Sort: "
124                 + digestToString(sortDigest)
125                 + " permuted: "
126                 + digestToString(permutationDigest),
127             compareDigest(sortDigest, permutationDigest));
128         byte[] reverseDigest = runTest("<a a3='3' a2='2' a1='1'/>");
129         assertTrue(
130             "Sort: "
131                 + digestToString(sortDigest)
132                 + " permuted: "
133                 + digestToString(reverseDigest),
134             compareDigest(sortDigest, reverseDigest));
135     }
136
137     public final void testNamespacePrefix()
138         throws
139             NoSuchAlgorithmException JavaDoc,
140             ParserConfigurationException JavaDoc,
141             SAXException JavaDoc,
142             IOException JavaDoc {
143         byte[] prefix1Digest = runTest("<a:a xmlns:a='foo'/>");
144         byte[] prefix2Digest = runTest("<b:a xmlns:b='foo'/>");
145         assertTrue(
146             "prefix1: "
147                 + digestToString(prefix1Digest)
148                 + " prefix2: "
149                 + digestToString(prefix2Digest),
150             compareDigest(prefix1Digest, prefix2Digest));
151     }
152
153 }
154
Popular Tags