incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From monte...@apache.org
Subject svn commit: r830201 - in /incubator/kato/trunk/org.apache.kato/kato.cjvmti/src: main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java
Date Tue, 27 Oct 2009 14:28:45 GMT
Author: monteith
Date: Tue Oct 27 14:28:45 2009
New Revision: 830201

URL: http://svn.apache.org/viewvc?rev=830201&view=rev
Log:
Improve correctness of CachedRandomAccessFile and add more tests.

Modified:
    incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java
    incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java

Modified: incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java?rev=830201&r1=830200&r2=830201&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java
(original)
+++ incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/util/CachedRandomAccessFile.java
Tue Oct 27 14:28:45 2009
@@ -55,7 +55,7 @@
 			if (blockSize > length) {
 				block = new byte[(int)length];
 			} else if (pos > (length-blockSize)) {
-				block = new byte[(int)length-blockSize];
+				block = new byte[(int)(length-pos)];
 			} else {
 				block = new byte[blockSize];
 			}
@@ -113,6 +113,10 @@
 	
 	@Override
 	public int read() throws IOException {
+		// At end of file.
+		if (position >= length) {
+			return -1;
+		}
 		byte[] block = getBlock(position);
 		long tmpPosition = position;
 		position++;
@@ -131,6 +135,15 @@
 			throw new NullPointerException("byte[] b is null.");
 		}
 
+		if (len == 0) {
+			return 0;
+		}
+		
+		// We're starting beyond the end of the file.
+		if (position >= length) {
+			return -1;
+		}
+		
 		// Check that the passed values are correct.
 		if (off < 0) {
 			throw new IndexOutOfBoundsException("off is < 0 ("+off+")");
@@ -174,7 +187,7 @@
 
 				// check if we've reached the end of the file.
 				if (position >= length) {					
-					return -1;
+					break;
 				} else if (remaining > 0) {
 					block = getBlock(position);				
 					blockPos = 0; // The next block will always be on a

Modified: incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java?rev=830201&r1=830200&r2=830201&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java
(original)
+++ incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/test/java/org/apache/kato/jvmti/util/CachedRandomAccessFileTest.java
Tue Oct 27 14:28:45 2009
@@ -318,4 +318,173 @@
 		
 		craf.close();
 	}
+	
+	/**
+	 * Tests that a linear read of the file, byte by byte,
+	 * produces a "-1" when the end of the file is reached.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadToEnd() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		for(int i =0 ; i < FILE_SIZE+1; i++) {
+			int value = craf.read();
+			
+			if (i == FILE_SIZE) {
+				assertEquals("End of file should have caused read() to return -1", -1, value);
+			}
+		}
+		
+		craf.close();
+	}
+
+	/**
+	 * Checks that read being passed null causes an expception to be
+	 * thrown.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadArrayNull() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		try{
+			craf.read(null,0,5);
+			fail("read failed to throw NullPointerException.");
+		}catch(NullPointerException e) {
+			// Expected
+		}
+		
+		craf.close();
+	}
+	
+	/**
+	 * Checks that an offset beyond the end of the array is caught.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadOffsetIOB() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		byte[] array = new byte[5];
+		
+		try{
+			craf.read(array,array.length,1);
+			fail("Read failed to produce IndexOutOfBoundsException.");
+		} catch(IndexOutOfBoundsException e) {
+			// Expected
+		}
+		
+		// 
+		try{
+			craf.read(array,array.length+1,1);
+			fail("Read failed to produce IndexOutOfBoundsException.");
+		} catch(IndexOutOfBoundsException e) {
+			// Expected
+		}
+		craf.close();
+	}
+	
+	
+	/**
+	 * Tests for IndexOutOfBoundsException when length passed
+	 * to read is larger than array size.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadLengthIOB() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		byte[] array = new byte[5];
+		
+		try{
+			craf.read(array,0, FILE_SIZE);
+			fail("Read failed to produce IndexOutOfBoundsException.");
+		} catch(IndexOutOfBoundsException e) {
+			// Expected
+		}
+		
+		// 
+		try{
+			craf.read(array,0,array.length+1);
+			fail("Read failed to produce IndexOutOfBoundsException.");
+		} catch(IndexOutOfBoundsException e) {
+			// Expected
+		}
+	}
+	
+	/**
+	 * Test for IndexOutOfBoundsException when reading into
+	 * array with negative offset.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadNegOffset() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		byte[] array = new byte[5];
+		
+		try{
+			craf.read(array,-1, 12);
+			fail("Read failed to produce IndexOutOfBoundsException.");
+		} catch(IndexOutOfBoundsException e) {
+			// Expected
+		}
+		
+		craf.close();
+	}
+	
+	/**
+	 * Asserts that an array with zero elements will have
+	 * not bytes read.
+	 * 
+	 * @throws Exception
+	 */
+	public void testZeroArray() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		byte[] array = new byte[0];
+		
+		int numRead = craf.read(array);
+		
+		assertEquals("No bytes should be read into zero size array.",0, numRead);
+	}
+	
+	/**
+	 * Tests that an array read that straddles the end of
+	 * the file can be read.
+	 * 
+	 * @throws Exception
+	 */
+	public void testReadStraddleEnd() throws Exception {
+		craf = new CachedRandomAccessFile(file,4,32);
+		
+		byte[] array = new byte[16];
+		
+		byte init=47;
+		
+		for (int i=0; i< array.length; i++) {
+			array[i] = init;
+		}
+		
+		int offset = 8;
+		craf.seek(FILE_SIZE-offset);
+		
+		int num=craf.read(array);
+		
+		assertEquals("End of file was reached, but bytes read was not returned.", offset,num);
+		
+		// Check that the bytes that should have been read are read.
+		for(int i=0; i<offset; i++) {
+			assertEquals("Bytes read did not match",FILE_SIZE-offset+i, array[i]);
+		}
+		
+		// check that the part of the array that could not be read is
+		// left asis.
+		for(int i=offset; i<array.length; i++) {
+			assertEquals("Unread section of array have been written.",init,array[i]);
+		}
+		
+		craf.close();
+	}
 }



Mime
View raw message