testing work

This commit is contained in:
Eugen Paraschiv 2018-03-04 17:17:57 +02:00
parent 84a9b2e0d8
commit 535656eeb5
18 changed files with 73 additions and 97 deletions

View File

@ -1,26 +1,7 @@
*.class
0.* 0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests # Files generated by integration tests
*.txt *.txt
backup-pom.xml backup-pom.xml
/bin/ /bin/
/temp /temp
#IntelliJ specific
.idea/
*.iml

View File

@ -1,2 +0,0 @@
line 1
a second line

View File

@ -41,8 +41,8 @@ public class MappedByteBufferUnitTest {
@Test @Test
public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception { public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception {
// given // given
CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file"); final CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file");
Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt"); final Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt");
// when // when
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) { try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
@ -54,13 +54,15 @@ public class MappedByteBufferUnitTest {
} }
// then // then
List<String> fileContent = Files.readAllLines(pathToWrite); final List<String> fileContent = Files.readAllLines(pathToWrite);
assertEquals(fileContent.get(0), "This will be written to the file"); assertEquals(fileContent.get(0), "This will be written to the file");
} }
private Path getFileURIFromResources(String fileName) throws Exception { //
ClassLoader classLoader = getClass().getClassLoader();
private final Path getFileURIFromResources(String fileName) throws Exception {
final ClassLoader classLoader = getClass().getClassLoader();
return Paths.get(classLoader.getResource(fileName).toURI()); return Paths.get(classLoader.getResource(fileName).toURI());
} }
} }

View File

@ -1,47 +0,0 @@
package com.baeldung.stream;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class FileCopyTest {
@Test
public void whenUsingStream_thenCopyFile() throws IOException {
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt");
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt");
FileCopy.copyFileUsingStream(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingFiles_thenCopyFile() throws IOException {
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt");
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt");
FileCopy.copyFileUsingJavaFiles(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingChannel_thenCopyFile() throws IOException {
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt");
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt");
FileCopy.copyFileUsingChannel(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingApache_thenCopyFile() throws IOException {
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt");
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt");
FileCopy.copyFileUsingApacheCommonsIO(src, dest);
assertTrue(dest.exists());
dest.delete();
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.stream;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class FileCopyUnitTest {
@Test
public void whenUsingStream_thenCopyFile() throws IOException {
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt");
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt");
FileCopy.copyFileUsingStream(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingFiles_thenCopyFile() throws IOException {
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt");
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt");
FileCopy.copyFileUsingJavaFiles(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingChannel_thenCopyFile() throws IOException {
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt");
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt");
FileCopy.copyFileUsingChannel(src, dest);
assertTrue(dest.exists());
dest.delete();
}
@Test
public void whenUsingApache_thenCopyFile() throws IOException {
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt");
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt");
FileCopy.copyFileUsingApacheCommonsIO(src, dest);
assertTrue(dest.exists());
dest.delete();
}
}

View File

@ -152,11 +152,11 @@ public class JavaInputStreamToXUnitTest {
@Test @Test
public final void whenConvertingToFile_thenCorrect() throws IOException { public final void whenConvertingToFile_thenCorrect() throws IOException {
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
final byte[] buffer = new byte[initialStream.available()]; final byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer); initialStream.read(buffer);
final File targetFile = new File("src/main/resources/targetFile.tmp"); final File targetFile = new File("src/test/resources/targetFile.tmp");
final OutputStream outStream = new FileOutputStream(targetFile); final OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer); outStream.write(buffer);
@ -166,8 +166,8 @@ public class JavaInputStreamToXUnitTest {
@Test @Test
public final void whenConvertingInProgressToFile_thenCorrect() throws IOException { public final void whenConvertingInProgressToFile_thenCorrect() throws IOException {
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
final File targetFile = new File("src/main/resources/targetFile.tmp"); final File targetFile = new File("src/test/resources/targetFile.tmp");
final OutputStream outStream = new FileOutputStream(targetFile); final OutputStream outStream = new FileOutputStream(targetFile);
final byte[] buffer = new byte[8 * 1024]; final byte[] buffer = new byte[8 * 1024];
@ -182,8 +182,8 @@ public class JavaInputStreamToXUnitTest {
@Test @Test
public final void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException { public final void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException {
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
final File targetFile = new File("src/main/resources/targetFile.tmp"); final File targetFile = new File("src/test/resources/targetFile.tmp");
java.nio.file.Files.copy(initialStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); java.nio.file.Files.copy(initialStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
@ -192,11 +192,11 @@ public class JavaInputStreamToXUnitTest {
@Test @Test
public final void whenConvertingInputStreamToFile_thenCorrect3() throws IOException { public final void whenConvertingInputStreamToFile_thenCorrect3() throws IOException {
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt")); final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
final byte[] buffer = new byte[initialStream.available()]; final byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer); initialStream.read(buffer);
final File targetFile = new File("src/main/resources/targetFile.tmp"); final File targetFile = new File("src/test/resources/targetFile.tmp");
Files.write(buffer, targetFile); Files.write(buffer, targetFile);
IOUtils.closeQuietly(initialStream); IOUtils.closeQuietly(initialStream);
@ -204,9 +204,9 @@ public class JavaInputStreamToXUnitTest {
@Test @Test
public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException { public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException {
final InputStream initialStream = FileUtils.openInputStream(new File("src/main/resources/sample.txt")); final InputStream initialStream = FileUtils.openInputStream(new File("src/test/resources/sample.txt"));
final File targetFile = new File("src/main/resources/targetFile.tmp"); final File targetFile = new File("src/test/resources/targetFile.tmp");
FileUtils.copyInputStreamToFile(initialStream, targetFile); FileUtils.copyInputStreamToFile(initialStream, targetFile);
} }

View File

@ -68,7 +68,7 @@ public class JavaXToInputStreamUnitTest {
@Test @Test
public final void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect() throws IOException { public final void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect() throws IOException {
final File initialFile = new File("src/main/resources/sample.txt"); final File initialFile = new File("src/test/resources/sample.txt");
final InputStream targetStream = new FileInputStream(initialFile); final InputStream targetStream = new FileInputStream(initialFile);
IOUtils.closeQuietly(targetStream); IOUtils.closeQuietly(targetStream);
@ -76,7 +76,7 @@ public class JavaXToInputStreamUnitTest {
@Test @Test
public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException { public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException {
final File initialFile = new File("src/main/resources/sample.txt"); final File initialFile = new File("src/test/resources/sample.txt");
final InputStream targetStream = Files.asByteSource(initialFile).openStream(); final InputStream targetStream = Files.asByteSource(initialFile).openStream();
IOUtils.closeQuietly(targetStream); IOUtils.closeQuietly(targetStream);
@ -84,7 +84,7 @@ public class JavaXToInputStreamUnitTest {
@Test @Test
public final void givenUsingCommonsIO_whenConvertingFileToInputStream_thenCorrect() throws IOException { public final void givenUsingCommonsIO_whenConvertingFileToInputStream_thenCorrect() throws IOException {
final File initialFile = new File("src/main/resources/sample.txt"); final File initialFile = new File("src/test/resources/sample.txt");
final InputStream targetStream = FileUtils.openInputStream(initialFile); final InputStream targetStream = FileUtils.openInputStream(initialFile);
IOUtils.closeQuietly(targetStream); IOUtils.closeQuietly(targetStream);

View File

@ -0,0 +1 @@
Hello World

View File

@ -1,2 +0,0 @@
line 1
a second line

View File

@ -1,2 +0,0 @@
files will be copied here and then deleted
remove `file.delete()` to see the files here

View File

@ -1 +0,0 @@
This is a content of the file

View File

@ -1 +0,0 @@
Hello world

View File

@ -1 +0,0 @@
Hello world !