found a bug, this is why we write tests
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f616b4b4bf
commit
45630b2813
|
@ -61,7 +61,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
|
||||||
|
|
||||||
public void removeAll() {
|
public void removeAll() {
|
||||||
|
|
||||||
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, true))) {
|
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, false))) {
|
||||||
bufferedWriter.write("");
|
bufferedWriter.write("");
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
|
|
|
@ -1,79 +1,66 @@
|
||||||
package persistence;
|
package persistence;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import persistence.category.CategoryEntity;
|
import persistence.category.CategoryEntity;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class GenericCSVDAOTest {
|
class GenericCSVDAOTest {
|
||||||
|
|
||||||
GenericCSVDAO<CategoryEntity> sut;
|
GenericCSVDAO<CategoryEntity> sut;
|
||||||
|
File file = new File("test.csv");
|
||||||
|
|
||||||
public void beforeEachDirty() throws IOException {
|
@BeforeEach
|
||||||
File file = new File("test.csv");
|
public void beforeEach() throws IOException {
|
||||||
|
|
||||||
if (file.exists()){
|
if (file.exists()) {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
|
|
||||||
this.sut = new GenericCSVDAO<CategoryEntity>(file,CategoryEntity::new);
|
this.sut = new GenericCSVDAO<>(file, CategoryEntity::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void afterEach() {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@Test
|
||||||
|
public void addWorks() throws IOException {
|
||||||
|
assertEquals(0, sut.getALl().size());
|
||||||
|
|
||||||
@Test
|
var entityToAdd = new CategoryEntity("categoryName", 99);
|
||||||
public void addWorks() throws IOException {
|
sut.add(entityToAdd);
|
||||||
|
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
|
||||||
|
|
||||||
beforeEachDirty();
|
assertEquals(entityToAdd, foundEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeWorks() throws IOException {
|
||||||
|
var entityToAdd = new CategoryEntity("categoryName", 99);
|
||||||
|
sut.add(entityToAdd);
|
||||||
|
|
||||||
assertEquals(0, sut.getALl().size());
|
sut.remove(entityToAdd);
|
||||||
|
|
||||||
var entityToAdd = new CategoryEntity("categoryName",99);
|
assertEquals(0, sut.getALl().size());
|
||||||
sut.add(entityToAdd);
|
}
|
||||||
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
|
|
||||||
|
|
||||||
assertEquals(entityToAdd,foundEntity);
|
@Test
|
||||||
|
public void removeAllWorks() throws IOException {
|
||||||
|
var entityToAdd1 = new CategoryEntity("categoryName1", 101);
|
||||||
|
sut.add(entityToAdd1);
|
||||||
|
var entityToAdd2 = new CategoryEntity("categoryName2", 102);
|
||||||
|
sut.add(entityToAdd2);
|
||||||
|
assertEquals(2, sut.getALl().size());
|
||||||
|
sut.removeAll();
|
||||||
|
|
||||||
|
assertEquals(0, sut.getALl().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void removeWorks() throws IOException {
|
|
||||||
|
|
||||||
beforeEachDirty();
|
|
||||||
|
|
||||||
|
|
||||||
var entityToAdd = new CategoryEntity("categoryName",99);
|
|
||||||
sut.add(entityToAdd);
|
|
||||||
|
|
||||||
sut.remove(entityToAdd);
|
|
||||||
|
|
||||||
assertEquals(0, sut.getALl().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void removeAllWorks() throws IOException {
|
|
||||||
|
|
||||||
beforeEachDirty();
|
|
||||||
|
|
||||||
|
|
||||||
var entityToAdd1 = new CategoryEntity("categoryName1",101);
|
|
||||||
sut.add(entityToAdd1);
|
|
||||||
var entityToAdd2 = new CategoryEntity("categoryName2",102);
|
|
||||||
sut.add(entityToAdd2);
|
|
||||||
assertEquals(2, sut.getALl().size());
|
|
||||||
sut.removeAll();
|
|
||||||
|
|
||||||
assertEquals(0, sut.getALl().size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue