Skip to content

Commit 49cfb6b

Browse files
committed
adding test cases to all classes
1 parent 0bb2e90 commit 49cfb6b

File tree

8 files changed

+224
-16
lines changed

8 files changed

+224
-16
lines changed

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

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

27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
2730
/**
2831
* The main entry point for the application.
2932
* This class simulates a game loop where entities are created, updated, and their states are modified.
3033
*/
3134
public class App {
3235

36+
// Create a logger instance
37+
public static final Logger logger = LoggerFactory.getLogger(App.class);
38+
3339
/**
3440
* The main method that runs the application.
3541
* It creates entities, adds components, updates them over several frames,
@@ -38,7 +44,7 @@ public class App {
3844
* @param args Command-line arguments (not used in this application)
3945
*/
4046
public static void main(String[] args) {
41-
// Create example entities
47+
4248
Entity entity1 = new Entity("Entity1");
4349
Entity entity2 = new Entity("Entity2");
4450

@@ -70,35 +76,36 @@ public static void main(String[] args) {
7076

7177
// Simulate for a few frames
7278
for (int i = 0; i < 10; i++) {
73-
System.out.println("Frame: " + (i + 1));
79+
logger.info("Frame: {}", i + 1);
7480

7581
// Update all entities in the system
7682
gameSystem.update(deltaTime);
7783

7884
// Apply some damage to entity1's health component at frame 6
7985
if (i == 5) {
8086
health1.applyDamage(30);
81-
System.out.println("Entity1's health after damage: " + health1.getCurrentHealth());
87+
logger.info("Entity1's health after damage: {}", health1.getCurrentHealth());
8288
}
8389

8490
// Apply some force to entity1's velocity at frame 3
8591
if (i == 3) {
8692
velocity1.applyForce(0.5f, 0.0f, 0.0f);
87-
System.out.println("Entity1's velocity after force: (" + velocity1.getVelocityX() + ", "
88-
+ velocity1.getVelocityY() + ", " + velocity1.getVelocityZ() + ")");
93+
logger.info("Entity1's velocity after force: ({}, {}, {})", velocity1.getVelocityX(),
94+
velocity1.getVelocityY(), velocity1.getVelocityZ());
8995
}
9096

9197
// Render the system (optional rendering logic can be added here)
9298
gameSystem.renderSystem();
9399
}
94100

95101
// After the simulation, check final entity states
96-
System.out.println("\nFinal Entity States:");
97-
System.out.println("Entity1 position: " + entity1.getTransformComponent().getPosition()[0] + ", "
98-
+ entity1.getTransformComponent().getPosition()[1] + ", "
99-
+ entity1.getTransformComponent().getPosition()[2]);
100-
System.out.println("Entity1 velocity: " + velocity1.getVelocityX() + ", "
101-
+ velocity1.getVelocityY() + ", " + velocity1.getVelocityZ());
102-
System.out.println("Entity1 health: " + health1.getCurrentHealth());
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());
103110
}
104111
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.ArrayList;
2828
import java.util.List;
29+
import java.util.Objects;
2930
import java.util.UUID;
3031

3132
/**
@@ -58,6 +59,7 @@ public Entity(String entityName) {
5859
final UUID entityId = UUID.randomUUID(); // Generates a unique UUID for this entity
5960
transform = new TransformComponent(new float[] {0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f}, new float[] {1.0f, 1.0f, 1.0f});
6061
addComponent(transform);
62+
6163
}
6264

6365
/**
@@ -90,7 +92,7 @@ public void removeComponent(Component component) {
9092
*/
9193
public Component getComponent(String componentName) {
9294
for (Component component : components) {
93-
if (component.getName().equals(componentName)) {
95+
if (Objects.equals(component.getName(), componentName)) {
9496
return component;
9597
}
9698
}
@@ -204,6 +206,7 @@ public void setTransformComponent(TransformComponent transform) {
204206
* @return a list of mesh render components
205207
*/
206208
public List<Component> getComponents() {
209+
207210
return components;
208211
}
209212

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public HealthComponent(float maxHealth) {
5959
*/
6060
@Override
6161
public void update(float deltaTime) {
62-
logger.info(String.format("Updated health component: %f", currentHealth));
62+
//update health
6363
}
6464

6565
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
import static org.junit.jupiter.api.Assertions.*;
3030

3131
public class ComponentTest {
32+
@Test
33+
public void testGetName() {
34+
Component component = new HealthComponent(100);
35+
component.setName("Health");
36+
37+
assertEquals("Health", component.getName(), "getName should return 'TestComponent'");
38+
}
39+
40+
@Test
41+
public void testSetName() {
42+
Component component = new HealthComponent(100);
43+
component.setName("Health");
44+
45+
assertEquals("Health", component.getName(), "getName should return 'TestComponent'");
46+
}
3247

3348
@Test
3449
public void testComponentEnabled() {

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,79 @@ public void testRemoveComponent() {
6565
assertEquals(1, entity2.getComponents().size(), "Entity1 should have no components.");
6666
}
6767

68+
/*
6869
@Test
6970
public void testSetParent() {
7071
entity1 = new Entity("Entity1");
7172
entity2 = new Entity("Entity2");
7273
entity1.setParent(entity2);
7374
assertEquals(entity2.getEntityId(), entity1.getParent().getEntityId(), "Entity1 should have Entity2 as its parent.");
7475
}
75-
76+
*/
7677
@Test
7778
public void testAddChild() {
7879
entity1.addChild(entity2);
7980

8081
assertTrue(entity1.getChildren().contains(entity2), "Entity1 should have Entity2 as its child.");
8182
}
82-
83+
/*
8384
@Test
8485
public void testSetEnabled() {
8586
entity1.setEnabled(false);
8687
assertFalse(entity1.getEnabled(), "Entity1 should be disabled.");
8788
entity1.setEnabled(true);
8889
assertTrue(entity1.getEnabled(), "Entity1 should be enabled.");
90+
} */
91+
92+
@Test
93+
public void testGetComponent() {
94+
95+
Component component = new HealthComponent(100);
96+
component.setName("HealthComponent");
97+
Entity entity = new Entity("Entity1");
98+
entity.addComponent(component);
99+
Component retrievedComponent = entity.getComponent("HealthComponent");
100+
101+
assertEquals(component, retrievedComponent, "The component returned should match the added component.");
102+
103+
Component nonExistentComponent = entity.getComponent("NonExistentComponent");
104+
assertNull(nonExistentComponent, "The component should return null if it doesn't exist.");
105+
}
106+
107+
@Test
108+
public void testSetEnabled() {
109+
110+
Component component = new HealthComponent(100);
111+
Entity entity = new Entity("MyEntity");
112+
entity.addComponent(component);
113+
114+
assertFalse(component.getEnabled(), "Component should be disabled initially.");
115+
116+
entity.setEnabled(false);
117+
118+
assertFalse(component.getEnabled(), "Component should be disabled after calling setEnabled(false).");
119+
120+
entity.setEnabled(true);
121+
122+
assertTrue(component.getEnabled(), "Component should be enabled after calling setEnabled(true).");
123+
}
124+
125+
@Test
126+
public void testSetParent() {
127+
128+
Entity parent = new Entity("parent");
129+
Entity child = new Entity("child");
130+
131+
child.setParent(parent);
132+
133+
assertTrue(parent.getChildren().contains(child), "Parent should contain child in its children list.");
134+
135+
Entity newParent = new Entity("newParent");
136+
child.setParent(newParent);
137+
138+
assertFalse(parent.getChildren().contains(child), "Parent should no longer contain child after setting a new parent.");
139+
140+
assertTrue(newParent.getChildren().contains(child), "New parent should contain child in its children list.");
89141
}
90142

91143
@Test
@@ -95,4 +147,45 @@ public void testUpdate() {
95147

96148
assertNotNull(entity1, "Entity1 should be updated.");
97149
}
150+
151+
@Test
152+
public void testRenderEntity() {
153+
154+
Component component = new HealthComponent(100);
155+
Entity entity = new Entity("MyEntity");
156+
entity.addComponent(component);
157+
entity.renderEntity();
158+
159+
assertDoesNotThrow(() -> component.update(1.0f), "render function should not throw an exception");
160+
}
161+
162+
@Test
163+
public void testGetName() {
164+
Entity entity = new Entity("MyEntity");
165+
166+
assertEquals("MyEntity", entity.getName(), "The entity name should match the given name.");
167+
}
168+
169+
@Test
170+
public void testSetIsEnabled() {
171+
Entity entity = new Entity("MyEntity");
172+
173+
entity.setIsEnabled(false);
174+
175+
assertFalse(entity.isEnabled(), "The entity should be disabled after calling setIsEnabled(false).");
176+
177+
entity.setIsEnabled(true);
178+
179+
assertTrue(entity.isEnabled(), "The entity should be enabled after calling setIsEnabled(true).");
180+
}
181+
182+
@Test
183+
public void testGetAndSetGameSystem() {
184+
185+
GameSystem gameSystem = new GameSystem();
186+
Entity entity = new Entity("MyEntity");
187+
entity.setGameSystem(gameSystem);
188+
189+
assertEquals(gameSystem, entity.getGameSystem(), "The game system should match the one set.");
190+
}
98191
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,55 @@ public void testHeal() {
6060
healthComponent.heal(50);
6161
assertEquals(100, healthComponent.getCurrentHealth(), "Health should not exceed maximum (100).");
6262
}
63+
@Test
64+
public void testUpdateFunction() {
65+
healthComponent = new HealthComponent(100);
66+
67+
assertDoesNotThrow(() -> healthComponent.update(1.0f), "update function should not throw an exception");
68+
}
69+
70+
@Test
71+
public void testGetMaxHealth() {
72+
HealthComponent healthComponent = new HealthComponent(100f);
73+
74+
assertEquals(100f, healthComponent.getMaxHealth(), "Max health should be 100.");
75+
}
76+
77+
@Test
78+
public void testSetMaxHealth() {
79+
HealthComponent healthComponent = new HealthComponent(100f);
80+
healthComponent.setMaxHealth(120f);
81+
82+
assertEquals(120f, healthComponent.getMaxHealth(), "Max health should be updated to 120.");
83+
}
84+
85+
@Test
86+
public void testIsAlive() {
87+
88+
HealthComponent healthComponent = new HealthComponent(100f);
89+
assertTrue(healthComponent.isAlive(), "Entity should be alive initially.");
90+
healthComponent.applyDamage(100f);
91+
92+
assertFalse(healthComponent.isAlive(), "Entity should be dead after taking 100 damage.");
93+
}
94+
95+
@Test
96+
public void testSetAlive() {
6397

98+
HealthComponent healthComponent = new HealthComponent(100f);
99+
assertTrue(healthComponent.isAlive(), "Entity should be alive initially.");
100+
healthComponent.setAlive(false);
101+
102+
assertFalse(healthComponent.isAlive(), "Entity should be dead after setting alive to false.");
103+
}
104+
105+
@Test
106+
public void testSetCurrentHealth() {
107+
HealthComponent healthComponent = new HealthComponent(100f);
108+
healthComponent.setCurrentHealth(80f);
109+
110+
assertEquals(80f, healthComponent.getCurrentHealth(), "Current health should be updated to 80.");
111+
}
64112
@Test
65113
public void testHealthCannotGoAboveMax() {
66114
healthComponent.heal(150);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ public void setUp() {
3939
new float[]{0.0f, 0.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});
4040
}
4141

42+
43+
@Test
44+
public void testTransformComponentConstructor() {
45+
TransformComponent transform = new TransformComponent();
46+
47+
float[] expectedPosition = new float[]{0.0f, 0.0f, 0.0f};
48+
float[] expectedRotation = new float[]{0.0f, 0.0f, 0.0f};
49+
float[] expectedScale = new float[]{1.0f, 1.0f, 1.0f};
50+
51+
assertArrayEquals(expectedPosition, transform.getPosition(), "Position should be initialized to [0.0f, 0.0f, 0.0f]");
52+
assertArrayEquals(expectedRotation, transform.getRotation(), "Rotation should be initialized to [0.0f, 0.0f, 0.0f]");
53+
assertArrayEquals(expectedScale, transform.getScale(), "Scale should be initialized to [1.0f, 1.0f, 1.0f]");
54+
}
55+
4256
@Test
4357
public void testGetPosition() {
4458
float[] position = transform.getPosition();
@@ -74,6 +88,13 @@ public void testGetScale() {
7488
assertArrayEquals(new float[]{1.0f, 1.0f, 1.0f}, scale, "The scale should be initialized correctly.");
7589
}
7690

91+
@Test
92+
public void testUpdateFunction() {
93+
TransformComponent transform = new TransformComponent();
94+
95+
assertDoesNotThrow(() -> transform.update(1.0f), "update function should not throw an exception");
96+
}
97+
7798
@Test
7899
public void testSetScale() {
79100
transform.setScale(new float[]{2.0f, 2.0f, 2.0f});

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,25 @@ public void testUpdateVelocity() {
6161

6262
assertTrue(velocity.getVelocityX() > initialVelocityX, "VelocityX should increase after updating.");
6363
}
64+
65+
@Test
66+
public void testSetVelocityX() {
67+
68+
velocity = new VelocityComponent(1.0f, 0.0f, 0.0f);
69+
velocity.setVelocityX(5.0f);
70+
assertEquals(5.0f, velocity.getVelocityX(), "The velocityX should be set to 5.0f");
71+
72+
velocity.setVelocityX(10.0f);
73+
assertEquals(10.0f, velocity.getVelocityX(), "The velocityX should now be set to 10.0f");
74+
}
75+
@Test
76+
public void testSetVelocityZ() {
77+
78+
velocity = new VelocityComponent(1.0f, 0.0f, 0.0f);
79+
velocity.setVelocityZ(5.0f);
80+
assertEquals(5.0f, velocity.getVelocityZ(), "The velocityZ should be set to 5.0f");
81+
82+
velocity.setVelocityZ(10.0f);
83+
assertEquals(10.0f, velocity.getVelocityZ(), "The velocityZ should now be set to 10.0f");
84+
}
6485
}

0 commit comments

Comments
 (0)