001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.io;
027:
028: import junit.framework.TestCase;
029:
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.FileNotFoundException;
033: import java.io.IOException;
034: import java.nio.ByteBuffer;
035: import java.nio.channels.FileChannel;
036: import java.nio.channels.ReadableByteChannel;
037: import java.nio.channels.spi.AbstractInterruptibleChannel;
038:
039: /**
040: * <a href="http://www.junit.org/">JUnit</a>
041: * {@link TestCase testcase} for
042: * Filesystem.
043: *
044: * @todo a lot more tests....
045: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
046: * @version $Id: FilesystemTestCase.java,v 1.4 2004/02/26 16:51:56 lsimons Exp $
047: */
048: public class FilesystemTestCase extends TestCase {
049: public final static int BUFFER_SIZE = 1024;
050:
051: // ----------------------------------------------------------------------
052: // Properties
053: // ----------------------------------------------------------------------
054: protected FilesystemImpl system;
055:
056: // ----------------------------------------------------------------------
057: // Setup
058: // ----------------------------------------------------------------------
059: protected void setUp() throws Exception {
060: super .setUp();
061:
062: final String javaHome = System.getProperty("java.home");
063: final String slash = System.getProperty("file.separator");
064:
065: system = new FilesystemImpl(javaHome + slash + "lib");
066: }
067:
068: protected void tearDown() throws Exception {
069: super .tearDown();
070: }
071:
072: // ----------------------------------------------------------------------
073: // Test: getFile()
074: // ----------------------------------------------------------------------
075: public void testGetFile() throws Exception {
076: final ReadableByteChannel channel = system.getFile("rt.jar");
077:
078: assertNotNull(channel);
079: assertTrue(channel.isOpen());
080:
081: while (true) {
082: final ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
083: final int read = channel.read(buf);
084: if (read < 0)
085: break;
086: }
087: }
088:
089: public void testGetFileDoesNotAcceptNull() throws Exception {
090: boolean notAccepted = false;
091: try {
092: system.getFile(null);
093: } catch (RuntimeException re) {
094: notAccepted = true;
095: } catch (AssertionError e) {
096: notAccepted = true;
097: }
098:
099: assertTrue(notAccepted);
100: }
101:
102: public void testGetFileThrowsFileNotFoundException()
103: throws Exception {
104: boolean notFound = false;
105: try {
106: system.getFile("thisfiledoesnotexist.msg");
107: } catch (FileNotFoundException e) {
108: notFound = true;
109: }
110:
111: assertTrue(notFound);
112: }
113:
114: public void testGetFileDoesNotAcceptDirectories() throws Exception {
115: boolean notAccepted = false;
116: try {
117: system.getFile("");
118: } catch (RuntimeException re) {
119: notAccepted = true;
120: } catch (AssertionError e) {
121: notAccepted = true;
122: }
123:
124: assertTrue(notAccepted);
125: }
126:
127: // ----------------------------------------------------------------------
128: // Test: returnFile()
129: // ----------------------------------------------------------------------
130: public void testReturnFile() throws Exception {
131: final ReadableByteChannel channel = system.getFile("rt.jar");
132: assertNotNull(channel);
133: assertTrue(channel.isOpen());
134: system.returnFile(channel);
135: assertFalse(channel.isOpen());
136: }
137:
138: public void testReturnFileAcceptsNull() throws Exception {
139: system.returnFile(null);
140: }
141:
142: public void testReturnFileAcceptsNonFileChannel() throws Exception {
143: system.returnFile(new NoopReadableByteChannel());
144: }
145:
146: public void testReturnFileDoesNotModifyChannelsItDidNotCreate()
147: throws Exception {
148: final String javaHome = System.getProperty("java.home");
149: final String slash = System.getProperty("file.separator");
150: final String file = javaHome + slash + "lib" + slash + "rt.jar";
151:
152: final File f = new File(file);
153: final FileInputStream fs = new FileInputStream(f);
154: final FileChannel fc = fs.getChannel();
155:
156: system.returnFile(fc);
157: assertTrue(fc.isOpen());
158: fs.close();
159: }
160:
161: // ----------------------------------------------------------------------
162: // Inner Class: NoopReadableByteChannel
163: // ----------------------------------------------------------------------
164: public static class NoopReadableByteChannel extends
165: AbstractInterruptibleChannel implements ReadableByteChannel {
166: protected void implCloseChannel() throws IOException {
167: }
168:
169: public int read(final ByteBuffer dst) throws IOException {
170: return 0;
171: }
172: }
173: }
|