KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > TestCmsFileUtil


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsFileUtil.java,v $
3  * Date : $Date: 2006/07/19 14:53:58 $
4  * Version: $Revision: 1.1 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.io.InputStream JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 import junit.framework.TestCase;
40
41 /**
42  * @author Jason Trump
43  *
44  * @version $Revision: 1.1 $
45  *
46  * @since 6.2.2
47  */

48 public class TestCmsFileUtil extends TestCase {
49
50     /**
51      * An input stream that provides 24 bytes of data in two chunks, the first of 16 bytes,
52      * the second of 8 bytes.<p>
53      *
54      * This simulates the behavior of calls to <code>available()</code> and <code>read()</code> on
55      * buffered file or zip input streams, where the size of the underlying data slightly exceeds the size
56      * of the stream's internal buffers.<p>
57      */

58     private static class TestInputStream extends InputStream JavaDoc {
59
60         private boolean m_closed;
61         private ByteArrayInputStream JavaDoc m_firstChunk;
62         private ByteArrayInputStream JavaDoc m_secondChunk;
63
64         /**
65          * Input stream implementation that exploits an error with {@link CmsFileUtil#readFully(InputStream)}.<p>
66          */

67         TestInputStream() {
68
69             byte[] first = new byte[16];
70             byte[] second = new byte[8];
71
72             Arrays.fill(first, (byte)1);
73             Arrays.fill(second, (byte)2);
74
75             this.m_firstChunk = new ByteArrayInputStream JavaDoc(first);
76             this.m_secondChunk = new ByteArrayInputStream JavaDoc(second);
77         }
78
79         /**
80          * @see java.io.InputStream#available()
81          */

82         public int available() {
83
84             if (m_closed) {
85                 return 0;
86             }
87
88             int first = m_firstChunk.available();
89             return first > 0 ? first : m_secondChunk.available();
90         }
91
92         /**
93          * @see java.io.InputStream#close()
94          */

95         public void close() throws IOException JavaDoc {
96
97             //mark that close was called for testing purposes.
98
m_closed = true;
99             super.close();
100         }
101
102         /**
103          * Returns <code>true</code> if this stream was closed.<p>
104          *
105          * @return <code>true</code> if this stream was closed
106          */

107         public boolean isClosed() {
108
109             return m_closed;
110         }
111
112         /**
113          * @see java.io.InputStream#read()
114          */

115         public int read() throws IOException JavaDoc {
116
117             if (m_closed) {
118                 throw new IOException JavaDoc("Stream was closed");
119             }
120
121             return m_firstChunk.available() > 0 ? m_firstChunk.read() : m_secondChunk.read();
122         }
123
124         /**
125          * @see java.io.InputStream#read(byte[], int, int)
126          */

127         public int read(byte[] b, int off, int len) throws IOException JavaDoc {
128
129             if (m_closed) {
130                 throw new IOException JavaDoc("Stream was closed");
131             }
132
133             int first = m_firstChunk.available();
134             if (first > 0) {
135                 return m_firstChunk.read(b, off, len);
136             } else {
137                 return m_secondChunk.read(b, off, len);
138             }
139         }
140     }
141
142     /**
143      * Test the behavior of {@link CmsFileUtil#readFully()}
144      * when the read takes more than one iteration to complete.<p>
145      *
146      * @throws IOException in case the test fails
147      */

148     public void testMultiPassReadFully() throws IOException JavaDoc {
149
150         // this test is written especially to exploit an array bounds bug (bug 1131)
151
// if the first call to available() returns
152
// a value greater than the second call to available(), an IndexOutOfBoundsException is thrown.
153
TestInputStream is = new TestInputStream();
154         byte[] data = CmsFileUtil.readFully(is);
155         assertNotNull("new byte array returned by readFully", data);
156
157         // test that all the data from both chunks of the input stream was read correctly.
158
assertEquals("all data returned", 24, data.length);
159         for (int i = 0; i < 16; ++i) {
160             assertEquals("first chunk data[" + i + "] has correct value", 1, data[i]);
161         }
162         for (int i = 16; i < 24; ++i) {
163             assertEquals("second chunk data[" + i + "] has correct value", 2, data[i]);
164         }
165
166         assertTrue("input stream was closed", is.isClosed());
167     }
168 }
Popular Tags