1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6191269 6709457 8000330
* @summary Test truncate method of FileChannel
* @key randomness
*/
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*;
import static java.nio.charset.StandardCharsets.*;
import java.util.Random;
/**
* Testing FileChannel's truncate method.
*/
public class Truncate {
private static final Random generator = new Random();
public static void main(String[] args) throws Exception {
File blah = File.createTempFile("blah", null);
blah.deleteOnExit();
try {
basicTest(blah);
appendTest(blah);
exceptionTests(blah);
} finally {
blah.delete();
}
}
/**
* Basic test of asserts in truncate's specification.
*/
static void basicTest(File blah) throws Exception {
for(int i=0; i<100; i++) {
long testSize = generator.nextInt(1000) + 10;
initTestFile(blah, testSize);
try (FileChannel fc = (i < 50) ?
new RandomAccessFile(blah, "rw").getChannel() :
FileChannel.open(blah.toPath(), READ, WRITE))
{
if (fc.size() != testSize)
throw new RuntimeException("Size failed");
long position = generator.nextInt((int)testSize*2);
fc.position(position);
long newSize = generator.nextInt((int)testSize*2);
fc.truncate(newSize);
// check new size
if (newSize > testSize) {
if (fc.size() != testSize)
throw new RuntimeException("Attempt to expand file changed size");
} else {
if (fc.size() != newSize)
throw new RuntimeException("Unexpected size after truncate");
}
// check new position
if (position > newSize) {
if (fc.position() != newSize)
throw new RuntimeException("Position greater than size");
} else {
if (fc.position() != position)
throw new RuntimeException("Truncate changed position");
};
}
}
}
/**
* Test behavior of truncate method when file is opened for append
*/
static void appendTest(File blah) throws Exception {
for (int i=0; i<10; i++) {
long testSize = generator.nextInt(1000) + 10;
initTestFile(blah, testSize);
try (FileChannel fc = (i < 5) ?
new FileOutputStream(blah, true).getChannel() :
FileChannel.open(blah.toPath(), APPEND))
{
// truncate file
long newSize = generator.nextInt((int)testSize);
fc.truncate(newSize);
if (fc.size() != newSize)
throw new RuntimeException("Truncate failed");
// write one byte
ByteBuffer buf = ByteBuffer.allocate(1);
buf.put((byte)'x');
buf.flip();
fc.write(buf);
if (fc.size() != (newSize+1))
throw new RuntimeException("Unexpected size");
}
}
}
/**
* Test exceptions specified by truncate method
*/
static void exceptionTests(File blah) throws Exception {
// check exceptions when channel opened for read access
try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) {
long size = fc.size();
// open channel
checkException(fc, 0L, NonWritableChannelException.class);
checkException(fc, -1L, NonWritableChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, NonWritableChannelException.class);
// closed channel
fc.close();
checkException(fc, 0L, ClosedChannelException.class);
checkException(fc, -1L, ClosedChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, ClosedChannelException.class);
}
// check exceptions when channel opened for write access
try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {
long size = fc.size();
// open channel
checkException(fc, -1L, IllegalArgumentException.class);
// closed channel
fc.close();
checkException(fc, 0L, ClosedChannelException.class);
checkException(fc, -1L, ClosedChannelException.class,
IllegalArgumentException.class);
checkException(fc, size+1L, ClosedChannelException.class);
}
}
/**
* Checks that FileChannel truncate throws one of the expected exceptions
* when invoked with the given size.
*/
private static void checkException(FileChannel fc, long size, Class<?>... expected)
throws IOException
{
Exception exc = null;
try {
fc.truncate(size);
} catch (Exception actual) {
exc = actual;
}
if (exc != null) {
for (Class<?> clazz: expected) {
if (clazz.isInstance(exc)) {
return;
}
}
}
System.err.println("Expected one of");
for (Class<?> clazz: expected) {
System.err.println(clazz);
}
if (exc == null) {
throw new RuntimeException("No expection thrown");
} else {
throw new RuntimeException("Unexpected exception thrown", exc);
}
}
/**
* Creates file blah of specified size in bytes.
*/
private static void initTestFile(File blah, long size) throws Exception {
try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) {
for(int i=0; i<size; i++) {
writer.write("e");
}
}
}
}
|