Source Code Cross Referenced for ConcatTests.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:         * Contact Streams Regression test.
003:         * Copyright (C) 2004 Stephen Ostermiller
004:         * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005:         *
006:         * This program is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU General Public License as published by
008:         * the Free Software Foundation; either version 2 of the License, or
009:         * (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * See COPYING.TXT for details.
017:         */
018:        package com.Ostermiller.util;
019:
020:        import java.io.*;
021:
022:        /**
023:         * Regression test for Concatenation Streams.
024:         * More information about this class is available from <a target="_top" href=
025:         * "http://ostermiller.org/utils/Conact.html">ostermiller.org</a>.
026:         *
027:         * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
028:         * @since ostermillerutils 1.04.00
029:         */
030:        class ConcatTests {
031:
032:            /**
033:             * Main method for tests
034:             * @param args command line arguments (ignored)
035:             */
036:            public static void main(String[] args) {
037:                try {
038:                    ConcatReader cr = new ConcatReader(new Reader[] {
039:                            new StringReader("1"), new StringReader("two"),
040:                            new StringReader(""), new StringReader("4"),
041:                            new StringReader("five"), new StringReader("six"),
042:                            new StringReader("seven"), });
043:                    if (!cr.ready())
044:                        throw new Exception("Not Ready");
045:                    read(cr, '1');
046:                    read(cr, 't');
047:                    read(cr, 'w');
048:                    read(cr, 'o');
049:                    if (!cr.ready())
050:                        throw new Exception("Not Ready");
051:                    read(cr, '4');
052:                    read(cr, "fiv");
053:                    skip(cr, 2);
054:                    read(cr, "i");
055:                    read(cr, "xseven");
056:                    if (cr.read() != -1)
057:                        throw new Exception("Read did not terminate");
058:                    if (cr.read() != -1)
059:                        throw new Exception("Didn't stay closed");
060:                    cr.close();
061:
062:                    final ConcatReader cr1 = new ConcatReader();
063:                    if (cr1.ready())
064:                        throw new Exception("Ready");
065:                    cr1.addReader(new StringReader("one"));
066:                    read(cr1, 'o');
067:                    cr1.addReader(new StringReader("two"));
068:                    read(cr1, "netwo");
069:                    new Thread() {
070:                        @Override
071:                        public void run() {
072:                            try {
073:                                Thread.sleep(1000);
074:                            } catch (InterruptedException ix) {
075:                                throw new RuntimeException(ix);
076:                            }
077:                            cr1.addReader(new StringReader("three"));
078:                        }
079:                    }.start();
080:                    read(cr1, "three");
081:                    cr1.lastReaderAdded();
082:                    if (cr1.read() != -1)
083:                        throw new Exception("Read did not terminate");
084:
085:                    ConcatInputStream cis = new ConcatInputStream(
086:                            new InputStream[] {
087:                                    new ByteArrayInputStream(new byte[] { '1' }),
088:                                    new ByteArrayInputStream(new byte[] { 't',
089:                                            'w', 'o' }),
090:                                    new ByteArrayInputStream(new byte[] {}),
091:                                    new ByteArrayInputStream(new byte[] { '4' }),
092:                                    new ByteArrayInputStream(new byte[] { 'f',
093:                                            'i', 'v', 'e' }),
094:                                    new ByteArrayInputStream(new byte[] { 's',
095:                                            'i', 'x' }),
096:                                    new ByteArrayInputStream(new byte[] { 's',
097:                                            'e', 'v', 'e', 'n' }), });
098:                    if (cis.available() <= 0)
099:                        throw new Exception("Not Ready");
100:                    read(cis, '1');
101:                    read(cis, 't');
102:                    read(cis, 'w');
103:                    read(cis, 'o');
104:                    read(cis, '4');
105:                    read(cis, "fivesi");
106:                    if (cis.available() <= 0)
107:                        throw new Exception("Not Ready");
108:                    read(cis, "xseven");
109:                    if (cis.read() != -1)
110:                        throw new Exception("Read did not terminate");
111:                    if (cis.read() != -1)
112:                        throw new Exception("Didn't stay closed");
113:
114:                    final ConcatInputStream cis1 = new ConcatInputStream();
115:                    if (cis.available() != 0)
116:                        throw new Exception("Ready");
117:                    cis1.addInputStream(new ByteArrayInputStream("one"
118:                            .getBytes("ASCII")));
119:                    read(cis1, 'o');
120:                    cis1.addInputStream(new ByteArrayInputStream("two"
121:                            .getBytes("ASCII")));
122:                    read(cis1, "netwo");
123:                    new Thread() {
124:                        @Override
125:                        public void run() {
126:                            try {
127:                                Thread.sleep(1000);
128:                            } catch (InterruptedException ix) {
129:                                throw new RuntimeException(ix);
130:                            }
131:                            try {
132:                                cis1.addInputStream(new ByteArrayInputStream(
133:                                        "three".getBytes("ASCII")));
134:                            } catch (Exception x) {
135:                                System.err.println(x.getMessage());
136:                                x.printStackTrace();
137:                                System.exit(1);
138:                            }
139:                        }
140:                    }.start();
141:                    read(cis1, "three");
142:                    cis1.lastInputStreamAdded();
143:                    if (cis1.read() != -1)
144:                        throw new Exception("Read did not terminate");
145:
146:                } catch (Exception x) {
147:                    System.err.println(x.getMessage());
148:                    x.printStackTrace();
149:                    System.exit(1);
150:                }
151:                System.exit(0);
152:            }
153:
154:            private static void skip(Reader in, int n) throws Exception {
155:                int s = 0;
156:                while (s < n) {
157:                    s += in.skip(n - s);
158:                }
159:            }
160:
161:            private static void read(Reader in, char expected) throws Exception {
162:                int c = in.read();
163:                if (c != expected)
164:                    throw new Exception("Expected to read " + expected
165:                            + " but read " + (char) c);
166:            }
167:
168:            private static void read(InputStream in, char expected)
169:                    throws Exception {
170:                int c = in.read();
171:                if (c != expected)
172:                    throw new Exception("Expected to read " + expected
173:                            + " but read " + (char) c);
174:            }
175:
176:            private static void read(Reader in, String expected)
177:                    throws Exception {
178:                int totalRead = 0;
179:                while (totalRead < expected.length()) {
180:                    char[] buffer = new char[expected.length() - totalRead];
181:                    int read = in.read(buffer);
182:                    if (read == -1)
183:                        throw new Exception("Read terminated early");
184:                    if (!expected.substring(totalRead, totalRead + read)
185:                            .equals(new String(buffer, 0, read))) {
186:                        throw new Exception("Expected to read "
187:                                + expected.substring(totalRead, totalRead
188:                                        + read) + " but read "
189:                                + new String(buffer, 0, read));
190:                    }
191:                    totalRead += read;
192:                }
193:            }
194:
195:            private static void read(InputStream in, String expected)
196:                    throws Exception {
197:                int totalRead = 0;
198:                while (totalRead < expected.length()) {
199:                    byte[] buffer = new byte[expected.length() - totalRead];
200:                    int read = in.read(buffer);
201:                    if (read == -1)
202:                        throw new Exception("Read terminated early");
203:                    if (!expected.substring(totalRead, totalRead + read)
204:                            .equals(new String(buffer, 0, read, "ASCII"))) {
205:                        throw new Exception("Expected to read "
206:                                + expected.substring(totalRead, totalRead
207:                                        + read) + " but read "
208:                                + new String(buffer, 0, read, "ASCII"));
209:                    }
210:                    totalRead += read;
211:                }
212:            }
213:
214:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.