Modify test methods

This commit is contained in:
DOHA 2014-09-29 01:12:17 +02:00
parent 800cf141cf
commit a06fead42f
1 changed files with 13 additions and 35 deletions

View File

@ -3,20 +3,16 @@ package test;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import main.Foo;
import junit.framework.TestCase; import junit.framework.TestCase;
public class TestWriter extends TestCase { public class TestWriter extends TestCase {
@ -24,7 +20,6 @@ public class TestWriter extends TestCase {
private String fileName = "test.txt"; private String fileName = "test.txt";
private String fileName1 = "test1.txt"; private String fileName1 = "test1.txt";
private String fileName2 = "test2.txt"; private String fileName2 = "test2.txt";
private String fileName3 = "test3.txt";
private String fileName4 = "test4.txt"; private String fileName4 = "test4.txt";
private String fileName5 = "test5.txt"; private String fileName5 = "test5.txt";
@ -40,14 +35,14 @@ public class TestWriter extends TestCase {
super.tearDown(); super.tearDown();
} }
public void testWriteFormattedStringUsingPrintWriter() throws IOException{ public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{
FileWriter fileWriter = new FileWriter(fileName); FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter); PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("Product name is %s and its price is %d $","iPhone",1000); printWriter.printf("Product name is %s and its price is %d $","iPhone",1000);
printWriter.close(); printWriter.close();
} }
public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() throws IOException { public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
String value = "Hello"; String value = "Hello";
FileOutputStream fos = new FileOutputStream(fileName1); FileOutputStream fos = new FileOutputStream(fileName1);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
@ -63,7 +58,7 @@ public class TestWriter extends TestCase {
assertEquals(value, result); assertEquals(value, result);
} }
public void testWriteToPosition_editValueIfSpecificPos_shouldChange() { public void whenWriteToPositionAndEditIt_thenItShouldChange() {
int data1 = 2014; int data1 = 2014;
int data2 = 1500; int data2 = 1500;
testWriteToPosition(data1, 4); testWriteToPosition(data1, 4);
@ -72,28 +67,18 @@ public class TestWriter extends TestCase {
assertEquals(data2, testReadFromPosition(4)); assertEquals(data2, testReadFromPosition(4));
} }
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() throws Exception {
Foo foo = new Foo(1, "John");
FileOutputStream fos = new FileOutputStream(fileName3);
ObjectOutputStream writer = new ObjectOutputStream(fos);
writer.writeObject(foo);
writer.close();
Foo result = null;
FileInputStream fis = new FileInputStream(fileName3); public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
ObjectInputStream reader = new ObjectInputStream(fis);
result = (Foo) reader.readObject();
reader.close();
assertEquals(foo.getId(), result.getId());
assertEquals(foo.getName(), result.getName());
}
public void testFileLock() throws IOException {
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
FileChannel channel = stream.getChannel(); FileChannel channel = stream.getChannel();
FileLock lock = channel.tryLock(); FileLock lock = null;
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
stream.close();
channel.close();
}
stream.writeChars("test lock"); stream.writeChars("test lock");
lock.release(); lock.release();
@ -102,15 +87,8 @@ public class TestWriter extends TestCase {
} }
public void testCreateFile_thenCheckIfItExists_thenDeleteAndCheckIfExist() throws IOException{
File file = new File("test_create.txt");
file.createNewFile();
assertTrue(file.exists());
file.delete();
assertFalse(file.exists());
}
public void testWriteStringUsingFileChannel() throws IOException{ public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
FileChannel channel = stream.getChannel(); FileChannel channel = stream.getChannel();
String value = "Hello"; String value = "Hello";