|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 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 | + * |
| 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. |
| 22 | + */ |
| 23 | + |
| 24 | +/* @test |
| 25 | + * @bug 8114830 |
| 26 | + * @summary Verify FileAlreadyExistsException is not thrown for REPLACE_EXISTING |
| 27 | + * @run junit CopyInterference |
| 28 | + */ |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.CopyOption; |
| 32 | +import java.nio.file.FileAlreadyExistsException; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.FileSystemException; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 39 | +import java.util.concurrent.ExecutionException; |
| 40 | +import java.util.concurrent.ExecutorService; |
| 41 | +import java.util.concurrent.Executors; |
| 42 | +import java.util.concurrent.Future; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | +import java.util.stream.Stream; |
| 45 | + |
| 46 | +import static java.nio.file.StandardCopyOption.*; |
| 47 | +import static java.nio.file.LinkOption.*; |
| 48 | + |
| 49 | +import org.junit.jupiter.params.ParameterizedTest; |
| 50 | +import org.junit.jupiter.params.provider.Arguments; |
| 51 | +import org.junit.jupiter.params.provider.MethodSource; |
| 52 | + |
| 53 | +public class CopyInterference { |
| 54 | + |
| 55 | + private static final int N_THREADS = 2; |
| 56 | + |
| 57 | + private static final AtomicBoolean running = new AtomicBoolean(true); |
| 58 | + |
| 59 | + private static class CopyTask implements Runnable { |
| 60 | + final Path source; |
| 61 | + final Path target; |
| 62 | + final CopyOption[] options; |
| 63 | + |
| 64 | + CopyTask(Path source, Path target, CopyOption[] options) { |
| 65 | + this.source = source; |
| 66 | + this.target = target; |
| 67 | + this.options = options; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void run() { |
| 72 | + try { |
| 73 | + while (running.get()) { |
| 74 | + Files.copy(source, target, options); |
| 75 | + } |
| 76 | + } catch (FileAlreadyExistsException e) { |
| 77 | + throw new RuntimeException("Unexpected exception", e); |
| 78 | + } catch (FileSystemException e) { |
| 79 | + System.out.printf("Expected FileSystemException: \"%s\"%n", |
| 80 | + e.getMessage()); |
| 81 | + } catch (IOException e) { |
| 82 | + throw new RuntimeException("Unexpected exception", e); |
| 83 | + } finally { |
| 84 | + running.set(false); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static Stream<Arguments> pathAndOptionsProvider() |
| 90 | + throws IOException { |
| 91 | + Path parent = Path.of(System.getProperty("test.dir", ".")); |
| 92 | + Path dir = Files.createTempDirectory(parent, "foobargus"); |
| 93 | + |
| 94 | + List<Arguments> list = new ArrayList<Arguments>(); |
| 95 | + |
| 96 | + // regular file |
| 97 | + Path sourceFile = Files.createTempFile(dir, "foo", "baz"); |
| 98 | + Class c = CopyInterference.class; |
| 99 | + String name = "CopyInterference.class"; |
| 100 | + |
| 101 | + try (InputStream in = c.getResourceAsStream(name)) { |
| 102 | + Files.copy(in, sourceFile, REPLACE_EXISTING); |
| 103 | + } |
| 104 | + |
| 105 | + Arguments args = Arguments.of(sourceFile, dir.resolve("targetFile"), |
| 106 | + new CopyOption[] {REPLACE_EXISTING}); |
| 107 | + list.add(args); |
| 108 | + |
| 109 | + // directory |
| 110 | + Path sourceDirectory = Files.createTempDirectory(dir, "fubar"); |
| 111 | + args = Arguments.of(sourceDirectory, dir.resolve("targetDir"), |
| 112 | + new CopyOption[] {REPLACE_EXISTING}); |
| 113 | + list.add(args); |
| 114 | + |
| 115 | + // symblic link, followed |
| 116 | + Path link = dir.resolve("link"); |
| 117 | + Files.createSymbolicLink(link, sourceFile); |
| 118 | + args = Arguments.of(link, dir.resolve("linkFollowed"), |
| 119 | + new CopyOption[] {REPLACE_EXISTING}); |
| 120 | + list.add(args); |
| 121 | + |
| 122 | + // symblic link, not followed |
| 123 | + args = Arguments.of(link, dir.resolve("linkNotFollowed"), |
| 124 | + new CopyOption[] {REPLACE_EXISTING, NOFOLLOW_LINKS}); |
| 125 | + list.add(args); |
| 126 | + |
| 127 | + return list.stream(); |
| 128 | + } |
| 129 | + |
| 130 | + @ParameterizedTest |
| 131 | + @MethodSource("pathAndOptionsProvider") |
| 132 | + void copy(Path source, Path target, CopyOption[] options) |
| 133 | + throws InterruptedException, IOException { |
| 134 | + |
| 135 | + Future<?>[] results = new Future<?>[N_THREADS]; |
| 136 | + try (ExecutorService es = Executors.newFixedThreadPool(N_THREADS)) { |
| 137 | + CopyTask copyTask = new CopyTask(source, target, options); |
| 138 | + for (int i = 0; i < N_THREADS; i++) |
| 139 | + results[i] = es.submit(copyTask); |
| 140 | + } |
| 141 | + |
| 142 | + for (Future<?> res : results) { |
| 143 | + try { |
| 144 | + res.get(); |
| 145 | + } catch (ExecutionException e) { |
| 146 | + throw new RuntimeException(res.exceptionNow()); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments