Skip to content

Commit d75998e

Browse files
committed
Refactor tests to isolate deprecated constructor use
1 parent b04c9f5 commit d75998e

1 file changed

Lines changed: 64 additions & 20 deletions

File tree

src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,34 @@ private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
4646
return RandomAccessFileMode.READ_ONLY.create(DATA_FILE_NAME);
4747
}
4848

49+
@SuppressWarnings("resource")
4950
@Test
5051
public void testAvailableAfterClose() throws IOException {
51-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
52-
true)) {
52+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
53+
.setRandomAccessFile(createRandomAccessFile())
54+
.setCloseOnClose(true)
55+
.get()) {
5356
inputStream.close();
5457
assertEquals(0, inputStream.available());
5558
}
5659
}
5760

5861
@Test
5962
public void testAvailableAfterOpen() throws IOException {
60-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
61-
true)) {
63+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
64+
.setRandomAccessFile(createRandomAccessFile())
65+
.setCloseOnClose(true)
66+
.get()) {
6267
assertEquals(DATA_FILE_LEN, inputStream.available());
6368
}
6469
}
6570

6671
@Test
6772
public void testAvailableLong() throws IOException {
68-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
69-
true)) {
73+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
74+
.setRandomAccessFile(createRandomAccessFile())
75+
.setCloseOnClose(true)
76+
.get()) {
7077
assertEquals(DATA_FILE_LEN, inputStream.availableLong());
7178
}
7279
}
@@ -130,7 +137,9 @@ public void testBuilderRandomAccessFile() throws IOException {
130137
@Test
131138
public void testConstructorCloseOnCloseFalse() throws IOException {
132139
try (RandomAccessFile file = createRandomAccessFile()) {
133-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, false)) {
140+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
141+
.setRandomAccessFile(createRandomAccessFile())
142+
.get()) {
134143
assertFalse(inputStream.isCloseOnClose());
135144
assertNotEquals(-1, inputStream.getRandomAccessFile().read());
136145
}
@@ -140,7 +149,7 @@ public void testConstructorCloseOnCloseFalse() throws IOException {
140149

141150
@SuppressWarnings("resource") // instance variable access
142151
@Test
143-
public void testConstructorCloseOnCloseTrue() throws IOException {
152+
public void testDeprecatedConstructorCloseOnCloseTrue() throws IOException {
144153
try (RandomAccessFile file = createRandomAccessFile()) {
145154
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, true)) {
146155
assertTrue(inputStream.isCloseOnClose());
@@ -152,7 +161,7 @@ public void testConstructorCloseOnCloseTrue() throws IOException {
152161

153162
@SuppressWarnings("resource") // instance variable access
154163
@Test
155-
public void testConstructorRandomAccessFile() throws IOException {
164+
public void testDeprecatedConstructorRandomAccessFile() throws IOException {
156165
try (RandomAccessFile file = createRandomAccessFile()) {
157166
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file)) {
158167
assertFalse(inputStream.isCloseOnClose());
@@ -162,15 +171,40 @@ public void testConstructorRandomAccessFile() throws IOException {
162171
}
163172
}
164173

174+
@SuppressWarnings("deprecation")
175+
@Test
176+
public void testDeprecatedConstructors() throws IOException {
177+
try (RandomAccessFile randomAccessFile = createRandomAccessFile()) {
178+
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(randomAccessFile)) {
179+
assertFalse(inputStream.isCloseOnClose());
180+
assertEquals(randomAccessFile, inputStream.getRandomAccessFile());
181+
}
182+
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(randomAccessFile, true)) {
183+
assertTrue(inputStream.isCloseOnClose());
184+
assertEquals(randomAccessFile, inputStream.getRandomAccessFile());
185+
}
186+
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(randomAccessFile, false)) {
187+
assertFalse(inputStream.isCloseOnClose());
188+
assertEquals(randomAccessFile, inputStream.getRandomAccessFile());
189+
}
190+
}
191+
}
192+
193+
@SuppressWarnings("deprecation")
165194
@Test
166-
public void testConstructorRandomAccessFileNull() {
195+
public void testDeprecatedConstructorsNull() {
167196
assertThrows(NullPointerException.class, () -> new RandomAccessFileInputStream(null));
197+
assertThrows(NullPointerException.class, () -> new RandomAccessFileInputStream(null, true));
198+
assertThrows(NullPointerException.class, () -> new RandomAccessFileInputStream(null, false));
168199
}
169200

170201
@Test
171202
public void testGetters() throws IOException {
172203
try (RandomAccessFile file = createRandomAccessFile()) {
173-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, true)) {
204+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
205+
.setRandomAccessFile(file)
206+
.setCloseOnClose(true)
207+
.get()) {
174208
assertEquals(file, inputStream.getRandomAccessFile());
175209
assertTrue(inputStream.isCloseOnClose());
176210
}
@@ -179,8 +213,10 @@ public void testGetters() throws IOException {
179213

180214
@Test
181215
public void testRead() throws IOException {
182-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
183-
true)) {
216+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
217+
.setRandomAccessFile(createRandomAccessFile())
218+
.setCloseOnClose(true)
219+
.get()) {
184220
// A Test Line.
185221
assertEquals('A', inputStream.read());
186222
assertEquals(' ', inputStream.read());
@@ -201,17 +237,21 @@ public void testRead() throws IOException {
201237

202238
@Test
203239
public void testReadAfterClose() throws IOException {
204-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
205-
true)) {
240+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
241+
.setRandomAccessFile(createRandomAccessFile())
242+
.setCloseOnClose(true)
243+
.get()) {
206244
inputStream.close();
207245
assertThrows(IOException.class, inputStream::read);
208246
}
209247
}
210248

211249
@Test
212250
public void testReadByteArray() throws IOException {
213-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
214-
true)) {
251+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
252+
.setRandomAccessFile(createRandomAccessFile())
253+
.setCloseOnClose(true)
254+
.get()) {
215255
// A Test Line.
216256
final int dataLen = 12;
217257
final byte[] buffer = new byte[dataLen];
@@ -225,8 +265,10 @@ public void testReadByteArray() throws IOException {
225265

226266
@Test
227267
public void testReadByteArrayBounds() throws IOException {
228-
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
229-
true)) {
268+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
269+
.setRandomAccessFile(createRandomAccessFile())
270+
.setCloseOnClose(true)
271+
.get()) {
230272
// A Test Line.
231273
final int dataLen = 12;
232274
final byte[] buffer = new byte[dataLen];
@@ -242,7 +284,9 @@ public void testReadByteArrayBounds() throws IOException {
242284
public void testSkip() throws IOException {
243285

244286
try (RandomAccessFile file = createRandomAccessFile();
245-
final RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, false)) {
287+
final RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
288+
.setRandomAccessFile(file)
289+
.get()) {
246290
assertEquals(0, inputStream.skip(-1));
247291
assertEquals(0, inputStream.skip(Integer.MIN_VALUE));
248292
assertEquals(0, inputStream.skip(0));

0 commit comments

Comments
 (0)