1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: TestChannelFileSource.java,v 1.2 2003/02/17 04:17:28 michaelh Exp $ 20 */ 21 22 package test.juju.reattore.io.impl; 23 24 import junit.framework.*; 25 26 import java.io.*; 27 import java.nio.ByteBuffer; 28 29 import juju.reattore.io.impl.ChannelFileSource; 30 import juju.reattore.io.ByteSource; 31 32 public class TestChannelFileSource 33 extends TestCase { 34 35 private static final String FILE = "tests/test/juju/reattore/io/impl/testchannelfilesource.txt"; 36 37 private ChannelFileSource bs; 38 39 private void open(String file) 40 throws IOException { 41 42 bs = new ChannelFileSource(new File(file)); 43 } 44 45 private String getBody() 46 throws IOException { 47 48 byte[] body = new byte[bs.remaining()]; 49 bs.get(body, 0, body.length); 50 51 return new String(body); 52 } 53 54 public void testGet() 55 throws IOException { 56 57 open(FILE); 58 String body = getBody(); 59 60 assertEquals("This is testchannelfilesource.txt", body); 61 bs.dispose(); 62 } 63 64 public void testNegativeGet() { 65 66 boolean threw = false; 67 68 try { 69 open(FILE + "-x"); 70 bs.dispose(); 71 } 72 catch (IOException ex) { 73 threw = true; 74 } 75 76 assertTrue(threw); 77 } 78 79 public void testRewind() 80 throws IOException { 81 82 open(FILE); 83 84 String body = getBody(); 85 assertEquals("This is testchannelfilesource.txt", body); 86 87 String rest = getBody(); 88 assertEquals("", rest); 89 90 bs.rewind(); 91 String body2 = getBody(); 92 assertEquals(body, body2); 93 } 94 95 public void testIsExpired() 96 throws IOException { 97 98 open(FILE); 99 assertEquals(false, bs.isExpired()); 100 } 101 102 public void testBulk() 103 throws IOException { 104 105 open(FILE); 106 ByteBuffer bb = bs.getBulk(); 107 108 assertEquals(33, bb.remaining()); 109 110 byte[] body = new byte[bb.remaining()]; 111 bb.get(body, 0, body.length); 112 113 assertEquals("This is testchannelfilesource.txt", new String(body)); 114 } 115 116 public void testLargeFiles() 117 throws IOException { 118 119 open(FILE); 120 bs.setReadSize(20); 121 122 ByteBuffer bb = bs.getBulk(); 123 124 assertEquals(20, bb.remaining()); 125 assertEquals(13, bs.remaining()); 126 127 byte[] body = new byte[bb.remaining()]; 128 bb.get(body, 0, body.length); 129 130 assertEquals("This is testchannelf", new String(body)); 131 132 bb = bs.getBulk(); 133 134 assertEquals(13, bb.remaining()); 135 assertEquals(0, bs.remaining()); 136 137 byte[] body2 = new byte[bb.remaining()]; 138 bb.get(body2, 0, body2.length); 139 140 assertEquals("ilesource.txt", new String(body2)); 141 } 142 143 public static Test suite() { 144 return new TestSuite(TestChannelFileSource.class); 145 } 146 }

This page was automatically generated by Maven