Path: blob/master/test/jdk/java/nio/channels/FileChannel/Truncate.java
41154 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 6191269 6709457 800033025* @summary Test truncate method of FileChannel26* @key randomness27*/2829import java.io.*;30import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.nio.file.Files;33import static java.nio.file.StandardOpenOption.*;34import static java.nio.charset.StandardCharsets.*;35import java.util.Random;363738/**39* Testing FileChannel's truncate method.40*/4142public class Truncate {43private static final Random generator = new Random();4445public static void main(String[] args) throws Exception {46File blah = File.createTempFile("blah", null);47blah.deleteOnExit();48try {49basicTest(blah);50appendTest(blah);51exceptionTests(blah);52} finally {53blah.delete();54}55}5657/**58* Basic test of asserts in truncate's specification.59*/60static void basicTest(File blah) throws Exception {61for(int i=0; i<100; i++) {62long testSize = generator.nextInt(1000) + 10;63initTestFile(blah, testSize);6465try (FileChannel fc = (i < 50) ?66new RandomAccessFile(blah, "rw").getChannel() :67FileChannel.open(blah.toPath(), READ, WRITE))68{69if (fc.size() != testSize)70throw new RuntimeException("Size failed");7172long position = generator.nextInt((int)testSize*2);73fc.position(position);7475long newSize = generator.nextInt((int)testSize*2);76fc.truncate(newSize);7778// check new size79if (newSize > testSize) {80if (fc.size() != testSize)81throw new RuntimeException("Attempt to expand file changed size");82} else {83if (fc.size() != newSize)84throw new RuntimeException("Unexpected size after truncate");85}8687// check new position88if (position > newSize) {89if (fc.position() != newSize)90throw new RuntimeException("Position greater than size");91} else {92if (fc.position() != position)93throw new RuntimeException("Truncate changed position");94};95}96}97}9899/**100* Test behavior of truncate method when file is opened for append101*/102static void appendTest(File blah) throws Exception {103for (int i=0; i<10; i++) {104long testSize = generator.nextInt(1000) + 10;105initTestFile(blah, testSize);106try (FileChannel fc = (i < 5) ?107new FileOutputStream(blah, true).getChannel() :108FileChannel.open(blah.toPath(), APPEND))109{110// truncate file111long newSize = generator.nextInt((int)testSize);112fc.truncate(newSize);113if (fc.size() != newSize)114throw new RuntimeException("Truncate failed");115116// write one byte117ByteBuffer buf = ByteBuffer.allocate(1);118buf.put((byte)'x');119buf.flip();120fc.write(buf);121if (fc.size() != (newSize+1))122throw new RuntimeException("Unexpected size");123}124}125}126127/**128* Test exceptions specified by truncate method129*/130static void exceptionTests(File blah) throws Exception {131// check exceptions when channel opened for read access132try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) {133long size = fc.size();134135// open channel136checkException(fc, 0L, NonWritableChannelException.class);137138checkException(fc, -1L, NonWritableChannelException.class,139IllegalArgumentException.class);140141checkException(fc, size+1L, NonWritableChannelException.class);142143// closed channel144fc.close();145146checkException(fc, 0L, ClosedChannelException.class);147148checkException(fc, -1L, ClosedChannelException.class,149IllegalArgumentException.class);150151checkException(fc, size+1L, ClosedChannelException.class);152}153154// check exceptions when channel opened for write access155try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {156long size = fc.size();157158// open channel159checkException(fc, -1L, IllegalArgumentException.class);160161// closed channel162fc.close();163164checkException(fc, 0L, ClosedChannelException.class);165166checkException(fc, -1L, ClosedChannelException.class,167IllegalArgumentException.class);168169checkException(fc, size+1L, ClosedChannelException.class);170}171}172173/**174* Checks that FileChannel truncate throws one of the expected exceptions175* when invoked with the given size.176*/177private static void checkException(FileChannel fc, long size, Class<?>... expected)178throws IOException179{180Exception exc = null;181try {182fc.truncate(size);183} catch (Exception actual) {184exc = actual;185}186if (exc != null) {187for (Class<?> clazz: expected) {188if (clazz.isInstance(exc)) {189return;190}191}192}193System.err.println("Expected one of");194for (Class<?> clazz: expected) {195System.err.println(clazz);196}197if (exc == null) {198throw new RuntimeException("No expection thrown");199} else {200throw new RuntimeException("Unexpected exception thrown", exc);201}202}203204/**205* Creates file blah of specified size in bytes.206*/207private static void initTestFile(File blah, long size) throws Exception {208try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) {209for(int i=0; i<size; i++) {210writer.write("e");211}212}213}214}215216217