Skip to content

Commit

Permalink
change test files location
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Sep 29, 2014
1 parent 3737584 commit 2bdfde7
Showing 1 changed file with 91 additions and 109 deletions.
200 changes: 91 additions & 109 deletions core-java/src/test/java/org/baeldung/java/io/TestWriter.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package test;
package org.baeldung.java.io;

import static org.junit.Assert.assertEquals;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
Expand All @@ -21,178 +23,158 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import junit.framework.TestCase;

public class TestWriter extends TestCase {
import org.junit.Test;

private String fileName = "test.txt";
private String fileName1 = "test1.txt";
private String fileName2 = "test2.txt";
private String fileName3 = "test3.txt";
private String fileName4 = "test4.txt";
private String fileName5 = "test5.txt";

public TestWriter(String name) {
super(name);
}
public class TestWriter {

protected void setUp() throws Exception {
super.setUp();
}
private String fileName = "src/test/resources/test_write.txt";
private String fileName1 = "src/test/resources/test_write_1.txt";
private String fileName2 = "src/test/resources/test_write_2.txt";
private String fileName3 = "src/test/resources/test_write_3.txt";
private String fileName4 = "src/test/resources/test_write_4.txt";
private String fileName5 = "src/test/resources/test_write_5.txt";

protected void tearDown() throws Exception {
super.tearDown();
}


public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException{
String str = "Hello";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
@Test
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
final String str = "Hello";
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
writer.write(str);
writer.close();
}

public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException{
String str = "World";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3 , true));

@Test
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
final String str = "World";
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true));
writer.append(' ');
writer.append(str);
writer.close();
}

public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("Product name is %s and its price is %d $","iPhone",1000);

@Test
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException {
final FileWriter fileWriter = new FileWriter(fileName);
final PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
printWriter.close();
}

public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException{
String str = "Hello";
FileOutputStream outputStream = new FileOutputStream(fileName3);
byte[] strToBytes = str.getBytes();

@Test
public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException {
final String str = "Hello";
final FileOutputStream outputStream = new FileOutputStream(fileName3);
final byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}

@Test
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
String value = "Hello";
FileOutputStream fos = new FileOutputStream(fileName1);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
final String value = "Hello";
final FileOutputStream fos = new FileOutputStream(fileName1);
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(value);
outStream.close();

String result;
FileInputStream fis = new FileInputStream(fileName1);
DataInputStream reader = new DataInputStream(fis);
final FileInputStream fis = new FileInputStream(fileName1);
final DataInputStream reader = new DataInputStream(fis);
result = reader.readUTF();
reader.close();

assertEquals(value, result);
}

public void whenWriteToPositionAndEditIt_thenItShouldChange() {
int data1 = 2014;
int data2 = 1500;
@Test
public void whenWriteToPositionAndEditIt_thenItShouldChange() throws IOException {
final int data1 = 2014;
final int data2 = 1500;
testWriteToPosition(data1, 4);
assertEquals(data1, testReadFromPosition(4));
testWriteToPosition(data2, 4);
assertEquals(data2, testReadFromPosition(4));
}


@Test
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
FileChannel channel = stream.getChannel();
final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
final FileChannel channel = stream.getChannel();

FileLock lock = null;
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
} catch (final OverlappingFileLockException e) {
stream.close();
channel.close();
}
stream.writeChars("test lock");
lock.release();

stream.close();
channel.close();
}



public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
FileChannel channel = stream.getChannel();
String value = "Hello";
byte[] strBytes = value.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
@Test
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException {
final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
final FileChannel channel = stream.getChannel();
final String value = "Hello";
final byte[] strBytes = value.getBytes();
final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
buffer.put(strBytes);
buffer.flip();
channel.write(buffer);
channel.write(buffer);
stream.close();
channel.close();
RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
assertEquals(value , reader.readLine());

final RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
assertEquals(value, reader.readLine());
reader.close();

}

public void whenWriteToTmpFile_thenCorrect() throws IOException{
String toWrite = "Hello";
File tmpFile = File.createTempFile("test", ".tmp");
FileWriter writer = new FileWriter(tmpFile);

@Test
public void whenWriteToTmpFile_thenCorrect() throws IOException {
final String toWrite = "Hello";
final File tmpFile = File.createTempFile("test", ".tmp");
final FileWriter writer = new FileWriter(tmpFile);
writer.write(toWrite);
writer.close();
BufferedReader reader = new BufferedReader(new FileReader(tmpFile));

final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
assertEquals(toWrite, reader.readLine());
reader.close();
}

public void whenWriteUsingJava7_thenCorrect() throws IOException{
String str = "Hello";

Path path = Paths.get(fileName3);
byte[] strToBytes = str.getBytes();


@Test
public void whenWriteUsingJava7_thenCorrect() throws IOException {
final String str = "Hello";

final Path path = Paths.get(fileName3);
final byte[] strToBytes = str.getBytes();

Files.write(path, strToBytes);
String read = Files.readAllLines(path).get(0);

final String read = Files.readAllLines(path).get(0);
assertEquals(str, read);
}

// use RandomAccessFile to write data at specific position in the file
public void testWriteToPosition(int data, long position) {
try {
RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
writer.seek(position);
writer.writeInt(data);
writer.close();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
private void testWriteToPosition(final int data, final long position) throws IOException {
final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
writer.seek(position);
writer.writeInt(data);
writer.close();
}



// use RandomAccessFile to read data from specific position in the file
public int testReadFromPosition(long position) {
private int testReadFromPosition(final long position) throws IOException {
int result = 0;
try {
RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
reader.seek(position);
result = reader.readInt();
reader.close();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
final RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
reader.seek(position);
result = reader.readInt();
reader.close();
return result;
}





}
}

0 comments on commit 2bdfde7

Please sign in to comment.