test Write
This commit is contained in:
parent
158f566e64
commit
c2e2483973
@ -27,8 +27,12 @@ public class Read {
|
||||
boolean saved = Overwrite.checkIfPeselExists(file, personalInfo);
|
||||
|
||||
if(!saved) {
|
||||
Write.write(file, personalInfo);
|
||||
System.out.println("Osoba dodana do listy.");
|
||||
if(Write.write(file, personalInfo)){
|
||||
System.out.println("Osoba dodana do listy.");
|
||||
}
|
||||
else {
|
||||
System.out.println("Problem z zapisem osoby do listy.");
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.print("Chcesz kontynuować? [t/n] ");
|
||||
|
@ -1,17 +1,20 @@
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
public class Write {
|
||||
public static void write(File file, List data) {
|
||||
public static boolean write(File file, List data) {
|
||||
if(file.exists()) {
|
||||
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, true))){
|
||||
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, StandardCharsets.UTF_8, true))){
|
||||
fileWriter.write(data.get(0) + " " + data.get(1) + " " + data.get(2) + " " + data.get(3) + "\n");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("Problem z zapisem do pliku.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
39
src/test/java/WriteTest.java
Normal file
39
src/test/java/WriteTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class WriteTest {
|
||||
protected File file;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy HH-mm-ss");
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String fileName = now.format(dtf) + " lista.txt";
|
||||
file = new File(fileName);
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeTest() {
|
||||
List<String> person = new ArrayList<String>();
|
||||
person.add("Gniezno");
|
||||
person.add("Jan");
|
||||
person.add("Kowalski");
|
||||
person.add("89042836312");
|
||||
|
||||
assertTrue(Write.write(file, person));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user