/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: TestChannelFileSource.java,v 1.2 2003/02/17 04:17:28 michaelh Exp $
*/
package test.juju.reattore.io.impl;
import junit.framework.*;
import java.io.*;
import java.nio.ByteBuffer;
import juju.reattore.io.impl.ChannelFileSource;
import juju.reattore.io.ByteSource;
public class TestChannelFileSource
extends TestCase {
private static final String FILE = "tests/test/juju/reattore/io/impl/testchannelfilesource.txt";
private ChannelFileSource bs;
private void open(String file)
throws IOException {
bs = new ChannelFileSource(new File(file));
}
private String getBody()
throws IOException {
byte[] body = new byte[bs.remaining()];
bs.get(body, 0, body.length);
return new String(body);
}
public void testGet()
throws IOException {
open(FILE);
String body = getBody();
assertEquals("This is testchannelfilesource.txt", body);
bs.dispose();
}
public void testNegativeGet() {
boolean threw = false;
try {
open(FILE + "-x");
bs.dispose();
}
catch (IOException ex) {
threw = true;
}
assertTrue(threw);
}
public void testRewind()
throws IOException {
open(FILE);
String body = getBody();
assertEquals("This is testchannelfilesource.txt", body);
String rest = getBody();
assertEquals("", rest);
bs.rewind();
String body2 = getBody();
assertEquals(body, body2);
}
public void testIsExpired()
throws IOException {
open(FILE);
assertEquals(false, bs.isExpired());
}
public void testBulk()
throws IOException {
open(FILE);
ByteBuffer bb = bs.getBulk();
assertEquals(33, bb.remaining());
byte[] body = new byte[bb.remaining()];
bb.get(body, 0, body.length);
assertEquals("This is testchannelfilesource.txt", new String(body));
}
public void testLargeFiles()
throws IOException {
open(FILE);
bs.setReadSize(20);
ByteBuffer bb = bs.getBulk();
assertEquals(20, bb.remaining());
assertEquals(13, bs.remaining());
byte[] body = new byte[bb.remaining()];
bb.get(body, 0, body.length);
assertEquals("This is testchannelf", new String(body));
bb = bs.getBulk();
assertEquals(13, bb.remaining());
assertEquals(0, bs.remaining());
byte[] body2 = new byte[bb.remaining()];
bb.get(body2, 0, body2.length);
assertEquals("ilesource.txt", new String(body2));
}
public static Test suite() {
return new TestSuite(TestChannelFileSource.class);
}
}