pero 2005/04/05 11:05:52 Modified: modules/cluster/src/share/org/apache/catalina/cluster/io XByteBuffer.java Added: modules/cluster/test/src/share/org/apache/catalina/cluster/io XByteBufferTest.java Log: Extract test code and move to real unit testcase! Revision Changes Path 1.12 +1 -47 jakarta-tomcat-catalina/modules/cluster/src/share/org/apache/catalina/cluster/io/XByteBuffer.java Index: XByteBuffer.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/modules/cluster/src/share/org/apache/catalina/cluster/io/XByteBuffer.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- XByteBuffer.java 25 Mar 2005 22:21:26 -0000 1.11 +++ XByteBuffer.java 5 Apr 2005 18:05:52 -0000 1.12 @@ -393,51 +393,5 @@ } - // FIXME: extract this to test code! - public static void main(String[] args) throws Exception { - log.info("Before="+Integer.MAX_VALUE); - byte[] d = toBytes(Integer.MAX_VALUE); - log.info("After="+toInt(d,0)); - - - log.info("Before="+Long.MAX_VALUE); - d = toBytes(Long.MAX_VALUE); - log.info("After="+toLong(d,0)); - - log.info("Before=" + 4564564); - d = toBytes((long)4564564); - log.info("After=" + toLong(d, 0)); - - byte[] d1 = createDataPackage(new byte[] {1},true); - byte[] d2 = createDataPackage(new byte[] {2},true); - byte[] d3 = createDataPackage(new byte[] {3},true); - byte[] test = new byte[d1.length+d2.length+d3.length+5]; - System.arraycopy(d1,0,test,0,d1.length); - System.arraycopy(d2,0,test,d1.length,d2.length); - System.arraycopy(d3,0,test,d2.length+d1.length,d3.length); - printBuf(d1); - printBuf(d2); - printBuf(d3); - printBuf(test); - XByteBuffer b = new XByteBuffer(); - b.append(test,0,test.length); - int s = b.countPackages(); - log.info("Nr of packages="+s); - while ( s > 0 ) { - d = b.extractPackage(true); - log.info("Package d1="); - printBuf(d); - s--; - }//while - - } - - public static void printBuf(byte[] b) { - StringBuffer buf = new StringBuffer(); - for ( int i=0; i<b.length; i++ ) { - buf.append(b[i] + " "); - } - log.info(buf); - } } 1.1 jakarta-tomcat-catalina/modules/cluster/test/src/share/org/apache/catalina/cluster/io/XByteBufferTest.java Index: XByteBufferTest.java =================================================================== /* * Copyright 1999,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package org.apache.catalina.cluster.io; import java.io.IOException; import junit.framework.TestCase; /** * @author Peter Rossbach * * @version $Revision: 1.1 $ $Date: 2005/04/05 18:05:52 $ */ public class XByteBufferTest extends TestCase { public void testBytes() { XByteBuffer xbuf = new XByteBuffer() ; assertEquals(0,xbuf.bufSize); assertEquals(1024,xbuf.buf.length); assertEquals(0,xbuf.getBytes().length); assertNotSame(xbuf.buf,xbuf.getBytes()); } /** * Test append to message buffer * Test that only correct message header are appended */ public void testAppend() { XByteBuffer xbuf = new XByteBuffer(); byte[] bs = new byte[] { 1, 2, 3 }; boolean status = xbuf.append(bs, 0, 3); assertTrue(status); assertEquals(3, xbuf.bufSize); byte buf[] = xbuf.getBytes(); assertEquals(3, buf.length); status = xbuf.append(bs, 0, 3); assertTrue(status); status = xbuf.append(bs, 0, 3); // not a correct message !! assertFalse(status); // buffer is cleared assertEquals(0,xbuf.bufSize); status = xbuf.append(XByteBuffer.START_DATA,0, XByteBuffer.START_DATA.length); assertTrue(status); } /** * Test create a compress multi message data package and extract it * @throws IOException */ public void testSendCompressedMessage() throws IOException { assertSendMessage(true); } /** * Test create a uncompressed multi message data package and extract it * @throws IOException */ public void testSendUncompressedMessage() throws IOException { assertSendMessage(false); } /** * @throws IOException */ private void assertSendMessage(boolean compress) throws IOException { byte[] test = createMessage(compress); XByteBuffer b = new XByteBuffer(compress); b.append(test, 0, test.length); int s = b.countPackages(); byte[] d ; assertEquals(3, s); for (byte i = 1; i < 4; i++) { d = b.extractPackage(true); assertEquals(i, d[0]); } } /** * @return * @throws IOException */ private byte[] createMessage(boolean compress) throws IOException { byte[] d1 = XByteBuffer.createDataPackage(new byte[] { 1 }, compress); byte[] d2 = XByteBuffer.createDataPackage(new byte[] { 2 }, compress); byte[] d3 = XByteBuffer.createDataPackage(new byte[] { 3 }, compress); byte[] test = new byte[d1.length + d2.length + d3.length + 5]; System.arraycopy(d1, 0, test, 0, d1.length); System.arraycopy(d2, 0, test, d1.length, d2.length); System.arraycopy(d3, 0, test, d2.length + d1.length, d3.length); return test; } /** * Test the type convertes to and from byte array */ public void testTypeconverter() { byte[] d = XByteBuffer.toBytes(Integer.MAX_VALUE); assertEquals(4, d.length); assertEquals(Integer.MAX_VALUE, XByteBuffer.toInt(d, 0)); d = XByteBuffer.toBytes(Long.MAX_VALUE); assertEquals(8, d.length); assertEquals(Long.MAX_VALUE, XByteBuffer.toLong(d, 0)); d = XByteBuffer.toBytes((long) 4564564); assertEquals(4564564, XByteBuffer.toLong(d, 0)); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]