Source Code Cross Referenced for T_MarkedLimitInputStream.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » unitTests » services » 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 » Database DBMS » db derby 10.2 » org.apache.derbyTesting.unitTests.services 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derbyTesting.unitTests.services.T_MarkedLimitInputStream
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:
022:        package org.apache.derbyTesting.unitTests.services;
023:
024:        import org.apache.derbyTesting.unitTests.harness.T_Generic;
025:        import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027:        import java.io.ByteArrayInputStream;
028:        import java.io.ByteArrayOutputStream;
029:        import java.io.InputStream;
030:        import java.io.OutputStream;
031:        import java.io.DataInputStream;
032:        import java.io.DataOutputStream;
033:        import java.io.IOException;
034:
035:        /**
036:         A simple unit test for a MarkedLimitInputStream.
037:         */
038:        public class T_MarkedLimitInputStream extends T_Generic {
039:
040:            private static final int TEST_SIZE = 10000;
041:            private static final int BLOCK_SIZE = 256;
042:
043:            private static MarkedLimitInputStream setup(byte[] data)
044:                    throws Exception {
045:                // make an InputStream on top of an array
046:                InputStream inputStream = new ByteArrayInputStream(data);
047:
048:                // make an OutputStream on top of an empty array
049:                ByteArrayOutputStream baos = new ByteArrayOutputStream(
050:                        TEST_SIZE + 200);
051:                // make it into a DataOutputStream
052:                DataOutputStream dos = new DataOutputStream(baos);
053:                // fill it with data in the correct (block) format
054:                writeDos(inputStream, dos);
055:
056:                // make a MarkedLimitInputStream
057:                return makeMLIS(baos.toByteArray());
058:
059:            }
060:
061:            private static void writeDos(InputStream x, DataOutputStream out)
062:                    throws Exception {
063:                boolean isLastBlock = false;
064:                byte[] b = new byte[BLOCK_SIZE];
065:
066:                while (isLastBlock == false) {
067:                    int len = x.read(b);
068:                    if (len != BLOCK_SIZE) {
069:                        isLastBlock = true;
070:                        if (len < 0) {
071:                            len = 0;
072:                        }
073:                    }
074:                    out.writeBoolean(isLastBlock);
075:                    out.writeInt(len);
076:                    for (int i = 0; i < len; i++) {
077:                        out.writeByte(b[i]);
078:                    }
079:                }
080:            }
081:
082:            private static MarkedLimitInputStream makeMLIS(byte[] b)
083:                    throws Exception {
084:                // make an InputStream
085:                InputStream inputStream = new ByteArrayInputStream(b);
086:                // make a DataInputStream
087:                DataInputStream dataInputStream = new DataInputStream(
088:                        inputStream);
089:                // make a MarkedLimitInputStream
090:                return new MarkedLimitInputStream(dataInputStream);
091:            }
092:
093:            private static boolean readAndCompare(MarkedLimitInputStream mlis,
094:                    byte[] x) throws Exception {
095:                int b;
096:                int i = 0;
097:                while ((b = mlis.read()) != -1) {
098:                    if (x[i] != (byte) b) {
099:                        System.out
100:                                .println("Stream and array differ at position "
101:                                        + i);
102:                        return false;
103:                    }
104:                    i++;
105:                }
106:                // read to end of stream, check array size
107:                if (i != x.length) {
108:                    System.out.println("array size and stream size differ");
109:                    return false;
110:                }
111:                return true;
112:
113:            }
114:
115:            private static boolean readAndCompareChunks(
116:                    MarkedLimitInputStream mlis, byte[] x) throws Exception {
117:                int chunkSize = 10;
118:                byte[] chunk = new byte[chunkSize];
119:                int c = 0;
120:                int base = 0;
121:                while ((c = mlis.read(chunk)) > 0) {
122:                    for (int offset = 0; offset < c; offset++) {
123:                        if (x[base + offset] != chunk[offset]) {
124:                            System.out
125:                                    .println("Stream and array differ at position "
126:                                            + (base + offset));
127:                            System.out.println("Array : x[" + (base + offset)
128:                                    + "] = " + x[base + offset]);
129:                            System.out.println("Stream : chunk[" + offset
130:                                    + "] = " + chunk[offset]);
131:                            return false;
132:                        }
133:                    }
134:                    base += c;
135:                }
136:
137:                // read to end of stream, check array size
138:                if (base != x.length) {
139:                    System.out.println("array size ( " + x.length
140:                            + " ) and stream size ( " + base + " ) differ");
141:                    return false;
142:                }
143:                return true;
144:
145:            }
146:
147:            private static boolean skipAndCompare(MarkedLimitInputStream mlis,
148:                    byte[] x, long skipTo) throws Exception {
149:                long c = mlis.skip(skipTo);
150:                T_Fail.T_ASSERT(c == skipTo);
151:                byte[] y = new byte[x.length - (int) c];
152:                System.arraycopy(x, (int) skipTo, y, 0, x.length - (int) c);
153:                return readAndCompare(mlis, y);
154:            }
155:
156:            /** Methods required by T_Generic
157:             */
158:            public String getModuleToTestProtocolName() {
159:                return "internalUtils.MarkedLimitInputStream";
160:            }
161:
162:            protected void runTests() throws Exception {
163:                boolean success = true;
164:                // create and initialize array
165:                byte[] data = new byte[TEST_SIZE];
166:                for (int i = 0; i < data.length; i++) {
167:                    data[i] = (byte) (i & 0xFF);
168:                }
169:
170:                MarkedLimitInputStream mlis = setup(data);
171:                // compare MarkedLimitInputStream with original byte array
172:                if (readAndCompare(mlis, data)) {
173:                    PASS("test1");
174:                } else {
175:                    FAIL("test1");
176:                    success = false;
177:                }
178:
179:                MarkedLimitInputStream mlis2 = setup(data);
180:                // compare MarkedLimitInputStream with original byte array
181:                // read in chunks
182:                if (readAndCompareChunks(mlis2, data)) {
183:                    PASS("test2");
184:                } else {
185:                    FAIL("test2");
186:                    success = false;
187:                }
188:
189:                MarkedLimitInputStream mlis3 = setup(data);
190:                // skip and compare MarkedLimitInputStream with original byte array
191:                if (skipAndCompare(mlis3, data, TEST_SIZE / 2)) {
192:                    PASS("test3");
193:                } else {
194:                    FAIL("test3");
195:                    success = false;
196:                }
197:
198:                MarkedLimitInputStream mlis4 = setup(data);
199:                // skip and compare MarkedLimitInputStream with original byte array
200:                if (skipAndCompare(mlis4, data, TEST_SIZE - 1)) {
201:                    PASS("test4");
202:                } else {
203:                    FAIL("test4");
204:                    success = false;
205:                }
206:
207:                if (!success) {
208:                    throw T_Fail.testFail();
209:                }
210:
211:                // create and initialize array with size BLOCK_SIZE
212:                byte[] data2 = new byte[BLOCK_SIZE];
213:                for (int i = 0; i < data.length; i++) {
214:                    data[i] = (byte) (i & 0xFF);
215:                }
216:                MarkedLimitInputStream mlis5 = setup(data2);
217:                // skip and compare MarkedLimitInputStream with original byte array
218:                if (readAndCompare(mlis5, data2)) {
219:                    PASS("test5");
220:                } else {
221:                    FAIL("test5");
222:                    success = false;
223:                }
224:
225:                if (!success) {
226:                    throw T_Fail.testFail();
227:                }
228:
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.