Skip to content

Instantly share code, notes, and snippets.

@Lin-Ya
Created January 18, 2019 03:34
Show Gist options
  • Save Lin-Ya/89c9818e8f178836904df246c774f2fc to your computer and use it in GitHub Desktop.
Save Lin-Ya/89c9818e8f178836904df246c774f2fc to your computer and use it in GitHub Desktop.
Task 21
package com.orrz.unittest.model;
import java.io.*;
public class File {
private String path;
private BufferedReader reader;
private OutputStream out;
public File(String path) {
this.path = path;
}
public String getPath() {
return path;
}
public void setReader(BufferedReader reader) {
this.reader = reader;
}
public void setOut(OutputStream out) {
this.out = out;
}
public String readToString(BufferedReader bufferedReader) throws IOException {
reader = bufferedReader;
String content = "";
String line;
line = reader.readLine();
content += line;
return content;
}
public void writeString(String content) throws IOException {
out.write(content.getBytes());
out.close();
}
}
package com.orrz.unittest.model;
import java.io.*;
public class FileHelper {
private BufferedReader file1Reader = null;
private String source = null;
public void setFile1Reader(BufferedReader file1Reader) {
this.file1Reader = file1Reader;
}
public void copy(File file1, File file2) {
try {
source = file1.readToString(file1Reader);
} catch (IOException e) {
e.printStackTrace();
}
if (source != null) {
try {
file2.writeString(source);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.orrz.unittest.model;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
public class FileHelperTest extends EasyMockSupport {
private File file1;
private File file2;
private FileHelper fileHelper;
private BufferedReader readerA;
@Before
public void setup() {
file1 = new File("pathA");
file2 = new File("pathB");
readerA = createStrictMock(BufferedReader.class);
}
@Test
public void shouldCopy() throws IOException {
expect(readerA.readLine()).andReturn("CopyTest");
file1.setReader(readerA);
ByteArrayOutputStream outputB = new ByteArrayOutputStream();
file2.setOut(outputB);
replayAll();
fileHelper = new FileHelper();
fileHelper.setFile1Reader(readerA);
fileHelper.copy(file1, file2);
verifyAll();
String result = new String(outputB.toByteArray());
assertEquals("CopyTest", result);
}
}
package com.orrz.unittest.model;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
public class FileTest extends EasyMockSupport {
private File file;
private BufferedReader reader;
@Before
public void steup() throws IOException {
reader = createStrictMock(BufferedReader.class);
file = new File("resources/1.txt");
}
@Test
public void shouldReadToString() throws IOException {
expect(reader.readLine()).andReturn("test");
replayAll();
String result = file.readToString(reader);
verifyAll();
assertEquals("test", result);
}
@Test
public void shouldWrite() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
file.setOut(output);
file.writeString("test");
String result = new String(output.toByteArray());
assertEquals("test", result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment