Skip to content

Commit daa8d10

Browse files
committed
Revamp unit tests
- Replace EasyMock with Mockito for easier mocking - Bring in Hamcrest for more semantic assertions
1 parent 8e5d62b commit daa8d10

File tree

66 files changed

+1746
-2296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1746
-2296
lines changed

aoxp-server/pom.xml

+10-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
<groupId>junit</groupId>
4444
<artifactId>junit</artifactId>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.hamcrest</groupId>
48+
<artifactId>java-hamcrest</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.hamcrest</groupId>
52+
<artifactId>hamcrest-junit</artifactId>
53+
</dependency>
4654
<dependency>
4755
<groupId>ch.qos.logback</groupId>
4856
<artifactId>logback-classic</artifactId>
@@ -64,12 +72,8 @@
6472
<artifactId>netty</artifactId>
6573
</dependency>
6674
<dependency>
67-
<groupId>org.easymock</groupId>
68-
<artifactId>easymock</artifactId>
69-
</dependency>
70-
<dependency>
71-
<groupId>org.easymock</groupId>
72-
<artifactId>easymockclassextension</artifactId>
75+
<groupId>org.mockito</groupId>
76+
<artifactId>mockito-core</artifactId>
7377
</dependency>
7478
<dependency>
7579
<groupId>commons-validator</groupId>

aoxp-server/src/main/java/com/ao/model/inventory/InventoryImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public InventoryImpl(Item[] inventory) {
4949
* @see com.ao.model.inventory.Inventory#addItem(ao.model.worldobject.Item)
5050
*/
5151
@Override
52-
public int addItem(Item item) {
52+
public int addItem(final Item item) {
5353
int i;
5454

5555
if ((i = hasItem(item)) != -1) {
5656
int amount = item.getAmount();
5757
int newAmount, oldAmount;
58-
int id = item.getId();
58+
final int id = item.getId();
5959

6060
// Stack the item to previous slots
6161
for (; i < inventory.length; i++) {

aoxp-server/src/main/java/com/ao/model/worldobject/AbstractItem.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
AO-XP Server (XP stands for Cross Platform) is a Java implementation of Argentum Online's server
2+
AO-XP Server (XP stands for Cross Platform) is a Java implementation of Argentum Online's server
33
Copyright (C) 2009 Juan Martín Sotuyo Dodero. <[email protected]>
44
55
This program is free software: you can redistribute it and/or modify
@@ -32,19 +32,19 @@
3232
public abstract class AbstractItem extends AbstractWorldObject implements Item {
3333

3434
protected static final int MAX_STACKED_ITEMS = 10000;
35-
35+
3636
protected int amount;
37-
37+
3838
/**
3939
* Creates a new AbstractItem instance.
4040
* @param properties The item's properties.
4141
* @param amount The item's amount.
4242
*/
43-
public AbstractItem(ItemProperties properties, int amount) {
43+
public AbstractItem(final ItemProperties properties, final int amount) {
4444
super(properties);
45-
45+
4646
this.amount = amount;
47-
47+
4848
if (this.amount > MAX_STACKED_ITEMS) {
4949
this.amount = MAX_STACKED_ITEMS;
5050
} else if (this.amount < 0) {
@@ -57,14 +57,14 @@ public AbstractItem(ItemProperties properties, int amount) {
5757
* @see com.ao.model.worldobject.Item#addAmount(int)
5858
*/
5959
@Override
60-
public int addAmount(int amount) {
61-
60+
public int addAmount(final int amount) {
61+
6262
if (this.amount + amount <= MAX_STACKED_ITEMS) {
6363
this.amount += amount;
6464
} else {
6565
this.amount = MAX_STACKED_ITEMS;
6666
}
67-
67+
6868
return this.amount;
6969
}
7070

@@ -73,25 +73,25 @@ public int addAmount(int amount) {
7373
* @see com.ao.model.worldobject.Item#canBeUsedBy(ao.model.character.Race, com.ao.model.character.Gender, com.ao.model.character.archetype.UserArchetype, com.ao.model.character.Reputation)
7474
*/
7575
@Override
76-
public boolean canBeUsedBy(Race race, Gender gender,
77-
UserArchetype archetype, Reputation reputation) {
78-
76+
public boolean canBeUsedBy(final Race race, final Gender gender,
77+
final UserArchetype archetype, final Reputation reputation) {
78+
7979
// Check if the archetype can use this item
80-
List<UserArchetype> forbiddenArchetypes = ((ItemProperties) properties).getForbiddenArchetypes();
81-
80+
final List<UserArchetype> forbiddenArchetypes = ((ItemProperties) properties).getForbiddenArchetypes();
81+
8282
if (forbiddenArchetypes != null && forbiddenArchetypes.contains(archetype)) {
8383
return false;
8484
}
85-
85+
8686
// Check if the race can use this item
87-
List<Race> forbiddenRaces = ((ItemProperties) properties).getForbiddenRaces();
88-
87+
final List<Race> forbiddenRaces = ((ItemProperties) properties).getForbiddenRaces();
88+
8989
if (forbiddenRaces != null && forbiddenRaces.contains(race)) {
9090
return false;
9191
}
92-
92+
9393
// TODO : Check by gender and reputation
94-
94+
9595
return true;
9696
}
9797

@@ -121,7 +121,7 @@ public int getValue() {
121121
public boolean isNewbie() {
122122
return ((ItemProperties) properties).isNewbie();
123123
}
124-
124+
125125
/*
126126
* (non-Javadoc)
127127
* @see com.ao.model.worldobject.Item#canBeStolen()
@@ -145,7 +145,7 @@ public boolean canBeStolen() {
145145
public boolean isFalls() {
146146
return ((ItemProperties) properties).isFalls();
147147
}
148-
148+
149149
/*
150150
* (non-Javadoc)
151151
* @see com.ao.model.worldobject.Item#isNoLog()

aoxp-server/src/test/java/com/ao/data/dao/ini/CityDAOIniTest.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.Reader;
1212

1313
import org.ini4j.Ini;
14-
import org.junit.After;
1514
import org.junit.Before;
1615
import org.junit.Test;
1716

@@ -38,13 +37,6 @@ public void setUp() throws Exception {
3837
dao = new CityDAOIni(CITIES_DAT_PATH);
3938
}
4039

41-
/**
42-
* @throws java.lang.Exception
43-
*/
44-
@After
45-
public void tearDown() throws Exception {
46-
}
47-
4840
/**
4941
* Test method for {@link com.ao.data.dao.ini.CityDAOIni#retrieveAll()}.
5042
*/
@@ -55,17 +47,16 @@ public final void testRetrieveAll() {
5547

5648
try {
5749
// Make sure the reader is closed, since Ini4J gives no guarantees.
58-
Reader reader = new BufferedReader(new FileReader(CITIES_DAT_PATH));
50+
final Reader reader = new BufferedReader(new FileReader(CITIES_DAT_PATH));
5951
iniFile = new Ini(reader);
6052
reader.close();
61-
} catch (Exception e) {
53+
} catch (final Exception e) {
6254
fail("Loading of cities failed with message " + e.getMessage());
6355
}
6456

65-
int totalCities = Integer.parseInt(iniFile.get(INIT_HEADER, NUM_CITIES_KEY));
66-
57+
final int totalCities = Integer.parseInt(iniFile.get(INIT_HEADER, NUM_CITIES_KEY));
6758

68-
City[] cities = dao.retrieveAll();
59+
final City[] cities = dao.retrieveAll();
6960

7061
assertEquals(totalCities,cities.length);
7162

aoxp-server/src/test/java/com/ao/data/dao/ini/NPCPropertiesDAOIniTest.java

+38-44
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
package com.ao.data.dao.ini;
1919

2020

21+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
2122
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.assertThat;
2324
import static org.junit.Assert.fail;
25+
import static org.mockito.Matchers.anyInt;
26+
import static org.mockito.Matchers.eq;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.when;
2429

25-
import org.easymock.classextension.EasyMock;
26-
import org.junit.After;
2730
import org.junit.Before;
2831
import org.junit.Test;
2932

@@ -60,83 +63,74 @@ public class NPCPropertiesDAOIniTest {
6063

6164
private static final String TEST_NPCS_DAT = "src/test/resources/NPCs.dat";
6265

63-
protected NPCPropertiesDAOIni dao;
66+
private NPCPropertiesDAOIni dao;
6467

6568
@Before
6669
public void setUp() throws Exception {
67-
WorldObjectProperties woProperties = EasyMock.createMock(WorldObjectProperties.class);
68-
AbstractItem item = EasyMock.createMock(AbstractItem.class);
69-
EasyMock.expect(item.getId()).andReturn(1).anyTimes();
70-
EasyMock.expect(item.getAmount()).andReturn(1).anyTimes();
71-
EasyMock.expect(item.addAmount(EasyMock.anyInt())).andReturn(1).anyTimes();
72-
EasyMock.replay(woProperties, item);
70+
final WorldObjectProperties woProperties = mock(WorldObjectProperties.class);
71+
final AbstractItem item = mock(AbstractItem.class);
7372

74-
WorldObjectPropertiesDAO woDao = EasyMock.createMock(WorldObjectPropertiesDAO.class);
75-
EasyMock.expect(woDao.getWorldObjectProperties(EasyMock.anyInt())).andReturn(woProperties).anyTimes();
73+
final WorldObjectPropertiesDAO woDao = mock(WorldObjectPropertiesDAO.class);
74+
when(woDao.getWorldObjectProperties(anyInt())).thenReturn(woProperties);
7675

77-
WorldObjectFactory woFactory = EasyMock.createMock(WorldObjectFactory.class);
78-
EasyMock.expect(woFactory.getWorldObject(EasyMock.eq(woProperties), EasyMock.anyInt())).andReturn(item).anyTimes();
79-
80-
EasyMock.replay(woDao, woFactory);
76+
final WorldObjectFactory woFactory = mock(WorldObjectFactory.class);
77+
when(woFactory.getWorldObject(eq(woProperties), anyInt())).thenReturn(item);
8178

8279
dao = new NPCPropertiesDAOIni(TEST_NPCS_DAT, woDao, woFactory);
8380
}
8481

85-
@After
86-
public void tearDown() throws Exception {
87-
}
88-
8982
@Test
9083
public void testRetrieveAll() {
91-
NPCProperties[] npcProperties = null;
84+
final NPCProperties[] npcProperties;
9285
try {
9386
npcProperties = dao.retrieveAll();
94-
} catch (DAOException e) {
87+
} catch (final DAOException e) {
9588
fail("Loading of npcs failed with message " + e.getMessage());
89+
return;
9690
}
9791

98-
NPCProperties snake = npcProperties[COMMON_NPC_INDEX];
99-
assertTrue(snake instanceof CreatureNPCProperties);
92+
final NPCProperties snake = npcProperties[COMMON_NPC_INDEX];
93+
assertThat(snake, instanceOf(CreatureNPCProperties.class));
10094
assertEquals(NPCType.COMMON, snake.getType());
10195

102-
NPCProperties dragon = npcProperties[DRAGON_NPC_INDEX];
103-
assertTrue(dragon instanceof CreatureNPCProperties);
96+
final NPCProperties dragon = npcProperties[DRAGON_NPC_INDEX];
97+
assertThat(dragon, instanceOf(CreatureNPCProperties.class));
10498
assertEquals(NPCType.DRAGON, dragon.getType());
10599

106-
NPCProperties trainer = npcProperties[TRAINER_NPC_INDEX];
107-
assertTrue(trainer instanceof TrainerNPCProperties);
100+
final NPCProperties trainer = npcProperties[TRAINER_NPC_INDEX];
101+
assertThat(trainer, instanceOf(TrainerNPCProperties.class));
108102
assertEquals(NPCType.TRAINER, trainer.getType());
109103

110-
NPCProperties governor = npcProperties[GOVERNOR_NPC_INDEX];
111-
assertTrue(governor instanceof GovernorNPCProperties);
104+
final NPCProperties governor = npcProperties[GOVERNOR_NPC_INDEX];
105+
assertThat(governor, instanceOf(GovernorNPCProperties.class));
112106
assertEquals(NPCType.GOVERNOR, governor.getType());
113107

114-
NPCProperties royalGuard = npcProperties[ROYAL_GUARD_NPC_INDEX];
115-
assertTrue(royalGuard instanceof GuardNPCProperties);
108+
final NPCProperties royalGuard = npcProperties[ROYAL_GUARD_NPC_INDEX];
109+
assertThat(royalGuard, instanceOf(GuardNPCProperties.class));
116110
assertEquals(NPCType.ROYAL_GUARD, royalGuard.getType());
117111

118-
NPCProperties chaosGuard = npcProperties[CHAOS_GUARD_NPC_INDEX];
119-
assertTrue(chaosGuard instanceof GuardNPCProperties);
112+
final NPCProperties chaosGuard = npcProperties[CHAOS_GUARD_NPC_INDEX];
113+
assertThat(chaosGuard, instanceOf(GuardNPCProperties.class));
120114
assertEquals(NPCType.CHAOS_GUARD, chaosGuard.getType());
121115

122-
NPCProperties newbieResucitator = npcProperties[NEWBIE_RESUCITATOR_NPC_INDEX];
123-
assertTrue(newbieResucitator instanceof NPCProperties);
116+
final NPCProperties newbieResucitator = npcProperties[NEWBIE_RESUCITATOR_NPC_INDEX];
117+
assertThat(newbieResucitator, instanceOf(NPCProperties.class));
124118
assertEquals(NPCType.NEWBIE_RESUCITATOR, newbieResucitator.getType());
125119

126-
NPCProperties resucitator = npcProperties[RESUCITATOR_NPC_INDEX];
127-
assertTrue(resucitator instanceof NPCProperties);
120+
final NPCProperties resucitator = npcProperties[RESUCITATOR_NPC_INDEX];
121+
assertThat(resucitator, instanceOf(NPCProperties.class));
128122
assertEquals(NPCType.RESUCITATOR, resucitator.getType());
129123

130-
NPCProperties gambler = npcProperties[GAMBLER_NPC_INDEX];
131-
assertTrue(gambler instanceof NPCProperties);
124+
final NPCProperties gambler = npcProperties[GAMBLER_NPC_INDEX];
125+
assertThat(gambler, instanceOf(NPCProperties.class));
132126
assertEquals(NPCType.GAMBLER, gambler.getType());
133127

134-
NPCProperties banker = npcProperties[BANKER_NPC_INDEX];
135-
assertTrue(banker instanceof NPCProperties);
128+
final NPCProperties banker = npcProperties[BANKER_NPC_INDEX];
129+
assertThat(banker, instanceOf(NPCProperties.class));
136130
assertEquals(NPCType.BANKER, banker.getType());
137131

138-
NPCProperties noble = npcProperties[NOBLE_NPC_INDEX];
139-
assertTrue(noble instanceof NobleNPCProperties);
132+
final NPCProperties noble = npcProperties[NOBLE_NPC_INDEX];
133+
assertThat(noble, instanceOf(NobleNPCProperties.class));
140134
assertEquals(NPCType.NOBLE, noble.getType());
141135
}
142136

0 commit comments

Comments
 (0)