KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > DynamicURITest


1 package org.apache.turbine;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 /**
62  * Tests the DynamicURI class.
63  *
64  * @author <a HREF="mailto:jmcnally@apache.org">John McNally</a>
65  * @since 3.0
66  * @version $Id: DynamicURITest.java,v 1.2 2003/11/23 00:42:22 mpoeschl Exp $
67  */

68 public class DynamicURITest
69     extends TestCase
70 {
71     /**
72      * Creates a new instance.
73      */

74     public DynamicURITest(String JavaDoc testName)
75     {
76         super(testName);
77     }
78
79     /**
80      * Return the Test
81      */

82     public static Test suite()
83     {
84         return new TestSuite(DynamicURITest.class);
85     }
86
87     /**
88      * Setup the test.
89      */

90     public void setUp()
91     {
92         // Nothing done here yet.
93
}
94    
95     /**
96      * Tear down the test.
97      */

98     public void tearDown()
99     {
100         // Nothing to do here yet.
101
}
102
103     /**
104      * Previous versions of DynamicURI immediately converted a pathinfo
105      * or query parameter to a byte array for url encoding. The method
106      * has been modified to only invoke the getBytes() method, if a
107      * character requires encoding. This test compares the results of
108      * the new method with the old.
109      */

110     public void testFastEncoding()
111     {
112         char[] allchars = new char[65546];
113         allchars[0] = 's';
114         allchars[1] = 't';
115         allchars[2] = 'a';
116         allchars[3] = 'r';
117         allchars[4] = 't';
118         allchars[5] = ' ';
119         allchars[6] = 's';
120         allchars[7] = 'a';
121         allchars[8] = 'f';
122         allchars[9] = 'e';
123         for (int i=0; i<65536; i++)
124         {
125             allchars[i+10] = (char)i;
126         }
127         final String JavaDoc all = new String JavaDoc(allchars);
128
129         // get reference String
130
StringBuffer JavaDoc out = new StringBuffer JavaDoc();
131         DynamicURI.writeEncoded(all, out);
132         String JavaDoc reference = out.toString();
133
134         // compare to our changes
135
out = new StringBuffer JavaDoc();
136         DynamicURI.writeFastEncoded(all, out);
137         String JavaDoc s = out.toString();
138
139         assertEquals("Test of complete character set failed.", reference, s);
140
141         // colon is a special character
142
String JavaDoc in = ":colon as first char";
143         // get reference String
144
out = new StringBuffer JavaDoc();
145         DynamicURI.writeEncoded(in, out);
146         reference = out.toString();
147         // compare to our changes
148
out = new StringBuffer JavaDoc();
149         DynamicURI.writeFastEncoded(in, out);
150         s = out.toString();
151
152         assertEquals("Special character as first character failed.", reference, s);
153
154         // colon is a special character
155
in = "colon as last char:";
156         // get reference String
157
out = new StringBuffer JavaDoc();
158         DynamicURI.writeEncoded(in, out);
159         reference = out.toString();
160         // compare to our changes
161
out = new StringBuffer JavaDoc();
162         DynamicURI.writeFastEncoded(in, out);
163         s = out.toString();
164
165         assertEquals("Special character as last character failed.", reference, s);
166
167         // colon is a special character
168
in = "n0special_chars";
169         // get reference String
170
out = new StringBuffer JavaDoc();
171         DynamicURI.writeEncoded(in, out);
172         reference = out.toString();
173         // compare to our changes
174
out = new StringBuffer JavaDoc();
175         DynamicURI.writeFastEncoded(in, out);
176         s = out.toString();
177
178         assertEquals("String with no special character failed.", reference, s);
179     }
180 }
181
Popular Tags