You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2020/10/19 06:04:38 UTC

svn commit: r1882631 - in /pdfbox/trunk/pdfbox/src/test: java/org/apache/pdfbox/io/ resources/org/apache/pdfbox/io/

Author: lehmi
Date: Mon Oct 19 06:04:38 2020
New Revision: 1882631

URL: http://svn.apache.org/viewvc?rev=1882631&view=rev
Log:
PDFBOX-4855: add tests for RandomAccessReadMemoryMappedFile

Added:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java   (with props)
    pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadEmptyFile.txt
      - copied unchanged from r1882630, pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadBufferedEmptyFile.txt
    pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadFile1.txt
      - copied unchanged from r1882630, pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadBufferedFile1.txt
Removed:
    pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadBufferedEmptyFile.txt
    pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/io/RandomAccessReadBufferedFile1.txt
Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java?rev=1882631&r1=1882630&r2=1882631&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java Mon Oct 19 06:04:38 2020
@@ -36,7 +36,7 @@ public class RandomAccessReadBufferedFil
     public void testPositionSkip() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         assertEquals(0, randomAccessSource.getPosition());
         randomAccessSource.skip(5);
@@ -50,7 +50,7 @@ public class RandomAccessReadBufferedFil
     public void testPositionRead() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         assertEquals(0, randomAccessSource.getPosition());
         assertEquals('0', randomAccessSource.read());
@@ -67,7 +67,7 @@ public class RandomAccessReadBufferedFil
     public void testSeekEOF() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         randomAccessSource.seek(3);
         assertEquals(3, randomAccessSource.getPosition());
@@ -104,7 +104,7 @@ public class RandomAccessReadBufferedFil
     public void testPositionReadBytes() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         assertEquals(0, randomAccessSource.getPosition());
         byte[] buffer = new byte[4];
@@ -127,7 +127,7 @@ public class RandomAccessReadBufferedFil
     public void testPositionPeek() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         assertEquals(0, randomAccessSource.getPosition());
         randomAccessSource.skip(6);
@@ -143,7 +143,7 @@ public class RandomAccessReadBufferedFil
     public void testPositionUnreadBytes() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedFile1.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
 
         assertEquals(0, randomAccessSource.getPosition());
         randomAccessSource.read();
@@ -167,7 +167,7 @@ public class RandomAccessReadBufferedFil
     public void testEmptyBuffer() throws IOException, URISyntaxException
     {
         RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
-                new File(getClass().getResource("RandomAccessReadBufferedEmptyFile.txt").toURI()));
+                new File(getClass().getResource("RandomAccessReadEmptyFile.txt").toURI()));
 
         assertEquals(-1, randomAccessSource.read());
         assertEquals(-1, randomAccessSource.peek());

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java?rev=1882631&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java Mon Oct 19 06:04:38 2020
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2014 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.pdfbox.io;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import org.junit.Test;
+
+/**
+ * Unittest for {@link org.apache.pdfbox.io.RandomAccessReadMemoryMappedFile}
+ */
+public class RandomAccessReadMemoryMappedFileTest
+{
+    @Test
+    public void testPositionSkip() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        assertEquals(0, randomAccessSource.getPosition());
+        randomAccessSource.skip(5);
+        assertEquals('5', randomAccessSource.read());
+        assertEquals(6, randomAccessSource.getPosition());
+
+        randomAccessSource.close();
+    }
+
+    @Test
+    public void testPositionRead() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        assertEquals(0, randomAccessSource.getPosition());
+        assertEquals('0', randomAccessSource.read());
+        assertEquals('1', randomAccessSource.read());
+        assertEquals('2', randomAccessSource.read());
+        assertEquals(3, randomAccessSource.getPosition());
+
+        assertFalse(randomAccessSource.isClosed());
+        randomAccessSource.close();
+        assertTrue(randomAccessSource.isClosed());
+    }
+
+    @Test
+    public void testSeekEOF() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        randomAccessSource.seek(3);
+        assertEquals(3, randomAccessSource.getPosition());
+        
+        try
+        {
+            randomAccessSource.seek(-1);
+            fail("seek should have thrown an IOException");
+        }
+        catch (IOException e)
+        {
+            
+        }
+        
+        assertFalse(randomAccessSource.isEOF());
+        randomAccessSource.seek(randomAccessSource.length());
+        assertTrue(randomAccessSource.isEOF());
+        assertEquals(-1, randomAccessSource.read());
+        assertEquals(-1, randomAccessSource.read(new byte[1], 0, 1));
+
+        randomAccessSource.close();
+        try
+        {
+            randomAccessSource.read();
+            fail("checkClosed should have thrown an IOException");
+        }
+        catch (IOException e)
+        {
+
+        }
+    }
+
+    @Test
+    public void testPositionReadBytes() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        assertEquals(0, randomAccessSource.getPosition());
+        byte[] buffer = new byte[4];
+        randomAccessSource.read(buffer);
+        assertEquals('0', buffer[0]);
+        assertEquals('3', buffer[3]);
+        assertEquals(4, randomAccessSource.getPosition());
+
+        randomAccessSource.read(buffer, 1, 2);
+        assertEquals('0', buffer[0]);
+        assertEquals('4', buffer[1]);
+        assertEquals('5', buffer[2]);
+        assertEquals('3', buffer[3]);
+        assertEquals(6, randomAccessSource.getPosition());
+
+        randomAccessSource.close();
+    }
+
+    @Test
+    public void testPositionPeek() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        assertEquals(0, randomAccessSource.getPosition());
+        randomAccessSource.skip(6);
+        assertEquals(6, randomAccessSource.getPosition());
+
+        assertEquals('6', randomAccessSource.peek());
+        assertEquals(6, randomAccessSource.getPosition());
+
+        randomAccessSource.close();
+    }
+
+    @Test
+    public void testPositionUnreadBytes() throws IOException, URISyntaxException
+    {
+        RandomAccessRead randomAccessSource = new RandomAccessReadMemoryMappedFile(
+                new File(getClass().getResource("RandomAccessReadFile1.txt").toURI()));
+
+        assertEquals(0, randomAccessSource.getPosition());
+        randomAccessSource.read();
+        randomAccessSource.read();
+        byte[] readBytes = new byte[6];
+        assertEquals(readBytes.length, randomAccessSource.read(readBytes));
+        assertEquals(8, randomAccessSource.getPosition());
+        randomAccessSource.rewind(readBytes.length);
+        assertEquals(2, randomAccessSource.getPosition());
+        assertEquals('2', randomAccessSource.read());
+        assertEquals(3, randomAccessSource.getPosition());
+        randomAccessSource.read(readBytes, 2, 4);
+        assertEquals(7, randomAccessSource.getPosition());
+        randomAccessSource.rewind(4);
+        assertEquals(3, randomAccessSource.getPosition());
+
+        randomAccessSource.close();
+    }
+
+}

Propchange: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java
------------------------------------------------------------------------------
    svn:eol-style = native