Skip to content

Commit f2a409c

Browse files
committed
even more test cases
1 parent 804d0e3 commit f2a409c

File tree

6 files changed

+182
-32
lines changed

6 files changed

+182
-32
lines changed

entity-component-system/src/main/java/com/iluwatar/App.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,12 @@
2424
*/
2525
package com.iluwatar;
2626

27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
29-
3027
/**
3128
* The main entry point for the application.
3229
* This class simulates a game loop where entities are created, updated, and their states are modified.
3330
*/
3431
public class App {
3532

36-
// Create a logger instance
37-
public static final Logger logger = LoggerFactory.getLogger(App.class);
38-
3933
/**
4034
* The main method that runs the application.
4135
* It creates entities, adds components, updates them over several frames,
@@ -44,68 +38,44 @@ public class App {
4438
* @param args Command-line arguments (not used in this application)
4539
*/
4640
public static void main(String[] args) {
41+
System.out.print("Hello, World!");
4742

4843
Entity entity1 = new Entity("Entity1");
4944
Entity entity2 = new Entity("Entity2");
5045

51-
// Set up some transform components (position, rotation, scale)
5246
TransformComponent transform1 = new TransformComponent(new float[]{0.0f, 0.0f, 0.0f},
5347
new float[]{0.0f, 0.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});
5448
TransformComponent transform2 = new TransformComponent(new float[]{5.0f, 0.0f, 0.0f},
5549
new float[]{0.0f, 45.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});
5650

57-
// Set the transform components for each entity
5851
entity1.addComponent(transform1);
5952
entity2.addComponent(transform2);
6053

61-
// Create a health component for entity1 with initial health of 100
6254
HealthComponent health1 = new HealthComponent(100); // Ensure HealthComponent is implemented
6355
entity1.addComponent(health1);
6456

65-
// Create a velocity component for entity1
6657
VelocityComponent velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f);
6758
entity1.addComponent(velocity1);
6859

69-
// Set up a system and add entities to the system
7060
GameSystem gameSystem = new GameSystem();
7161
gameSystem.addEntity(entity1);
7262
gameSystem.addEntity(entity2);
7363

74-
// Simulate game update loop (e.g., 60 FPS)
75-
float deltaTime = 1.0f / 60.0f; // 60 FPS
64+
float deltaTime = 1.0f / 60.0f;
7665

77-
// Simulate for a few frames
7866
for (int i = 0; i < 10; i++) {
79-
logger.info("Frame: {}", i + 1);
8067

81-
// Update all entities in the system
8268
gameSystem.update(deltaTime);
8369

84-
// Apply some damage to entity1's health component at frame 6
8570
if (i == 5) {
8671
health1.applyDamage(30);
87-
logger.info("Entity1's health after damage: {}", health1.getCurrentHealth());
8872
}
8973

90-
// Apply some force to entity1's velocity at frame 3
9174
if (i == 3) {
9275
velocity1.applyForce(0.5f, 0.0f, 0.0f);
93-
logger.info("Entity1's velocity after force: ({}, {}, {})", velocity1.getVelocityX(),
94-
velocity1.getVelocityY(), velocity1.getVelocityZ());
9576
}
9677

97-
// Render the system (optional rendering logic can be added here)
9878
gameSystem.renderSystem();
9979
}
100-
101-
// After the simulation, check final entity states
102-
logger.info("\nFinal Entity States:");
103-
logger.info("Entity1 position: {}, {}, {}",
104-
entity1.getTransformComponent().getPosition()[0],
105-
entity1.getTransformComponent().getPosition()[1],
106-
entity1.getTransformComponent().getPosition()[2]);
107-
logger.info("Entity1 velocity: {}, {}, {}",
108-
velocity1.getVelocityX(), velocity1.getVelocityY(), velocity1.getVelocityZ());
109-
logger.info("Entity1 health: {}", health1.getCurrentHealth());
11080
}
11181
}

entity-component-system/src/main/java/com/iluwatar/Entity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,13 @@ public boolean isEnabled() {
254254
* @param isEnabled true to enable the entity, false to disable it
255255
*/
256256
public void setIsEnabled(boolean isEnabled) {
257+
257258
this.isEnabled = isEnabled;
259+
260+
for (Entity child : children) {
261+
child.setIsEnabled(isEnabled);
262+
263+
}
258264
}
259265

260266
/**

entity-component-system/src/main/java/com/iluwatar/VelocityComponent.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public class VelocityComponent extends Component {
3838
private float velocityY; // The velocity in the Y direction
3939
private float velocityZ; // The velocity in the Z direction
4040

41+
private final float initialVelocityX;
42+
private final float initialVelocityY;
43+
private final float initialVelocityZ;
44+
4145
/**
4246
* Constructs a VelocityComponent with the given velocity values in the X, Y, and Z directions.
4347
*
@@ -49,8 +53,24 @@ public VelocityComponent(float velocityX, float velocityY, float velocityZ) {
4953
this.velocityX = velocityX;
5054
this.velocityY = velocityY;
5155
this.velocityZ = velocityZ;
56+
57+
this.initialVelocityX = velocityX;
58+
this.initialVelocityY = velocityY;
59+
this.initialVelocityZ = velocityZ;
60+
}
61+
62+
/**
63+
* update the velocity of the entity.
64+
* This method resets the velocity
65+
*
66+
*/
67+
public void resetVelocity() {
68+
this.velocityX = this.initialVelocityX;
69+
this.velocityY = this.initialVelocityY;
70+
this.velocityZ = this.initialVelocityZ;
5271
}
5372

73+
5474
/**
5575
* Updates the velocity of the entity based on delta time.
5676
* This method multiplies the current velocity by the delta time to simulate movement.

entity-component-system/src/test/java/com/iluwatar/AppTest.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import static org.junit.jupiter.api.Assertions.*;
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.PrintStream;
3032

3133
public class AppTest {
3234

@@ -53,7 +55,16 @@ public void setUp() {
5355
velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f);
5456
entity1.addComponent(velocity1);
5557
}
58+
@Test
59+
public void testMain_shouldPrintHelloWorld() {
60+
61+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
62+
System.setOut(new PrintStream(outputStream));
5663

64+
App.main(new String[]{});
65+
66+
assertEquals("Hello, World!", outputStream.toString(), "The output should be 'Hello, World!'");
67+
}
5768
@Test
5869
public void testHealthComponentApplyDamage() {
5970

@@ -121,4 +132,86 @@ public void testFinalEntityStateAfterSimulation() {
121132
assertNotNull(entity1.getTransformComponent(), "Entity1 should have a transform component");
122133

123134
}
135+
136+
@Test
137+
public void testAddTransformComponent() {
138+
entity1.addComponent(transform1);
139+
assertTrue(entity1.getComponents().contains(transform1), "Entity1 should contain the added TransformComponent.");
140+
}
141+
142+
@Test
143+
public void testGameLoopUpdatesEntityState() {
144+
GameSystem gameSystem = new GameSystem();
145+
gameSystem.addEntity(entity1);
146+
147+
for (int i = 0; i < 5; i++) {
148+
gameSystem.update(1.0f / 60.0f);
149+
}
150+
151+
// Check the updated health and velocity
152+
assertEquals(100, health1.getCurrentHealth(), "Health should not be affected yet.");
153+
assertEquals(1.0f, velocity1.getVelocityX(), "Velocity X should remain the same as no force is applied.");
154+
}
155+
156+
@Test
157+
public void testHealthReductionOverMultipleDamages() {
158+
health1.applyDamage(20);
159+
health1.applyDamage(30);
160+
161+
assertEquals(50, health1.getCurrentHealth(), "Health should be reduced by 50 after two damage applications.");
162+
}
163+
164+
@Test
165+
public void testEntityRemovalFromGameSystem() {
166+
GameSystem gameSystem = new GameSystem();
167+
gameSystem.addEntity(entity1);
168+
169+
gameSystem.removeEntity(entity1);
170+
171+
// Assert that entity1 is no longer in the system
172+
assertFalse(gameSystem.getEntities().contains(entity1), "Entity1 should no longer be in the GameSystem after removal.");
173+
}
174+
175+
@Test
176+
public void testEntityWithoutComponents() {
177+
Entity entity = new Entity("EmptyEntity");
178+
entity.removeComponent(entity.getTransformComponent());
179+
assertTrue(entity.getComponents().isEmpty(), "Entity should have no components.");
180+
assertEquals("EmptyEntity", entity.getName(), "Entity should have the correct name.");
181+
}
182+
183+
@Test
184+
public void testSetParentAndChildren() {
185+
Entity parentEntity = new Entity("ParentEntity");
186+
Entity childEntity = new Entity("ChildEntity");
187+
188+
parentEntity.addChild(childEntity);
189+
190+
assertTrue(parentEntity.getChildren().contains(childEntity), "Parent entity should contain the child entity.");
191+
assertEquals(parentEntity, childEntity.getParent(), "Child entity should have the parent entity set.");
192+
}
193+
194+
@Test
195+
public void testVelocityComponentReset() {
196+
velocity1.applyForce(1.0f, 0.0f, 0.0f);
197+
float newVelocityX = velocity1.getVelocityX();
198+
199+
velocity1.resetVelocity();
200+
201+
assertEquals(1.0f, velocity1.getVelocityX(), "Velocity should be reset to its initial value.");
202+
assertEquals(0.0f, velocity1.getVelocityY(), "Velocity Y should remain reset.");
203+
assertEquals(0.0f, velocity1.getVelocityZ(), "Velocity Z should remain reset.");
204+
}
205+
206+
@Test
207+
public void testHealthAndForceAppliedTogether() {
208+
health1.applyDamage(20);
209+
velocity1.applyForce(0.5f, 0.0f, 0.0f);
210+
211+
// Update entity state after applying damage and force
212+
entity1.update(1.0f / 60.0f);
213+
214+
assertEquals(80, health1.getCurrentHealth(), "Health should be reduced by 20 after damage.");
215+
assertEquals(1.5f, velocity1.getVelocityX(), "Velocity X should increase by 0.5 after applying force.");
216+
}
124217
}

entity-component-system/src/test/java/com/iluwatar/EntityTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,52 @@ public void testGetAndSetGameSystem() {
191191

192192
assertEquals(gameSystem, entity.getGameSystem(), "The game system should match the one set.");
193193
}
194+
195+
@Test
196+
public void testUpdate_whenEntityDisabled_shouldReturnImmediately() {
197+
198+
199+
Entity parent = new Entity("parent");
200+
Entity child = new Entity("child");
201+
202+
child.setParent(parent);
203+
204+
parent.setEnabled(false);
205+
parent.addComponent(transform1);
206+
207+
parent.update(1.0f);
208+
209+
assertFalse(transform1.getEnabled(), "Component should not be enabled.");
210+
assertDoesNotThrow(() -> transform1.update(1.0f), "Component update should not throw an exception when entity is disabled.");
211+
212+
assertDoesNotThrow(() -> child.update(1.0f), "Child entity should not throw an exception if the parent is disabled.");
213+
}
214+
215+
@Test
216+
public void testUpdate_shouldUpdateEnabledComponents() {
217+
entity1.setEnabled(true);
218+
entity2.setEnabled(false);
219+
entity1.addComponent(transform1);
220+
entity2.addComponent(transform2);
221+
entity1.update(1.0f);
222+
entity2.update(1.0f);
223+
224+
assertTrue(transform1.getEnabled(), "Transform1 should be enabled after update when the parent entity is enabled.");
225+
assertDoesNotThrow(() -> transform1.update(1.0f), "Transform1 update should not throw an exception when the parent entity is enabled.");
226+
227+
assertFalse(transform2.getEnabled(), "Transform2 should remain disabled after update when it is added but disabled.");
228+
assertDoesNotThrow(() -> transform2.update(1.0f), "Transform2 update should not throw an exception even though it's disabled.");
229+
}
230+
231+
@Test
232+
public void testUpdate_shouldUpdateChildEntities() {
233+
Entity child = new Entity("child");
234+
entity1.setEnabled(true);
235+
entity1.addChild(child);
236+
child.setParent(entity1);
237+
entity1.update(1.0f);
238+
assertDoesNotThrow(() -> child.update(1.0f));
239+
}
194240
}
241+
242+

entity-component-system/src/test/java/com/iluwatar/VelocityComponentTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ public void testApplyFriction() {
5252
assertEquals(0.9f, velocity.getVelocityX(), "VelocityX should be reduced by the friction coefficient.");
5353
}
5454

55+
@Test
56+
public void testReset_shouldResetVelocityToZero() {
57+
58+
velocity.setVelocityX( 5.0f);
59+
velocity.setVelocityY( 10.0f);
60+
velocity.setVelocityZ( 15.0f);
61+
velocity.resetVelocity();
62+
63+
assertEquals(1.0f, velocity.getVelocityX(), "Velocity X should be reset to 1.0f");
64+
assertEquals(0.0f, velocity.getVelocityY(), "Velocity Y should be reset to 0");
65+
assertEquals(0.0f, velocity.getVelocityZ(), "Velocity Z should be reset to 0");
66+
}
67+
5568
@Test
5669
public void testUpdateVelocity() {
5770
float deltaTime = 1.0f / 60.0f;

0 commit comments

Comments
 (0)