Last active
June 21, 2016 13:02
-
-
Save tomoTaka01/5f2933bba5317c6ba564cc082838a143 to your computer and use it in GitHub Desktop.
Unit test for files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardOpenOption; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Target1 { | |
private static final String CRLF = "\r\n"; | |
public void doSomething(Path path, List<Person> persons) { | |
final boolean needHeader = !Files.exists(path); | |
try (BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("sjis"), StandardOpenOption.CREATE, | |
StandardOpenOption.APPEND);) { | |
if (needHeader) { | |
writer.write(getHeaders()); | |
writer.write(CRLF); | |
} | |
persons.forEach(p -> { | |
String val = Arrays.stream(new String[] { p.name, String.valueOf(p.age) }) | |
.collect(Collectors.joining(",")); | |
try { | |
writer.write(val); | |
writer.write(CRLF); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private String getHeaders() { | |
return Arrays.asList("名前", "年齢").stream().collect(Collectors.joining(",")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Test; | |
public class Target1Test { | |
@Test | |
public void testDoSomething() { | |
Path path = Paths.get("/Users/tomo/tmp/test.csv"); | |
try { | |
Files.deleteIfExists(path); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
List<Person> persons = Arrays.asList(new Person("はなこ", 1)); | |
Target1 target = new Target1(); | |
target.doSomething(path, persons); | |
persons = Arrays.asList(new Person("たろう", 2)); | |
target.doSomething(path, persons); | |
try { | |
doCheck(path); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void doCheck(Path path) throws IOException { | |
List<String> lines = Files.readAllLines(path, Charset.forName("sjis")); | |
String lineHead = lines.get(0); | |
assertThat(lineHead, is("名前,年齢")); | |
String line1 = lines.get(1); | |
checkLine1(line1); | |
String line2 = lines.get(2); | |
checkLine2(line2); | |
} | |
private void checkLine1(String line1) { | |
String[] cols = line1.split(","); | |
assertThat(cols[0], is("はなこ")); | |
assertThat(cols[1], is("1")); | |
} | |
private void checkLine2(String line2) { | |
String[] cols = line2.split(","); | |
assertThat(cols[0], is("たろう")); | |
assertThat(cols[1], is("2")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment