/* * Copyright (c) 2020 Microsoft Corporation. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
/** * Setup for the test: * + Create a temporary directory where all subsequently created temp * directories will be in. This directory and all of its contents will be * deleted when the test finishes. * + Find a drive that is available for use with subst.
*/
@BeforeClass publicvoid setup() throws IOException {
TEST_TEMP_DIRECTORY = Files.createTempDirectory("tmp");
System.out.printf("Test directory is at %s\n", TEST_TEMP_DIRECTORY);
Optional<Path> substDrive = findAvailableDrive(TEST_TEMP_DIRECTORY); if (!substDrive.isPresent()) { thrownew SkipException( "Could not find any available drive to use with subst, skipping the tests");
}
SUBST_DRIVE = substDrive.get();
System.out.printf("Using drive %s\n with subst", SUBST_DRIVE);
}
/** * Delete the root temporary directory together with all of its contents * when all tests finish.
*/
@AfterClass publicvoid removeRootTempDirectory() throws IOException {
TestUtil.removeAll(TEST_TEMP_DIRECTORY);
}
/** * Each test method maps drive `SUBST_DRIVE` to a temporary directory, * unmap the drive after every test so that subsequent ones can reuse * the drive.
*/
@AfterMethod publicvoid deleteSubstDrive() throws IOException {
Stream<String> substitutedDrives = substFindMappedDrives(); // Only delete `SUBST_DRIVE` if it is currently being substituted if (substitutedDrives.anyMatch(e -> e.contains(SUBST_DRIVE.toString()))) {
substDelete(SUBST_DRIVE);
}
}
/** * Test whether files can be created in the substituted drive.
*/
@Test publicvoid testCreateAndDeleteFile() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
substCreate(SUBST_DRIVE, tempDirectory);
/** * Test if we can delete the substituted drive (essentially just a directory).
*/
@Test publicvoid testDeleteSubstitutedDrive() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
substCreate(SUBST_DRIVE, tempDirectory);
/** * Test if the attributes returned by the Files' APIs are consistent when * using the actual path and the substituted path.
*/
@Test publicvoid testAttributes() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
substCreate(SUBST_DRIVE, tempDirectory);
/** * Test if setting attributes for a substituted path works the same way * as it would for a real path.
*/
@Test publicvoid testGetSetAttributes() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
substCreate(SUBST_DRIVE, tempDirectory);
/** * Test if the FileStores returned from using substituted path and real path * are the same.
*/
@Test publicvoid testFileStore() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
substCreate(SUBST_DRIVE, tempDirectory);
/** * Test if Files.copy works correctly on a substituted drive, and that * all of the attributes are the same.
*/
@Test publicvoid testMoveAndCopySubstDrive() throws IOException {
Path tempDirectory = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp");
Path tempDirectoryCopy = Path.of(tempDirectory.toString() + "_copy");
/** * Test if the attributes of a resolved symlink are the same as its target's * Note: requires administrator privileges.
*/
@Test publicvoid testGetResolvedSymlinkAttribute() throws IOException { if (!TestUtil.supportsLinks(TEST_TEMP_DIRECTORY)) { return;
}
/** * Test if files and directories can be created, moved, and cut when the * substituted drive is a symlink. * Note: requires administrator privileges.
*/
@Test publicvoid testSubstWithSymlinkedDirectory() throws IOException { if (!TestUtil.supportsLinks(TEST_TEMP_DIRECTORY)) { return;
}
/** * When the substituted drive is a symlink, test if it has the same * attributes as its target. * Note: requires administrator privileges.
*/
@Test publicvoid testMoveAndCopyFilesToSymlinkedDrive() throws IOException { if (!TestUtil.supportsLinks(TEST_TEMP_DIRECTORY)) { return;
}
/** * Return a list of strings that represents all the currently mapped drives. * For instance, with the following output of subst: * A:\: => path1 * B:\: => path2 * T:\: => path3 * X:\: => path4 * The function returns: ["A:\", "B:\", "T:\", "X:\"] * For reference, see: * https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/subst
*/ private Stream<String> substFindMappedDrives() throws UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String utf8 = StandardCharsets.UTF_8.name(); try (PrintStream ps = new PrintStream(baos, true, utf8)) { // subst without any arguments returns a list of drives that // are being substituted
runCmd(new ProcessBuilder("cmd", "/c", "subst"), ps);
String stdout = baos.toString(utf8); return stdout // split lines
.lines() // only examine lines with "=>"
.filter(line -> line.contains("=>")) // split each line into 2 components and take the first one
.map(line -> line.split("=>")[0].trim());
}
}
/** * subst can fail if the drive to be mapped already exists. The method returns * a drive that is available.
*/ private Optional<Path> findAvailableDrive(Path tempDirectory) { for (char letter = 'Z'; letter >= 'A'; letter--) { try {
Path p = Path.of(letter + ":");
substCreate(p, tempDirectory);
substDelete(p); return Optional.of(p);
} catch (Throwable t) { // fall through
}
} return Optional.empty();
}
}
¤ Dauer der Verarbeitung: 0.3 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.