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