Commit 77d02396 authored by CJGutz's avatar CJGutz
Browse files

enhance: divide up file handler tests and one more test for unit

parent 6ee0553d
Pipeline #183919 passed with stages
in 38 seconds
......@@ -25,7 +25,6 @@ public class ArmyFileHandlerTest {
public Path tempDir;
Army army;
ArmyFileHandler fileHandler;
@BeforeEach
public void initializeValidArmyWithSeveralUnitsOfAllClasses() {
......@@ -41,13 +40,18 @@ public class ArmyFileHandlerTest {
}
army.addUnits(units);
fileHandler = new ArmyFileHandler();
}
@Nested
public class Write_army_to_file {
ArmyFileHandler fileHandler;
@BeforeEach
public void initilizeFileHandler() {
fileHandler = new ArmyFileHandler();
}
@Test
public void writing_to_file_does_not_cause_error_and_file_path_is_valid() {
Path filePath = tempDir.resolve("valid-name.csv");
......@@ -65,7 +69,7 @@ public class ArmyFileHandlerTest {
@Test
public void file_extension_exception_thrown_if_file_is_not_csv() {
String filePath = tempDir.resolve("unvalid-name.csv.cs").toString();
String filePath = tempDir.resolve("invalid-name.csv.cs").toString();
assertThrows(FileExtensionException.class, () -> fileHandler.writeToFile(army, filePath));
}
......@@ -90,13 +94,52 @@ public class ArmyFileHandlerTest {
}
}
@Nested
public class Read_valid_army_values_from_file {
String filePath;
@BeforeEach
public void createFileInTempDirectory() {
filePath = tempDir.resolve("valid-name.csv").toString();
}
@Test
public void file_handler_reads_all_units_when_values_are_separated_by_comma_and_space() {
ArmyFileHandler fileHandler = new ArmyFileHandler();
try (FileWriter fileWriter = new FileWriter(filePath, true)) {
fileWriter.write("Name Of Army \n");
fileWriter.write(" InfantryUnit , infantry , 10 \n");
fileWriter.write(" InfantryUnit , infantry , 10 \n");
fileWriter.write(" RangedUnit , ranged , 10 \n");
fileWriter.write(" RangedUnit , ranged , 10 \n");
} catch (IOException e) {
fail();
}
try {
Army armyFromFile = fileHandler.readArmyFromFile(filePath);
assertEquals(4, armyFromFile.getAllUnits().size());
assertEquals(2, armyFromFile.getInfantryUnits().size());
assertEquals(2, armyFromFile.getRangedUnits().size());
assertEquals(0, fileHandler.getReadLinesSkipped().size());
} catch (IOException e) {
fail();
}
}
}
@Nested
public class Read_invalid_army_values_from_file {
String filePath;
@BeforeEach
public void write_valid_army_to_file() {
public void writeArmyWithFortyUnitsToFile() {
ArmyFileHandler fileHandler = new ArmyFileHandler();
filePath = tempDir.resolve("valid-name.csv").toString();
try {
......@@ -107,7 +150,7 @@ public class ArmyFileHandlerTest {
}
@Test
public void file_handler_reads_forty_one_units_from_file_when_several_extra_lines_are_not_valid_units_but_one_is() {
public void file_handler_reads_forty_one_units_from_file_when_units_do_not_have_health_as_int_but_one_has() {
ArmyFileHandler fileHandler = new ArmyFileHandler();
int unitSize = army.getAllUnits().size();
......@@ -116,9 +159,34 @@ public class ArmyFileHandlerTest {
try (FileWriter fileWriter = new FileWriter(filePath, true)) {
fileWriter.write("InfantryUnit,unit,2.0\n");
fileWriter.write("InfantryUnit,unit,NaN\n");
fileWriter.write("RangedUnit,infantry,10\n");
fileWriter.write("CavalryUnit,infantry,-2\n");
fileWriter.write("RangedUnit,ranged,10\n");
} catch (IOException e) {
fail();
}
try {
Army armyFromFile = fileHandler.readArmyFromFile(filePath);
assertEquals(unitSize + 1, armyFromFile.getAllUnits().size());
assertEquals(rangedUnitSize + 1, armyFromFile.getRangedUnits().size());
assertEquals(3, fileHandler.getReadLinesSkipped().size());
} catch (IOException e) {
fail();
}
}
@Test
public void file_handler_reads_forty_one_units_from_file_when_lines_are_not_units_but_one_is() {
ArmyFileHandler fileHandler = new ArmyFileHandler();
int unitSize = army.getAllUnits().size();
int rangedUnitSize = army.getRangedUnits().size();
try (FileWriter fileWriter = new FileWriter(filePath, true)) {
fileWriter.write("Unit,infantry,10\n");
fileWriter.write("rangedunit,ranged,10\n");
fileWriter.write("RangedUnit,ranged,10\n");
} catch (IOException e) {
fail();
}
......@@ -128,7 +196,7 @@ public class ArmyFileHandlerTest {
assertEquals(unitSize + 1, armyFromFile.getAllUnits().size());
assertEquals(rangedUnitSize + 1, armyFromFile.getRangedUnits().size());
assertEquals(4, fileHandler.getReadLinesSkipped().size());
assertEquals(2, fileHandler.getReadLinesSkipped().size());
} catch (IOException e) {
fail();
}
......
......@@ -37,16 +37,16 @@ public class UnitFactoryTest {
@Test
public void unrecognized_unit_type_throws_exception_for_one_unit() {
UnitType unvalidUnitType = UnitType.getUnitType("NotAType");
UnitType invalidUnitType = UnitType.getUnitType("NotAType");
assertThrows(IllegalArgumentException.class, () -> factory.createUnit(unvalidUnitType, validName, validHealth));
assertThrows(IllegalArgumentException.class, () -> factory.createUnit(invalidUnitType, validName, validHealth));
}
@Test
public void unrecognized_unit_type_throws_exception_for_multiple_units() {
UnitType unvalidUnitType = UnitType.getUnitType("NotAType");
UnitType invalidUnitType = UnitType.getUnitType("NotAType");
assertThrows(IllegalArgumentException.class, () -> factory.createListOfUnits(10, unvalidUnitType, validName, validHealth));
assertThrows(IllegalArgumentException.class, () -> factory.createListOfUnits(10, invalidUnitType, validName, validHealth));
}
@Test
......
package edu.ntnu.idatt2001.carljgu.units;
import edu.ntnu.idatt2001.carljgu.battle.units.UnitType;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class UnitTypeTest {
@Test
......@@ -14,6 +17,10 @@ public class UnitTypeTest {
assertEquals(UnitType.RANGED_UNIT, UnitType.getUnitType("RangedUnit"));
assertEquals(UnitType.CAVALRY_UNIT, UnitType.getUnitType("CavalryUnit"));
assertEquals(UnitType.COMMANDER_UNIT, UnitType.getUnitType("CommanderUnit"));
}
@Test
public void null_object_is_returned_if_string_is_unrecognized() {
assertNull(UnitType.getUnitType("Not A Unit"));
}
......
......@@ -15,25 +15,37 @@ import org.junit.jupiter.api.Test;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class WarUnitTest {
private Unit unit;
@Nested
public class Unit_constructor_throws_exception {
public class Unit_constructor_throws_exception_for_unit_name {
@Test
public void exception_for_name_thrown_if_name_only_contains_whitespace() {
try {
unit = new InfantryUnit(" ", 1);
new InfantryUnit(" ", 1);
fail();
} catch (IllegalArgumentException exception) {
assertEquals(exception.getMessage(), "Name can not be empty");
}
}
@Test
public void exception_is_thrown_if_name_contains_comma() {
try {
new InfantryUnit("Not, a valid name", 1);
fail();
} catch (IllegalArgumentException exception) {
assertEquals(exception.getMessage(), "Name can not contain comma");
}
}
}
@Nested
public class Unit_constructor_throws_exception_for_invalid_numbers {
@Test
public void exception_for_health_thrown_if_health_is_less_than_or_equal_to_zero() {
try {
unit = new InfantryUnit("Valid name", -1);
new InfantryUnit("Valid name", -1);
fail();
} catch (IllegalArgumentException exception) {
assertEquals(exception.getMessage(), "Health can not start as less than or equal to 0");
......@@ -43,7 +55,7 @@ public class WarUnitTest {
@Test
public void exception_is_thrown_if_attack_is_negative() {
try {
unit = new InfantryUnit("Valid name", 1, -1, 1);
new InfantryUnit("Valid name", 1, -1, 1);
fail();
} catch (IllegalArgumentException exception) {
assertEquals(exception.getMessage(), "Attack can not be negative");
......@@ -53,7 +65,7 @@ public class WarUnitTest {
@Test
public void exception_is_thrown_if_armor_is_negative() {
try {
unit = new InfantryUnit("Valid name", 1, 1, -1);
new InfantryUnit("Valid name", 1, 1, -1);
fail();
} catch (IllegalArgumentException exception) {
assertEquals(exception.getMessage(), "Armor can not be negative");
......@@ -63,7 +75,7 @@ public class WarUnitTest {
@Test
public void exception_is_not_thrown_if_attack_or_armor_is_zero() {
try {
unit = new InfantryUnit("Valid name", 1, 0, 0);
new InfantryUnit("Valid name", 1, 0, 0);
assertTrue(true);
} catch (IllegalArgumentException exception) {
fail();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment