Author: wavey
Date: 2008-04-12 10:35:19 +0000 (Sat, 12 Apr 2008)
New Revision: 19216

Added:
   trunk/freenet/test/freenet/support/BufferTest.java
Log:
added unit test

Added: trunk/freenet/test/freenet/support/BufferTest.java
===================================================================
--- trunk/freenet/test/freenet/support/BufferTest.java                          
(rev 0)
+++ trunk/freenet/test/freenet/support/BufferTest.java  2008-04-12 10:35:19 UTC 
(rev 19216)
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+package freenet.support;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link freenet.support.Buffer} class.
+ * 
+ * @author stuart martin <wavey at freenetproject.org>
+ */
+public class BufferTest extends TestCase {
+
+       private static final String DATA_STRING_1 = 
"asldkjaskjdsakdhasdhaskjdhaskjhbkasbhdjkasbduiwbxgdoudgboewuydxbybuewyxbuewyuwe"
 + 
+               
"dasdkljasndijwnodhnqweoidhnaouidhbnwoduihwnxodiuhnwuioxdhnwqiouhnxwqoiushdnxwqoiudhxnwqoiudhxni";
+
+       public void testByteArrayBuffer() {
+               
+               
+               byte[] data = DATA_STRING_1.getBytes();
+               
+               Buffer buffer = new Buffer(data);
+               
+               assertEquals(data, buffer.getData());
+               assertEquals(data.length, buffer.getLength());
+               
+               for(int i = 0; i < buffer.getLength(); i++) {
+                       assertEquals(data[i], buffer.byteAt(i));
+               }
+               
+               try{
+                       buffer.byteAt(data.length + 1); // expect exception
+                       fail();
+               }
+               catch(ArrayIndexOutOfBoundsException e) {
+                       // expect this
+               }
+       }
+
+       public void testLongBufferToString() {
+               
+               Buffer buffer = new Buffer(DATA_STRING_1.getBytes());
+               String longString = buffer.toString();
+               assertEquals("Buffer {" + buffer.getLength() + "}", longString);
+       }
+
+       public void testShortBufferToString() {
+               String shortString = "feep";
+               Buffer shortBuffer = new Buffer(shortString.getBytes());
+               
+               String outString = shortBuffer.toString();
+               assertEquals(outString, "{4:102 101 101 112 "); // FIXME: final 
brace?
+       }
+       
+       public void testEquals() {
+               
+               Buffer b1 = new Buffer("Buffer1".getBytes());
+               Buffer b2 = new Buffer("Buffer2".getBytes());
+               Buffer b3 = new Buffer("Buffer1".getBytes());
+               
+               assertFalse(b1.equals(b2));
+               assertTrue(b1.equals(b3));
+               assertFalse(b2.equals(b3));
+               assertTrue(b1.equals(b1));
+               assertTrue(b2.equals(b2));
+               assertTrue(b3.equals(b1));                              
+       }
+       
+       public void testHashcode() {
+               
+               Buffer b1 = new Buffer("Buffer1".getBytes());
+               Buffer b2 = new Buffer("Buffer2".getBytes());
+               Buffer b3 = new Buffer("Buffer1".getBytes());
+               
+               Map hashMap = new HashMap();
+               
+               hashMap.put(b1, b1); 
+               hashMap.put(b2, b2);
+               hashMap.put(b3, b3); // should clobber b1 due to content
+
+               // see if b3 survived
+               Object o = hashMap.get(b3);
+               assertFalse(o == b1);
+               assertTrue(o == b3);
+               
+               // see if b1 survived
+               o = hashMap.get(b1);
+               assertFalse(o == b1);           
+               assertTrue(o == b3);
+       }
+       
+       public void testCopy() {
+               Buffer b = new Buffer(DATA_STRING_1.getBytes());
+               
+               byte[] newBuf = new byte[b.getLength()];
+               b.copyTo(newBuf, 0);
+       }
+}


Reply via email to