Skip to content

Commit 37d4744

Browse files
committed
fix quality (coverage)
1 parent 50be660 commit 37d4744

File tree

6 files changed

+113
-18
lines changed

6 files changed

+113
-18
lines changed

table-inheritance/src/main/java/com/iluwatar/Main.java renamed to table-inheritance/src/main/java/com/iluwatar/table/inheritance/App.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package com.iluwatar;
1+
package com.iluwatar.table.inheritance;
22

33
/**
44
* The main entry point of the application demonstrating the use of vehicles.
55
*/
6-
public class Main {
7-
6+
public class App {
87
/**
98
* The main method to demonstrate adding and retrieving vehicles from the database.
109
*

table-inheritance/src/main/java/com/iluwatar/Car.java renamed to table-inheritance/src/main/java/com/iluwatar/table/inheritance/Car.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar;
1+
package com.iluwatar.table.inheritance;
22

33
/**
44
* Represents a car with a specific number of doors.
@@ -17,6 +17,9 @@ public class Car extends Vehicle {
1717
*/
1818
public Car(int year, String make, String model, int numDoors, int id) {
1919
super(year, make, model, id);
20+
if (numDoors <= 0) {
21+
throw new IllegalArgumentException("Number of doors must be positive.");
22+
}
2023
this.numDoors = numDoors;
2124
}
2225

@@ -26,6 +29,9 @@ public Car(int year, String make, String model, int numDoors, int id) {
2629
* @param doors the number of doors
2730
*/
2831
public void setNumDoors(int doors) {
32+
if (doors <= 0) {
33+
throw new IllegalArgumentException("Number of doors must be positive.");
34+
}
2935
this.numDoors = doors;
3036
}
3137

@@ -49,4 +55,3 @@ public String toString() {
4955
+ '}';
5056
}
5157
}
52-

table-inheritance/src/main/java/com/iluwatar/Truck.java renamed to table-inheritance/src/main/java/com/iluwatar/table/inheritance/Truck.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar;
1+
package com.iluwatar.table.inheritance;
22

33
/**
44
* Represents a truck, a type of vehicle with a specific load capacity.
@@ -17,7 +17,10 @@ public class Truck extends Vehicle {
1717
*/
1818
public Truck(int year, String make, String model, double loadCapacity, int id) {
1919
super(year, make, model, id);
20-
this.loadCapacity = loadCapacity; // Added spaces around '='
20+
if (loadCapacity <= 0) {
21+
throw new IllegalArgumentException("Load capacity must be positive.");
22+
}
23+
this.loadCapacity = loadCapacity;
2124
}
2225

2326
/**
@@ -26,7 +29,10 @@ public Truck(int year, String make, String model, double loadCapacity, int id) {
2629
* @param capacity the new load capacity
2730
*/
2831
public void setLoadCapacity(double capacity) {
29-
this.loadCapacity = capacity; // Added spaces around '='
32+
if (capacity <= 0) {
33+
throw new IllegalArgumentException("Load capacity must be positive.");
34+
}
35+
this.loadCapacity = capacity;
3036
}
3137

3238
/**
@@ -54,3 +60,4 @@ public String toString() {
5460
+ '}';
5561
}
5662
}
63+

table-inheritance/src/main/java/com/iluwatar/Vehicle.java renamed to table-inheritance/src/main/java/com/iluwatar/table/inheritance/Vehicle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar;
1+
package com.iluwatar.table.inheritance;
22

33
/**
44
* Represents a generic vehicle with basic attributes like make, model, year, and ID.

table-inheritance/src/main/java/com/iluwatar/VehicleDatabase.java renamed to table-inheritance/src/main/java/com/iluwatar/table/inheritance/VehicleDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.iluwatar;
1+
package com.iluwatar.table.inheritance;
22

33
import java.util.HashMap;
44
import java.util.Map;

table-inheritance/src/test/java/VehicleDatabaseTest.java

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import static org.junit.jupiter.api.Assertions.assertEquals;
22
import static org.junit.jupiter.api.Assertions.assertNotNull;
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
34

4-
import com.iluwatar.Car;
5-
import com.iluwatar.Truck;
6-
import com.iluwatar.Vehicle;
7-
import com.iluwatar.VehicleDatabase;
5+
import com.iluwatar.table.inheritance.Car;
6+
import com.iluwatar.table.inheritance.Truck;
7+
import com.iluwatar.table.inheritance.Vehicle;
8+
import com.iluwatar.table.inheritance.VehicleDatabase;
89
import org.junit.jupiter.api.BeforeEach;
910
import org.junit.jupiter.api.Test;
1011

1112
/**
1213
* Unit tests for the {@link VehicleDatabase} class.
1314
* Tests saving, retrieving, and printing vehicles of different types.
1415
*/
15-
public class VehicleDatabaseTest {
16+
class VehicleDatabaseTest {
1617

1718
private VehicleDatabase vehicleDatabase;
1819

@@ -28,7 +29,7 @@ public void setUp() {
2829
* Tests saving a {@link Car} to the database and retrieving it.
2930
*/
3031
@Test
31-
public void testSaveAndRetrieveCar() {
32+
void testSaveAndRetrieveCar() {
3233
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
3334
vehicleDatabase.saveVehicle(car);
3435

@@ -48,7 +49,7 @@ public void testSaveAndRetrieveCar() {
4849
* Tests saving a {@link Truck} to the database and retrieving it.
4950
*/
5051
@Test
51-
public void testSaveAndRetrieveTruck() {
52+
void testSaveAndRetrieveTruck() {
5253
Truck truck = new Truck(2018, "Ford", "F-150", 60, 2);
5354
vehicleDatabase.saveVehicle(truck);
5455

@@ -68,7 +69,7 @@ public void testSaveAndRetrieveTruck() {
6869
* Tests saving multiple vehicles to the database and printing them.
6970
*/
7071
@Test
71-
public void testPrintAllVehicles() {
72+
void testPrintAllVehicles() {
7273
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
7374
Truck truck = new Truck(2018, "Ford", "F-150", 60, 2);
7475
vehicleDatabase.saveVehicle(car);
@@ -82,4 +83,87 @@ public void testPrintAllVehicles() {
8283
assertNotNull(retrievedCar);
8384
assertNotNull(retrievedTruck);
8485
}
86+
87+
/**
88+
* Tests the constructor of {@link Car} with valid values.
89+
*/
90+
@Test
91+
void testCarConstructor() {
92+
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
93+
assertEquals(2020, car.getYear());
94+
assertEquals("Toyota", car.getMake());
95+
assertEquals("Corolla", car.getModel());
96+
assertEquals(4, car.getNumDoors());
97+
assertEquals(1, car.getId()); // Assuming the ID is auto-generated in the constructor
98+
}
99+
100+
/**
101+
* Tests the constructor of {@link Car} with invalid number of doors (negative value).
102+
*/
103+
@Test
104+
void testCarConstructorWithInvalidNumDoors() {
105+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
106+
new Car(2020, "Toyota", "Corolla", -4, 1);
107+
});
108+
assertEquals("Number of doors must be positive.", exception.getMessage());
109+
}
110+
111+
/**
112+
* Tests the constructor of {@link Car} with zero doors.
113+
*/
114+
@Test
115+
void testCarConstructorWithZeroDoors() {
116+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
117+
new Car(2020, "Toyota", "Corolla", 0, 1);
118+
});
119+
assertEquals("Number of doors must be positive.", exception.getMessage());
120+
}
121+
122+
/**
123+
* Tests the constructor of {@link Truck} with invalid load capacity (negative value).
124+
*/
125+
@Test
126+
void testTruckConstructorWithInvalidLoadCapacity() {
127+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
128+
new Truck(2018, "Ford", "F-150", -60, 2);
129+
});
130+
assertEquals("Load capacity must be positive.", exception.getMessage());
131+
}
132+
133+
/**
134+
* Tests the constructor of {@link Truck} with zero load capacity.
135+
*/
136+
@Test
137+
void testTruckConstructorWithZeroLoadCapacity() {
138+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
139+
new Truck(2018, "Ford", "F-150", 0, 2);
140+
});
141+
assertEquals("Load capacity must be positive.", exception.getMessage());
142+
}
143+
144+
/**
145+
* Tests setting invalid number of doors in {@link Car} using setter (negative value).
146+
*/
147+
@Test
148+
void testSetInvalidNumDoors() {
149+
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
150+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
151+
car.setNumDoors(-2);
152+
});
153+
assertEquals("Number of doors must be positive.", exception.getMessage());
154+
}
155+
156+
/**
157+
* Tests setting invalid load capacity in {@link Truck} using setter (negative value).
158+
*/
159+
@Test
160+
void testSetInvalidLoadCapacity() {
161+
Truck truck = new Truck(2018, "Ford", "F-150", 60, 2);
162+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
163+
truck.setLoadCapacity(-10);
164+
});
165+
assertEquals("Load capacity must be positive.", exception.getMessage());
166+
}
85167
}
168+
169+

0 commit comments

Comments
 (0)