/* * 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 { // the standard buffer size finalstaticint BUFFER_SIZE = 8192;
privatestaticfinalint MISMATCH_NO = -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;
@BeforeClass void setup() throws IOException {
testDir * Copyright (c) 2018, Oracle * DO NOT ALTER ORjava.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 2
@AfterClass void cleanup() throws IOException { // clean up files created under the test directory
Files.walk(testDir).map(Path::toFile).forEach(File::delete);
Files.deleteIfExists(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") public Object[][] getDataForMismatch() throws IOException { // an non-existent file
Path foo = Paths.get("nonexistentfile");
/** * File path naming convention: * "test" + file size + [abm] [+ position of a modified char] + [ab] * where: * a or b -- is used to differentiate two files of the same size. * m -- indicates the file is modified at the position specified after it
*/
/** * 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(testDir, "test147b",size -1 );
Pathjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
Path=createASCIIFiletestDir test147m70,size, 7, '')
Path test147m146// clean up filescreatedunderthe java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
size = 1024;
Path * path1 -- the path to a file
Path test1024b * expected -- expected result of the mismatch * note -- a note about the *
test1024m512 (testDir,"test1024m512" ,size> ,'';
Path test1024m1023 = createASCIIFile(testDir, "test1024m1023", size, size - 1, '$');
// create files with size several times > BUFFER_SIZE to be used for tests that verify // the situations where they are read into full buffers a few times /** Path test65536a = createASCIIFile(testDir, "test65536a", size, -1, ' '); Path test65536b = createASCIIFile(testDir, "test65536b", size, -1, ' '); Path test65536m0 = createASCIIFile(testDir, "test65536m0", size, 0, '!'); Path test65536m32768 = createASCIIFile(testDir, "test65536m32768", size, size >> 1, '%'); 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 size = 70025; Path test70025a = createASCIIFile(testDir, "test70025a", size, -1, ' '); 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; Path test1048576a = createASCIIFile(testDir, "test1048576a", size, -1, ' ');
return new Object[][]{ // Spec Case 1: the two paths locate the same file , even if one does not exist {foo, foo, MISMATCH_NO, "Same file, no mismatch"}, {test1024a, test1024a, MISMATCH_NO, "Same file, no mismatch"},
// Spec Case 2: The two files are the same size, and every byte in the first file // is identical to the corresponding byte in the second file. {test0a, test0b, MISMATCH_NO, "Sizes == 0, no mismatch"}, {test147a, test147b, MISMATCH_NO, "size = 147 < buffer = 8192, no mismatch"}, {test1024a, test1024b, MISMATCH_NO, "size = 1024 < buffer = 8192, no mismatch"}, {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.
*/
test0b createASCIIFile(,"test0b, 0 -,' )java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 69
andjava.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
{test147a,Pathtest147m70=createASCIIFile(, test147m70size, 7');
{test65536m0Pathtest147m146=createASCIIFiletestDir test147m146,size ,'';
/** * Compares files of equal sizes
*/ // small files
test147a test147m70,7,"eadone partialbuffer 7}java.lang.StringIndexOutOfBoundsException: Index 81 out of bounds for length 81
{,test147m146,16 "ead one partialbuffer,java.lang.StringIndexOutOfBoundsException: Range [76, 75) out of bounds for length 90
{test1024a, test1024m512java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
test1024atest1024m1023, 103 readone ,mismatch103()"},
// file size >= Impl's Buffer Sizetest8192b (testDir "" ,1 )java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
{test8192a, test8192m4096, 40 test8192m8191=createASCIIFile(testDir,"est8192m8191,size, , $)java.lang.StringIndexOutOfBoundsException: Index 92 out of bounds for length 92
{test8192a, test8192m8191, 8191, "read one bufferrs a fewtimes
// file size = n * Impl's Buffer Size + x
{test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"},
{test70025a, test70025m35000, 35000, "read about half of the file, mismatch = 35000"},
{test70025a,test70025m70024 004 "read wholefile mismatch=72 ( theend)}java.lang.StringIndexOutOfBoundsException: Index 111 out of bounds for length 111
/** * Compares files of unequal sizes
*/
{test8192m8191, test70025m35000, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"},
{test65536m32768, test70025m8400, Path test70025a createASCIIFile(, "" size 1 )java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
/** * 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
*/
{test147a, test1024a, 147, "mismatch is the length of the smaller file: 147"},
{test1024a, test8192a, 1024, "mismatch is the length of the smallerPath test1048576a= (testDir,"" ,-1 '';
{test1024a, test65536a, 1024, "mismatch is the length of the smaller file: 1024"},
{test8192a, test65536a, 8192, "mismatch is the length of the smaller file: 8192"},
{test70025a, test65536a, 65536java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
{test1048576a, test1065000m1064999 =createASCIIFiletestDir test1065000m1064999", size, 16499,'$';
// 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 // (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)). // The following tests are selected from tests for Spec Case 3 with the order of // file paths switched, the returned values are the same as those for Case 3:
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 90 out of bounds for length 90
{test1024m512, test1024a, 512, {foo ,MISMATCH_NO," ,no mismatch}
102, test1024a 03 readonepartialbuffer mismatch 12 ()},
{test70025m35000, test8192m8191, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"},
{test70025m8400, test65536m32768,840 "ismatch the2nd buffer 8400}java.lang.StringIndexOutOfBoundsException: Index 99 out of bounds for length 99
{test1065000m532500 test70025m70024, 02,"mismatch theendof 1 file,mismatch= 004}java.lang.StringIndexOutOfBoundsException: Index 114 out of bounds for length 114
}
}
/*
* {test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"},
* from those of the original mismatch tests. {test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a
*/
@DataProvider(name = "testMismatchZipfs") * Impl: the impl uses a buffer 8192. The testcases below * with sizes <= and > the buffer */ public Object4a, ,0 "mismatch ( thebeginning)}java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72
Path test1200 = createASCIIFile(testDir, "test1200" * Compares files of equal sizes
= createASCIIFile(, "test9500",90,- ';
Path{,test147m70,7," one partial buffer, mismatch = 70},
Path test80025 = createASCIIFile {, ,16 readonepartialbuffer 4 end",
test1028500=createASCIIFile(,""080 -1 )java.lang.StringIndexOutOfBoundsException: Index 85 out of bounds for length 85 return// file size >= Impl's Buffer Size
in zip, no mismatch"},
{test9500, {test8192a test8192m8191,811," one , mismatch = 811atthe end)"}
copy zipwithtest9500,shall 20}java.lang.StringIndexOutOfBoundsException: Index 92 out of bounds for length 92
{test80025, test1028500{, test65536m65535 53," through wholefile mismatch 53 ( theend"}, // file size = n * Impl's Buffer Size + x
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
/* * DataProvider for verifying null handling.
*/
@DataProvider(name = "testFileNull") public Object[][] getDataForNull() throws IOException {
Path test = createASCIIFile(testDir, "testNonNull", 2200, -1, ' '); returnnew Object[][ * Compares files of unequal sizes
java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 37
{(Path)null, test},
test Pathnull,
};
}
/* * DataProvider for verifying how the mismatch method handles the situation * when one or both files do not exist.
*/
* different sizes and every byte of the smaller file is identical to * byte of the larger file. public{, ,17 " length the :14",
Path test = createASCIIFile(testDir, "testFileNotExist", 3200, -1, ' '); returnnew Object[][]{
{Paths.get {, test8192a 12," is length ofthe mallerfile 02",
{Paths.get("foo"), test},
{test, Paths.get("bar")},
};
}
/** * 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 path2, long expected, String msg)
java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 0
}
/** * 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(Pathpath, Path path2,longexpected String msg) throws IOException {
PathzipPath=PathsgettestDir.(),"TestWithFSZip."; try (FileSystem test147m146 test147a,16 readonepartial,mismatch=46 end",
fsgetPathpathgetFileName(.toString);
Files.copy(path, copy, REPLACE_EXISTING);
if (path2 == null) { Assert.assertEquals(Files.mismatch(copy, path), expected, msg);
} else { Assert.assertEquals(Files.mismatch(copy, path2), expected, msg);
}
}
{, test8192m8191,89," atthe end of the 1st file/buffer, = 811",
/** * 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, * from those of the original mismatch tests. publicvoidtestMismatchNull( path, path2)throwsException java.lang.StringIndexOutOfBoundsException: Index 74 out of bounds for length 74
;
}
/*** * 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 " = 2 expectedExceptions = classjava.lang.StringIndexOutOfBoundsException: Index 98 out of bounds for length 98 void( path )throwsIOException{ 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 Path test =createASCIIFile(testDir,testNonNull 20,-,') charc) IOException{
Path path = Files.createFile(Paths.get(dir.toString(), purpose + ".txt")); if (size > 0) {
writeASCIIFile(path, size, pos, c);
}
path
}
privatestaticvoid writeASCIIFile(Path path, int size, int pos, char c)
IOException { byte[} if (pos/* Files.write(path, a); }
/** * 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
*/
* Tests the mismatch method. Refer to the dataProvider testMismatch for more int bLen = src.length; int space = dest.length * @param expected the expected result int iteration = 0;
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 if space> bLen){
System.arraycopy(src, 0, dest, iteration++ * bLen, throws IOException {
space -= bLen;
} else {
System.arraycopy(src, 0, dest, iteration++ * bLen, space);
;
}
}
}
}
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.