Path: blob/master/test/jdk/java/nio/file/Files/CallWithInterruptSet.java
41153 views
/*1* Copyright (c) 2018, 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/*24* @test25* @bug 820561226* @run testng CallWithInterruptSet27* @summary Test invoking Files methods with the interrupt status set28*/2930import java.io.InputStream;31import java.io.OutputStream;32import java.io.IOException;33import java.io.BufferedReader;34import java.io.BufferedWriter;35import java.io.Reader;36import java.io.Writer;37import java.nio.file.Files;38import java.nio.file.Path;3940import org.testng.annotations.Test;41import static org.testng.Assert.*;4243public class CallWithInterruptSet {4445@Test46public void testReadAllBytes() throws Exception {47Path file = mkfile(100);48Thread.currentThread().interrupt();49try {50byte[] bytes = Files.readAllBytes(file);51assertTrue(bytes.length == 100);52} finally {53assertTrue(Thread.interrupted()); // clear interrupt54}55}5657@Test58public void testInputStream() throws IOException {59Path file = mkfile(100);60Thread.currentThread().interrupt();61try (InputStream in = Files.newInputStream(file)) {62int n = in.read(new byte[10]);63assertTrue(n > 0);64} finally {65assertTrue(Thread.interrupted()); // clear interrupt66}67}6869@Test70public void testOutputStream() throws Exception {71Path file = mkfile();72try (OutputStream out = Files.newOutputStream(file)) {73Thread.currentThread().interrupt();74out.write(new byte[10]);75} finally {76assertTrue(Thread.interrupted()); // clear interrupt77}78assertTrue(Files.size(file) == 10);79}8081@Test82public void testReadString() throws Exception {83Path file = mkfile();84Files.writeString(file, "hello");85Thread.currentThread().interrupt();86try {87String msg = Files.readString(file);88assertEquals(msg, "hello");89} finally {90assertTrue(Thread.interrupted()); // clear interrupt91}92}9394@Test95public void testWriteString() throws Exception {96Path file = mkfile();97Thread.currentThread().interrupt();98try {99Files.writeString(file, "hello");100} finally {101assertTrue(Thread.interrupted()); // clear interrupt102}103String msg = Files.readString(file);104assertEquals(msg, "hello");105}106107@Test108public void testBufferedReader() throws Exception {109Path file = mkfile();110Files.writeString(file, "hello");111Thread.currentThread().interrupt();112try (BufferedReader reader = Files.newBufferedReader(file)) {113String msg = reader.readLine();114assertEquals(msg, "hello");115} finally {116assertTrue(Thread.interrupted()); // clear interrupt117}118}119120@Test121public void testBufferedWriter() throws Exception {122Path file = mkfile();123Thread.currentThread().interrupt();124try (BufferedWriter writer = Files.newBufferedWriter(file)) {125writer.write("hello");126} finally {127assertTrue(Thread.interrupted()); // clear interrupt128}129String msg = Files.readString(file);130assertEquals(msg, "hello");131}132133private Path mkfile() throws IOException {134return Files.createTempFile(Path.of("."), "tmp", "tmp");135}136137private Path mkfile(int size) throws IOException {138return Files.write(mkfile(), new byte[size]);139}140141}142143144