1 // Copyright 2016 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.archivepatcher.shared; 16 17 import org.junit.After; 18 import org.junit.Assert; 19 import org.junit.Before; 20 import org.junit.Test; 21 import org.junit.runner.RunWith; 22 import org.junit.runners.JUnit4; 23 24 import java.io.DataInputStream; 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 import java.io.RandomAccessFile; 29 30 /** 31 * Tests for {@link RandomAccessFileOutputStream}. 32 */ 33 @RunWith(JUnit4.class) 34 @SuppressWarnings("javadoc") 35 public class RandomAccessFileOutputStreamTest { 36 /** 37 * The object under test. 38 */ 39 private RandomAccessFileOutputStream stream = null; 40 41 /** 42 * Test data written to the file. 43 */ 44 private byte[] testData = null; 45 46 /** 47 * The temp file. 48 */ 49 private File tempFile = null; 50 51 @Before 52 public void setup() throws IOException { 53 testData = new byte[128]; 54 for (int x = 0; x < 128; x++) { 55 testData[x] = (byte) x; 56 } 57 tempFile = File.createTempFile("ra-fost", "tmp"); 58 tempFile.deleteOnExit(); 59 } 60 61 @After 62 public void tearDown() { 63 try { 64 stream.close(); 65 } catch (Exception ignored) { 66 // Nothing to do 67 } 68 try { 69 tempFile.delete(); 70 } catch (Exception ignored) { 71 // Nothing to do 72 } 73 } 74 75 @Test 76 public void testCreateAndSize() throws IOException { 77 stream = new RandomAccessFileOutputStream(tempFile, 11L); 78 Assert.assertEquals(11, tempFile.length()); 79 } 80 81 @Test(expected = IOException.class) 82 public void testCreateAndFailToSize() throws IOException { 83 stream = 84 new RandomAccessFileOutputStream(tempFile, 11L) { 85 @Override 86 protected RandomAccessFile getRandomAccessFile(File file) throws IOException { 87 return new RandomAccessFile(file, "rw") { 88 @Override 89 public void setLength(long newLength) throws IOException { 90 // Do nothing, to trigger failure case in the constructor. 91 } 92 }; 93 } 94 }; 95 } 96 97 @Test 98 public void testWrite() throws IOException { 99 stream = new RandomAccessFileOutputStream(tempFile, 1L); 100 stream.write(7); 101 stream.flush(); 102 stream.close(); 103 FileInputStream in = null; 104 try { 105 in = new FileInputStream(tempFile); 106 Assert.assertEquals(7, in.read()); 107 } finally { 108 try { 109 in.close(); 110 } catch (Exception ignored) { 111 // Nothing 112 } 113 } 114 } 115 116 @Test 117 public void testWriteArray() throws IOException { 118 stream = new RandomAccessFileOutputStream(tempFile, 1L); 119 stream.write(testData, 0, testData.length); 120 stream.flush(); 121 stream.close(); 122 FileInputStream in = null; 123 DataInputStream dataIn = null; 124 try { 125 in = new FileInputStream(tempFile); 126 dataIn = new DataInputStream(in); 127 byte[] actual = new byte[testData.length]; 128 dataIn.readFully(actual); 129 Assert.assertArrayEquals(testData, actual); 130 } finally { 131 if (dataIn != null) { 132 try { 133 dataIn.close(); 134 } catch (Exception ignored) { 135 // Nothing 136 } 137 } 138 if (in != null) { 139 try { 140 in.close(); 141 } catch (Exception ignored) { 142 // Nothing 143 } 144 } 145 } 146 } 147 } 148