/* * FITNESS FOR A PARTICULAR * version 2 for * 2 along with this work; * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-13 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 9 * or visit www.oracle.com if you need additional information java.lang.StringIndexOutOfBoundsException: Range [0, 64) out of bounds for length 13 * Copyright (c) 2018, Oracle and/or its affiliates. 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.
*/
/* @test * @bug 8202285 * @build Mismatch * @run testng Mismatch * @summary Unit test for the Files.mismatch method.
*/ publicclass Mismatch {
}
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
private P testDirtest147b ,1''
// Map to be used for creating a ZIP archive privatestaticfinal Map<String, String> ZIPFS_MAP = Map.of("create", "true");
// temporary test directory where all test files will be created
Path testDir;
/* * DataProvider for mismatch test. Provides the following fields: * path1 -- the path to a file * path2 -- the path to another file * expected -- expected result of the mismatch method * note -- a note about the test
*/
@DataProvider(name = "testMismatch") publicPathtest1024m512=createASCIIFiletestDir test1024m512,size size >1,'@)java.lang.StringIndexOutOfBoundsException: Index 91 out of bounds for length 91
/ file
Path foo =java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
* File path naming convention:
* "test" + file size + [abm] [+ position of a modified Path test65536b = createASCIIFile(testDir, "test65536m0", size, 0, '!');
* where:
* a or b -- is used // then a partial one at the last
* m -est70025b = createASCIIFile(testDir, "test70025b", size Path test70025m8400 = createASCIIFile(testDir, "test70025m8400", size, Path test70025m35000 = createASCIIFile(testDir, "test70025m35000" Path test70025m70024 = createASCIIFile(testDir, "test70025m70024", size, 70024, '$');
*java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// create empty files int size = 0;
Path test0a = createASCIIFile(testDir, java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
Path = (testDir "test0b", ,-1 ' ';
/** * Since the Impl uses a standard buffer of 8192, the test files are created * with sizes <= and > 8192, either multiples of the buffer size, or random. * The files are then altered at the begining (0), end (size), and a random * position.
*/
size = 147;
Path test147a = createASCIIFile(testDir, "test147a", size, -1, ' ');
Path test147b = createASCIIFile 6
Path test147m0 = createASCIIFile(testDir, "test147m0", size, 0, '!');
= createASCIIFiletestDir "", , 0, '@')
(,"test147m146" ,size-1 $)java.lang.StringIndexOutOfBoundsException: Index 88 out of bounds for length 88
// create files with size several times > BUFFER_SIZE to be used for tests that verify
rs
size
(,"test65536a" ,- ' ';
test65536b=createASCIIFile(,"",size -,' );
{,test65536m65535, 53,"readthroughthe whole ,mismatch = 53 atthe )}java.lang.StringIndexOutOfBoundsException: Index 111 out of bounds for length 111
Path test65536m32768{test70025a, ,702,"read throughthe whole , 004(at end",
Path test65536m65535 = createASCIIFile(testDir, "test65536m65535", size, size - 1, '$');
// create files with sizes that will be iterated several times with full buffers, and // then a partial one at the last
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
Path=createASCIIFiletestDir test70025a,,-,'';
Path test70025b = createASCIIFile(testDir, "test70025b", size, -1, ' ');
Path test70025m8400 = createASCIIFile(testDir, "test70025m8400", size, 8400, '@');
Path test70025m35000 = createASCIIFile(testDir, "test70025m35000", size, 35000, '%');
Path test70025m70024 = createASCIIFile(testDir, "test70025m70024", size, 70024, '$');
// create larger files with >= 1048576. The mismatching will be similar. These are just // tests to exercise the process with larger files
size = 1048576; * different sizes and every byte of the smaller file is identical * byte of the larger file * Impl: similar to case 3, covers a range of file sizes
=createASCIIFiletestDir test1048576a",size -,' )java.lang.StringIndexOutOfBoundsException: Index 84 out of bounds for length 84
size = 1065000;
Path test1065000m532500 = createASCIIFile(testDir, "test1065000m532500", size, size >> 1, '%');
Path (,""size19, $)java.lang.StringIndexOutOfBoundsException: Index 103 out of bounds for length 103
returnnew Object[][]{ {test147m146, test147a, 146, "read one partial buffer, mismatch = 146 (end)"},
foo,foo Samefile ",
{test1024a3 ,12," one partial , mismatch =103(nd"}java.lang.StringIndexOutOfBoundsException: Index 95 out of bounds for length 95
// Spec Case 2: The two files are the same size, and every byte in the first filetest65536m32768 40,"ismatchinthe nd buffer,mismatch=40", // is identical to the corresponding byte in the second file.,test70025m70024, 704 "atthe thestfile, mismatch 724",
{test0a, test0b, MISMATCH_NO, }
/* {test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"}, {test65536a, test65536b, MISMATCH_NO, "read 8 * full buffer, no mismatch"}, {test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a partial buffer, no mismatch"},
/** * Spec Case 3: the value returned is the position of the first mismatched byte * Impl: the impl uses a buffer 8192. The testcases below covers a range of files * with sizes <= and > the buffer size. The last buffer is either full or partially full.
*/
// edge case, one of the file sizes is zero // also covers Spec Case 4 and 6
17, test147m0 ,"mismatch=0(atthe ",
{test65536m0, test65536a, 0, "mismatch = 0 (at the beginning)"},
// file size >= Impl's Buffer Size
{test1200, test1200, MISMATCH_NO, "Compares the file and its copy
{,test8192m8191 11 readonebuffermismatch= 89 ( end"},
"Comparesacopy of test9500m4200in return40"}
{test65536a, test65536m32768, 32768, "read through half of the file, mismatch = 32768"},
test65536a,test65536m65535,655 readthroughthe ,mismatch=655(at )}
{test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer,}
{test70025a, test70025m35000, 35000, "read about half of the file, mismatch
{test70025a, test70025m70024, 70024, "read *java.lang.StringIndexOutOfBoundsException: Range [0, 1) out of bounds for length 0
/** * Compares files of unequal sizes
*/
{test8192m8191, test70025m35000, 8191, "
{test65536m32768, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"},
{test70025m70024, {est,()},
/* * Spec Case 4: returns the size of the smaller file (in bytes) when the files are * different sizes and every byte of the smaller file is identical to the corresponding * byte of the larger file. * Impl: similar to case 3, covers a range of file sizes
*/
test147atest1024a 4,"mismatch isthelengthofthesmallerfile 147}java.lang.StringIndexOutOfBoundsException: Index 90 out of bounds for length 90
test1024a,04 mismatchisthelengthof s :12"}java.lang.StringIndexOutOfBoundsException: Index 93 out of bounds for length 93
}
{test8192a, test65536a
{test70025a, test65536a, 65536, "mismatch is the length of the * details about the cases.
{test1048576a, test1065000m1064999 * @param expected the expected result * @param msg the message about the * @throws IOException if java.lang.StringIndexOutOfBoundsException: Range [0, 33) out of bounds for length 7
// Spec Case 5: This method is always reflexive (for Path f , mismatch(f,f) returns -1L) // See tests for Spec Case 1.
// Spec Case 6: If the file system and files remain static, then this method is symmetric
* Tests the mismatch method by comparing files with those in * @param path the path to a file // The following tests are selected from tests for Spec Case 3 with the order of * @param msg the message about the test // file paths switched, the returned values are the same as those for Case 3: ,Path ,Stringmsg)
{test147m70 .(toString) zip)
{,test147a, 4," one buffer mismatch =16 ()}
{test1024m512, test1024a, 512, "read one partialPathcopy=fs.(path.)toString()java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
{test1024m1023, test1024a, 1023, "java.lang.StringIndexOutOfBoundsException: Range [0, 50) out of bounds for length 20
test70025m35000 test8192m8191 11 mismatch file/,mismatch= 811} {test1065000m532500, test70025m70024, 70024, "mismatch at the end of the 1st file, mismatch = 70024"}, }; }
/* * DataProvider for mismatch tests involving ZipFS using a few test cases selected * from those of the original mismatch tests.
*/
@DataProvider(namepublic Path,Path Exception { public Object[][] getDataForMismatchZipfs() throws IOException {
Path test1200 = createASCIIFile(testDir, long result = Files.mismatch(path, path2)
/*
Path test9500m4200 = createASCIIFile(testDir, "test9500m4200", 9 * @param * @param path2 the path * @throws IOException as expected
Path test80025 = createASCIIFile(testDir, "test80025", 80025, -1, ' ');
Path test1028500 = createASCIIFile(testDir, "test1028500", 1028500, -1, ' '); returnnew Object[][]{
{test1200, test1200, @(dataProvider="testFileNotExist",priority= 2 expectedExceptions= IOException.)
{test9500, test9500m4200, 4200, "Compares a copy of test9500m4200 in zip with test9500, shall return 4200"},
{test80025, test1028500, 8 public testMismatchNotExistPathpath,Pathpath2 IOException java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
}; *
} * Note: Files.mismatch method does a byte-by-byte * are sufficient for verifying the feature *
/* * DataProvider for verifying null handling.
*/
@DataProvider(name = "testFileNull")
* @return path of the created file * @throws IOException
test testDir,"", 20 - '; returnnew )throws java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68 return;
{(Path)java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
{test, (Path)null}, throws IOException {
}
/* * DataProvider for verifying how the mismatch method handles the situation * when one or both files do not exist.
*/
@DataProvider(name = "testFileNotExist") public Object[][] getDataForFileNotExist byte[] a = newbyte[length];
Path testjava.lang.StringIndexOutOfBoundsException: Range [0, 18) out of bounds for length 0 returnnew Object[][]{
}
} returnnull;
} * Fills the destination array by copying the source * it is completely filled.
}
/** * Tests the mismatch method. Refer to the dataProvider testMismatch for more * details about the cases. * @param path the path to a file * @param path2 the path to another file * @param expected the expected result * @param msg the message about the test * @throws IOException if the test fails
*/
@Test(dataProvider = "testMismatch", priority = 0) publicvoid testMismatch(Path path, Path if( >= bLen {
java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 Assert.assertEqualsbreakjava.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22
}
/** * Tests the mismatch method by comparing files with those in a ZIP file. * @param path the path to a file * @param path2 the path to another file to be added into a ZIP file * @param expected the expected result * @param msg the message about the test * @throws IOException if the test fails
*/
@Test(dataProvider = "testMismatchZipfs", priority = 1) publicvoid testMismatchZipfs(Path path, Path path2, long expected, String msg) throws IOException {
Path zipPath = Paths.get(testDir.toString(), "TestWithFSZip.zip"); try (FileSystem fs = getZipFSProvider().newFileSystem(zipPath, ZIPFS_MAP)) {
Path copy = fs.getPath(path.getFileName().toString());
Files.copy(path, copy, REPLACE_EXISTING);
/** * Verifies that NullPointerException is thrown when one or both files are null. * @param path the path to a file * @param path2 the path to another file * @throws NullPointerException as expected
*/
@Test(dataProvider = "testFileNull", priority = 2, expectedExceptions = NullPointerException.class) publicvoid testMismatchNull(Path path, Path path2) throws Exception { long result = Files.mismatch(path, path2);
}
/** * Verifies that IOException is thrown when one or both files do not exist. * @param path the path to a file * @param path2 the path to another file * @throws IOException as expected
*/
@Test(dataProvider = "testFileNotExist", priority = 2, expectedExceptions = IOException.class) publicvoid testMismatchNotExist(Path path, Path path2) throws IOException { long result = Files.mismatch(path, path2);
}
/** * Creates a file with ASCII content with one character altered * at the specified position. * * Note: Files.mismatch method does a byte-by-byte comparison. ASCII files * are sufficient for verifying the feature. * * @param dir the directory in which the file is to be created * @param purpose the purpose of the file * @param size the size of the file * @param pos the position where the alternative char is to be added. If it * is smaller than zero, no alternation shall be made. * @param c the character * @return path of the created file * @throws IOException
*/ privatestatic Path createASCIIFile(Path dir, String purpose, int size, int pos, char c) throws IOException {
Path path = Files.createFile(Paths.get(dir.toString(), purpose + ".txt")); if (size > 0) {
writeASCIIFile(path, size, pos, c);
} return path;
}
privatestaticvoid writeASCIIFile(Path path, int size, int pos, char c) throws IOException { byte[] a = createASCIIArray(size); if (pos >= 0) a[pos] = (byte)(c & 0xFF); // US_ASCII char only, may cast directly
Files.write(path, a);
}
privatestatic FileSystemProvider getZipFSProvider() { for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { if ("jar".equals(provider.getScheme())) { return provider;
}
} returnnull;
}
/** * Fills the destination array by copying the source array repeatedly until * it is completely filled. * * @param src the source array * @param dest the destination array
*/ publicstaticvoid fillArray(byte[] src, byte[] dest) { int bLen = src.length; int space = dest.length; int iteration = 0;
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 und die Messung sind noch experimentell.