Source Code Cross Referenced for Base64Tests.java in  » Template-Engine » ostermillerutils » com » Ostermiller » util » 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 » Template Engine » ostermillerutils » com.Ostermiller.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2004-2007 Stephen Ostermiller
003:         * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004:         *
005:         * This program is free software; you can redistribute it and/or modify
006:         * it under the terms of the GNU General Public License as published by
007:         * the Free Software Foundation; either version 2 of the License, or
008:         * (at your option) any later version.
009:         *
010:         * This program is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013:         * GNU General Public License for more details.
014:         *
015:         * See COPYING.TXT for details.
016:         */
017:        package com.Ostermiller.util;
018:
019:        import java.util.*;
020:
021:        /**
022:         * Test cases for Base64
023:         */
024:        class Base64Tests {
025:
026:            private static class TestCase {
027:                private String encoded;
028:                private byte[] decoded;
029:
030:                /**
031:                 * @param encoded
032:                 * @param decoded
033:                 */
034:                public TestCase(String encoded, byte[] decoded) {
035:                    this .encoded = encoded;
036:                    this .decoded = decoded;
037:                }
038:
039:                private void test() throws Exception {
040:                    String enc = Base64.encodeToString(decoded);
041:                    if (!encoded.equals(enc)) {
042:                        throw new Exception("Decoding problem, expected '"
043:                                + encoded + "' got '" + enc + "'.");
044:                    }
045:                    byte[] b = Base64.decodeToBytes(encoded);
046:                    if (!byteArraysEqual(b, decoded)) {
047:                        throw new Exception("Encoding problem, started with '"
048:                                + encoded + "'.");
049:                    }
050:                }
051:            }
052:
053:            private static boolean byteArraysEqual(byte[] b1, byte[] b2) {
054:                if (b1.length != b2.length)
055:                    return false;
056:                for (int i = 0; i < b1.length; i++) {
057:                    if (b1[i] != b2[i])
058:                        return false;
059:                }
060:                return true;
061:            }
062:
063:            private static final TestCase[] testCases = new TestCase[] {
064:                    new TestCase("", new byte[] {}),
065:                    new TestCase("aA==", new byte[] { 'h' }),
066:                    new TestCase("dGU=", new byte[] { 't', 'e' }),
067:                    new TestCase("Y29i", new byte[] { 'c', 'o', 'b' }), };
068:
069:            /**
070:             * Main method for tests
071:             * @param args command line arguments (ignored)
072:             */
073:            public static void main(String[] args) {
074:                try {
075:                    for (int i = 0; i < testCases.length; i++) {
076:                        testCases[i].test();
077:                    }
078:                    for (int i = 0; i < 1024; i++) {
079:                        byte[] before = randBytes();
080:                        byte[] after = Base64.decodeToBytes(Base64
081:                                .encodeToString(before));
082:                        if (!byteArraysEqual(before, after)) {
083:                            throw new Exception(
084:                                    "Could not decode and then re-encode:\n before: "
085:                                            + bytesToString(before)
086:                                            + "\n after:  "
087:                                            + bytesToString(after));
088:                        }
089:                    }
090:
091:                } catch (Exception x) {
092:                    x.printStackTrace(System.err);
093:                    System.exit(1);
094:                }
095:                System.exit(0);
096:            }
097:
098:            private static String bytesToString(byte[] b) {
099:                StringBuffer sb = new StringBuffer();
100:                for (int i = 0; i < b.length; i++) {
101:                    sb.append("'").append(b[i]).append("',");
102:                }
103:                return sb.toString();
104:            }
105:
106:            private static byte[] randBytes() {
107:                Random rand = new Random();
108:                byte[] bytes = new byte[rand.nextInt(128) * 3];
109:                for (int i = 0; i < bytes.length; i++) {
110:                    bytes[i] = (byte) (rand.nextInt() & 0xff);
111:                }
112:                return bytes;
113:            }
114:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.