Source Code Cross Referenced for URLCodecTest.java in  » Library » Apache-common-codec » org » apache » commons » codec » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common codec » org.apache.commons.codec.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.codec.net;
018:
019:        import junit.framework.TestCase;
020:        import org.apache.commons.codec.DecoderException;
021:        import org.apache.commons.codec.EncoderException;
022:
023:        /**
024:         * URL codec test cases
025:         * 
026:         * @author Apache Software Foundation
027:         */
028:        public class URLCodecTest extends TestCase {
029:
030:            static final int SWISS_GERMAN_STUFF_UNICODE[] = { 0x47, 0x72, 0xFC,
031:                    0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
032:
033:            static final int RUSSIAN_STUFF_UNICODE[] = { 0x412, 0x441, 0x435,
034:                    0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
035:
036:            public URLCodecTest(String name) {
037:                super (name);
038:            }
039:
040:            private void validateState(URLCodec urlCodec) {
041:                assertEquals(urlCodec.getEncoding(), urlCodec
042:                        .getDefaultCharset());
043:            }
044:
045:            private String constructString(int[] unicodeChars) {
046:                StringBuffer buffer = new StringBuffer();
047:                if (unicodeChars != null) {
048:                    for (int i = 0; i < unicodeChars.length; i++) {
049:                        buffer.append((char) unicodeChars[i]);
050:                    }
051:                }
052:                return buffer.toString();
053:            }
054:
055:            public void testUTF8RoundTrip() throws Exception {
056:
057:                String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
058:                String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
059:
060:                URLCodec urlCodec = new URLCodec();
061:                this .validateState(urlCodec);
062:
063:                assertEquals(
064:                        "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
065:                        urlCodec.encode(ru_msg, "UTF-8"));
066:                assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(
067:                        ch_msg, "UTF-8"));
068:
069:                assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg,
070:                        "UTF-8"), "UTF-8"));
071:                assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg,
072:                        "UTF-8"), "UTF-8"));
073:                this .validateState(urlCodec);
074:            }
075:
076:            public void testBasicEncodeDecode() throws Exception {
077:                URLCodec urlCodec = new URLCodec();
078:                String plain = "Hello there!";
079:                String encoded = urlCodec.encode(plain);
080:                assertEquals("Basic URL encoding test", "Hello+there%21",
081:                        encoded);
082:                assertEquals("Basic URL decoding test", plain, urlCodec
083:                        .decode(encoded));
084:                this .validateState(urlCodec);
085:            }
086:
087:            public void testSafeCharEncodeDecode() throws Exception {
088:                URLCodec urlCodec = new URLCodec();
089:                String plain = "abc123_-.*";
090:                String encoded = urlCodec.encode(plain);
091:                assertEquals("Safe chars URL encoding test", plain, encoded);
092:                assertEquals("Safe chars URL decoding test", plain, urlCodec
093:                        .decode(encoded));
094:                this .validateState(urlCodec);
095:            }
096:
097:            public void testUnsafeEncodeDecode() throws Exception {
098:                URLCodec urlCodec = new URLCodec();
099:                String plain = "~!@#$%^&()+{}\"\\;:`,/[]";
100:                String encoded = urlCodec.encode(plain);
101:                assertEquals(
102:                        "Unsafe chars URL encoding test",
103:                        "%7E%21%40%23%24%25%5E%26%28%29%2B%7B%7D%22%5C%3B%3A%60%2C%2F%5B%5D",
104:                        encoded);
105:                assertEquals("Unsafe chars URL decoding test", plain, urlCodec
106:                        .decode(encoded));
107:                this .validateState(urlCodec);
108:            }
109:
110:            public void testEncodeDecodeNull() throws Exception {
111:                URLCodec urlCodec = new URLCodec();
112:                assertNull("Null string URL encoding test", urlCodec
113:                        .encode((String) null));
114:                assertNull("Null string URL decoding test", urlCodec
115:                        .decode((String) null));
116:                this .validateState(urlCodec);
117:            }
118:
119:            public void testDecodeInvalid() throws Exception {
120:                URLCodec urlCodec = new URLCodec();
121:                try {
122:                    urlCodec.decode("%");
123:                    fail("DecoderException should have been thrown");
124:                } catch (DecoderException e) {
125:                    // Expected. Move on
126:                }
127:                try {
128:                    urlCodec.decode("%A");
129:                    fail("DecoderException should have been thrown");
130:                } catch (DecoderException e) {
131:                    // Expected. Move on
132:                }
133:                try {
134:                    urlCodec.decode("%WW");
135:                    fail("DecoderException should have been thrown");
136:                } catch (DecoderException e) {
137:                    // Expected. Move on
138:                }
139:                this .validateState(urlCodec);
140:            }
141:
142:            public void testEncodeNull() throws Exception {
143:                URLCodec urlCodec = new URLCodec();
144:                byte[] plain = null;
145:                byte[] encoded = urlCodec.encode(plain);
146:                assertEquals("Encoding a null string should return null", null,
147:                        encoded);
148:                this .validateState(urlCodec);
149:            }
150:
151:            public void testEncodeUrlWithNullBitSet() throws Exception {
152:                URLCodec urlCodec = new URLCodec();
153:                String plain = "Hello there!";
154:                String encoded = new String(URLCodec.encodeUrl(null, plain
155:                        .getBytes()));
156:                assertEquals("Basic URL encoding test", "Hello+there%21",
157:                        encoded);
158:                assertEquals("Basic URL decoding test", plain, urlCodec
159:                        .decode(encoded));
160:                this .validateState(urlCodec);
161:            }
162:
163:            public void testDecodeWithNullArray() throws Exception {
164:                byte[] plain = null;
165:                byte[] result = URLCodec.decodeUrl(plain);
166:                assertEquals("Result should be null", null, result);
167:            }
168:
169:            public void testEncodeStringWithNull() throws Exception {
170:                URLCodec urlCodec = new URLCodec();
171:                String test = null;
172:                String result = urlCodec.encode(test, "charset");
173:                assertEquals("Result should be null", null, result);
174:            }
175:
176:            public void testDecodeStringWithNull() throws Exception {
177:                URLCodec urlCodec = new URLCodec();
178:                String test = null;
179:                String result = urlCodec.decode(test, "charset");
180:                assertEquals("Result should be null", null, result);
181:            }
182:
183:            public void testEncodeObjects() throws Exception {
184:                URLCodec urlCodec = new URLCodec();
185:                String plain = "Hello there!";
186:                String encoded = (String) urlCodec.encode((Object) plain);
187:                assertEquals("Basic URL encoding test", "Hello+there%21",
188:                        encoded);
189:
190:                byte[] plainBA = plain.getBytes();
191:                byte[] encodedBA = (byte[]) urlCodec.encode((Object) plainBA);
192:                encoded = new String(encodedBA);
193:                assertEquals("Basic URL encoding test", "Hello+there%21",
194:                        encoded);
195:
196:                Object result = urlCodec.encode((Object) null);
197:                assertEquals("Encoding a null Object should return null", null,
198:                        result);
199:
200:                try {
201:                    Object dObj = new Double(3.0);
202:                    urlCodec.encode(dObj);
203:                    fail("Trying to url encode a Double object should cause an exception.");
204:                } catch (EncoderException ee) {
205:                    // Exception expected, test segment passes.
206:                }
207:                this .validateState(urlCodec);
208:            }
209:
210:            public void testInvalidEncoding() {
211:                URLCodec urlCodec = new URLCodec("NONSENSE");
212:                String plain = "Hello there!";
213:                try {
214:                    urlCodec.encode(plain);
215:                    fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
216:                } catch (EncoderException ee) {
217:                    // Exception expected, test segment passes.
218:                }
219:                try {
220:                    urlCodec.decode(plain);
221:                    fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
222:                } catch (DecoderException ee) {
223:                    // Exception expected, test segment passes.
224:                }
225:                this .validateState(urlCodec);
226:            }
227:
228:            public void testDecodeObjects() throws Exception {
229:                URLCodec urlCodec = new URLCodec();
230:                String plain = "Hello+there%21";
231:                String decoded = (String) urlCodec.decode((Object) plain);
232:                assertEquals("Basic URL decoding test", "Hello there!", decoded);
233:
234:                byte[] plainBA = plain.getBytes();
235:                byte[] decodedBA = (byte[]) urlCodec.decode((Object) plainBA);
236:                decoded = new String(decodedBA);
237:                assertEquals("Basic URL decoding test", "Hello there!", decoded);
238:
239:                Object result = urlCodec.decode((Object) null);
240:                assertEquals("Decoding a null Object should return null", null,
241:                        result);
242:
243:                try {
244:                    Object dObj = new Double(3.0);
245:                    urlCodec.decode(dObj);
246:                    fail("Trying to url encode a Double object should cause an exception.");
247:                } catch (DecoderException ee) {
248:                    // Exception expected, test segment passes.
249:                }
250:                this .validateState(urlCodec);
251:            }
252:
253:            public void testDefaultEncoding() throws Exception {
254:                String plain = "Hello there!";
255:                URLCodec urlCodec = new URLCodec("UnicodeBig");
256:                urlCodec.encode(plain); // To work around a weird quirk in Java 1.2.2
257:                String encoded1 = urlCodec.encode(plain, "UnicodeBig");
258:                String encoded2 = urlCodec.encode(plain);
259:                assertEquals(encoded1, encoded2);
260:                this.validateState(urlCodec);
261:            }
262:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.