|
| 1 | +package com.spectralogic.ds3client.helpers; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.ByteBuffer; |
| 7 | +import java.nio.channels.SeekableByteChannel; |
| 8 | +import java.nio.charset.Charset; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.StandardOpenOption; |
| 12 | + |
| 13 | +import static org.hamcrest.CoreMatchers.is; |
| 14 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 15 | +import static org.junit.Assert.assertThat; |
| 16 | + |
| 17 | +public class FileObjectPutter_Test { |
| 18 | + |
| 19 | + final private static String testString = "This is some test data."; |
| 20 | + final private static byte[] testData = testString.getBytes(Charset.forName("UTF-8")); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testSymlink() throws IOException { |
| 24 | + final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_"); |
| 25 | + final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt"); |
| 26 | + |
| 27 | + try { |
| 28 | + try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
| 29 | + channel.write(ByteBuffer.wrap(testData)); |
| 30 | + } |
| 31 | + |
| 32 | + final Path symLinkPath = tempDir.resolve("sym_" + tempPath.getFileName().toString()); |
| 33 | + Files.createSymbolicLink(symLinkPath, tempPath); |
| 34 | + |
| 35 | + try { |
| 36 | + final FileObjectPutter putter = new FileObjectPutter(tempDir); |
| 37 | + |
| 38 | + final SeekableByteChannel newChannel = putter.buildChannel(symLinkPath.getFileName().toString()); |
| 39 | + assertThat(newChannel, is(notNullValue())); |
| 40 | + |
| 41 | + final ByteBuffer buff = ByteBuffer.allocate(testData.length); |
| 42 | + assertThat(newChannel.read(buff), is(testData.length)); |
| 43 | + |
| 44 | + assertThat(new String(buff.array(), Charset.forName("UTF-8")), is(testString)); |
| 45 | + } finally { |
| 46 | + Files.deleteIfExists(symLinkPath); |
| 47 | + } |
| 48 | + } finally { |
| 49 | + Files.deleteIfExists(tempPath); |
| 50 | + Files.deleteIfExists(tempDir); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testRegularFile() throws IOException { |
| 56 | + final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_"); |
| 57 | + final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt"); |
| 58 | + |
| 59 | + try { |
| 60 | + try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
| 61 | + channel.write(ByteBuffer.wrap(testData)); |
| 62 | + } |
| 63 | + final FileObjectPutter putter = new FileObjectPutter(tempDir); |
| 64 | + |
| 65 | + final SeekableByteChannel newChannel = putter.buildChannel(tempPath.getFileName().toString()); |
| 66 | + assertThat(newChannel, is(notNullValue())); |
| 67 | + |
| 68 | + final ByteBuffer buff = ByteBuffer.allocate(testData.length); |
| 69 | + assertThat(newChannel.read(buff), is(testData.length)); |
| 70 | + |
| 71 | + assertThat(new String(buff.array(), Charset.forName("UTF-8")), is(testString)); |
| 72 | + |
| 73 | + } finally { |
| 74 | + Files.deleteIfExists(tempPath); |
| 75 | + Files.deleteIfExists(tempDir); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments