package org.dict.zip;
import static org.testng.Assert.*;
+
+import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
in.close();
}
- private synchronized void getDZHeader(DictZipInputStream din) throws IOException {
- header = din.readHeader();
- }
-
/**
* Test of read method, of class DictZipInputStream.
*/
public void testRead() throws Exception {
System.out.println("read");
int len = 10;
- getDZHeader(din);
byte[] buf = new byte[len];
byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
din.read(buf, 0, len);
System.out.println("read with seek");
int start = 0x20;
int len = 10;
- getDZHeader(din);
din.seek(start);
byte[] buf = new byte[len];
byte[] expResult = {
}
/**
+ * Test of read method, of class DictZipInputStream.
+ */
+ @Test
+ public void testRead_null() throws Exception {
+ System.out.println("read null buffer");
+ int len = 10;
+ byte[] buf = null;
+ boolean r = false;
+ try {
+ din.read(buf, 0, len);
+ fail("Should be throw exception.");
+ } catch (NullPointerException e) {
+ // expected.
+ r = true;
+ }
+ assertTrue(r, "Got NullPointerException when buffer is null");
+ }
+
+ /**
+ * Test of read method, of class DictZipInputStream.
+ */
+ @Test
+ public void testRead_outOfBound() throws Exception {
+ System.out.println("read out of buffer size");
+ int len = 10;
+ byte[] buf = new byte[len];
+ boolean r = false;
+ try {
+ din.read(buf, 0, len + 10);
+ fail("Should be throw exception.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected.
+ r = true;
+ }
+ assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
+ }
+
+ /**
+ * Test of read method, of class DictZipInputStream.
+ */
+ @Test
+ public void testRead_zeroSize() throws Exception {
+ System.out.println("read zero size");
+ int len = 512;
+ byte[] buf = new byte[len];
+ int size = din.read(buf, 0, 0);
+ throw new SkipException("Unknown bug.");
+ //assertEquals(size, 0);
+ }
+
+ /**
+ * Test of read method, of class DictZipInputStream.
+ */
+ @Test
+ public void testRead_with_seekLast() throws Exception {
+ System.out.println("read with seek to last");
+ int start = 383273;
+ int len = 512;
+ din.seek(start);
+ byte[] buf = new byte[len];
+ din.read(buf, 0, len);
+ int result = din.read(buf, 0, len);
+ assertEquals(result, -1);
+ }
+
+ /**
* Test of readFully method, of class DictZipInputStream.
* @throws java.lang.Exception
*/
System.out.println("readFully");
int start = 1;
int len = 10;
- getDZHeader(din);
- int off = header.getOffset(start);
- long pos = header.getPosition(start);
- in.seek(pos);
- byte[] buf = new byte[off + len];
+ in.seek(start);
+ byte[] buf = new byte[len];
din.readFully(buf);
}
System.out.println("readFully");
int start = 1;
int len = 10;
- getDZHeader(din);
- int off = header.getOffset(start);
- long pos = header.getPosition(start);
- in.seek(pos);
- byte[] buf = new byte[off + len];
- din.readFully(buf, off, len);
+ in.seek(start);
+ byte[] buf = new byte[len];
+ din.readFully(buf, 0, len);
}
-
/**
* Test of readFully method, of class DictZipInputStream.
* @throws java.lang.Exception
*/
@Test
- public void testReadFully_checkTrailer() throws Exception {
- System.out.println("readFully and checkTrailer");
- getDZHeader(din);
+ public void testReadFully_readTrailer() throws IOException {
+ System.out.println("readFully and readTrailer");
byte[] buf = new byte[512];
try {
din.readFully(buf);
assertEquals(din.getLength(), 383783);
}
- /**
- * Test of readHeader method, of class DictZipInputStream.
- * @throws java.lang.Exception
- */
- @Test
- public void testReadHeader() throws Exception {
- System.out.println("readHeader");
- header = din.readHeader();
- StringBuilder sb = new StringBuilder();
- sb.append("\nHeader length = 49");
- sb.append("\nSubfield ID = RA");
- sb.append("\nSubfield length = 20");
- sb.append("\nSubfield version = 1");
- sb.append("\nChunk length = 58315");
- sb.append("\nNumber of chunks = 7");
- String expResult = sb.toString();
- assertEquals(header.toString(), expResult);
- }
-
}