|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.translog.transfer; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.opensearch.test.OpenSearchTestCase; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | + |
| 18 | +public class FileSnapshotTests extends OpenSearchTestCase { |
| 19 | + |
| 20 | + FileSnapshot fileSnapshot; |
| 21 | + |
| 22 | + @After |
| 23 | + public void tearDown() throws Exception { |
| 24 | + super.tearDown(); |
| 25 | + fileSnapshot.close(); |
| 26 | + } |
| 27 | + |
| 28 | + public void testFileSnapshotPath() throws IOException { |
| 29 | + Path file = createTempFile(); |
| 30 | + Files.writeString(file, "hello"); |
| 31 | + fileSnapshot = new FileSnapshot.TransferFileSnapshot(file, 12); |
| 32 | + |
| 33 | + assertFileSnapshotProperties(file); |
| 34 | + |
| 35 | + try (FileSnapshot sameFileSnapshot = new FileSnapshot.TransferFileSnapshot(file, 12)) { |
| 36 | + assertEquals(sameFileSnapshot, fileSnapshot); |
| 37 | + } |
| 38 | + |
| 39 | + try (FileSnapshot sameFileDiffPTSnapshot = new FileSnapshot.TransferFileSnapshot(file, 34)) { |
| 40 | + assertNotEquals(sameFileDiffPTSnapshot, fileSnapshot); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public void testFileSnapshotContent() throws IOException { |
| 45 | + Path file = createTempFile(); |
| 46 | + Files.writeString(file, "hello"); |
| 47 | + fileSnapshot = new FileSnapshot.TransferFileSnapshot(file.getFileName().toString(), Files.readAllBytes(file), 23); |
| 48 | + |
| 49 | + assertFileSnapshotProperties(file); |
| 50 | + |
| 51 | + try ( |
| 52 | + FileSnapshot sameFileSnapshot = new FileSnapshot.TransferFileSnapshot( |
| 53 | + file.getFileName().toString(), |
| 54 | + Files.readAllBytes(file), |
| 55 | + 23 |
| 56 | + ) |
| 57 | + ) { |
| 58 | + assertEquals(sameFileSnapshot, fileSnapshot); |
| 59 | + } |
| 60 | + |
| 61 | + try ( |
| 62 | + FileSnapshot anotherFileSnapshot = new FileSnapshot.TransferFileSnapshot( |
| 63 | + file.getFileName().toString(), |
| 64 | + Files.readAllBytes(createTempFile()), |
| 65 | + 23 |
| 66 | + ) |
| 67 | + ) { |
| 68 | + assertNotEquals(anotherFileSnapshot, fileSnapshot); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void assertFileSnapshotProperties(Path file) throws IOException { |
| 73 | + assertEquals(file.getFileName().toString(), fileSnapshot.getName()); |
| 74 | + assertEquals(Files.size(file), fileSnapshot.getContentLength()); |
| 75 | + assertTrue(fileSnapshot.inputStream().markSupported()); |
| 76 | + } |
| 77 | +} |
0 commit comments