1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +00:00

Change-Id: I0cca627373c68d94025647f802a7fa6b419e0aad

This commit is contained in:
rogiel
2011-04-30 01:51:36 -03:00
parent f1d8e6588f
commit f454e3c35a
74 changed files with 2744 additions and 255 deletions

View File

@@ -0,0 +1,64 @@
package com.l2jserver.model.id.allocator;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import org.junit.Before;
import org.junit.Test;
public class BitSetIDAllocatorTest {
private final BitSetIDAllocator allocator = new BitSetIDAllocator();
@Before
public void tearUp() {
allocator.init();
}
@Test
public void testAllocate() {
final int id1 = allocator.allocate();
final int id2 = allocator.allocate();
assertFalse(id1 == id2);
assertEquals(IDAllocator.FIRST_ID, id1);
assertEquals(IDAllocator.FIRST_ID + 1, id2);
}
@Test
public void testAllocateRestore() {
final int id1 = IDAllocator.FIRST_ID;
final int id2 = IDAllocator.FIRST_ID + 1;
allocator.allocate(id1);
allocator.allocate(id2);
int id3 = allocator.allocate();
assertFalse(id1 == id3);
assertFalse(id2 == id3);
}
@Test
public void testAllocateMany() {
for (int i = 0; i < 100 * 1000; i++) {
allocator.allocate();
}
assertEquals(100000, allocator.getAllocatedIDs());
}
@Test(expected = IDAllocatorException.class)
public void testAllocateAlreadyAllocated() {
final int id1 = allocator.allocate();
allocator.allocate(id1);
}
@Test
public void testRelease() {
final int id = allocator.allocate();
allocator.release(id);
}
@Test(expected = IDAllocatorException.class)
public void testReleaseUnalloc() {
allocator.release(IDAllocator.FIRST_ID);
}
}

View File

@@ -0,0 +1,48 @@
package com.l2jserver.model.id.factory;
import junit.framework.Assert;
import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.l2jserver.db.dao.mysql5.DAOModuleMySQL5;
import com.l2jserver.model.id.CharacterID;
import com.l2jserver.model.id.ID;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.BasicServiceModule;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.database.DatabaseService;
public class IDFactoryTest {
private final Injector injector = Guice.createInjector(
new BasicServiceModule(), new DAOModuleMySQL5(),
new IDFactoryModule());
private final CharacterIDFactory charIdFactory = injector
.getInstance(CharacterIDFactory.class);
@Test
public void testCreateID() {
final ID id1 = charIdFactory.createID();
final ID id2 = charIdFactory.createID();
Assert.assertNotNull(id1);
Assert.assertFalse(id1.equals(id2));
}
@Test
public void testDestroy() {
final CharacterID id1 = charIdFactory.createID();
Assert.assertNotNull(id1);
charIdFactory.destroy(id1);
}
@Test
public void testGetObject() throws ServiceStartException {
injector.getInstance(DatabaseService.class).start();
final CharacterID id1 = charIdFactory.createID(268435456);
final L2Character character = id1.getObject();
Assert.assertNotNull(character);
Assert.assertEquals(id1, character.getID());
}
}

View File

@@ -0,0 +1,74 @@
package com.l2jserver.service.cache;
import java.util.Random;
import junit.framework.Assert;
import org.junit.Test;
public class SimpleCacheServiceTest {
private final SimpleCacheService cacheService = new SimpleCacheService();
@Test
public void testNoArgs() {
final TestCacheable cached = cacheService.decorate(TestCacheable.class,
new TestCacheableInstance());
int output1 = cached.random();
int output2 = cached.random();
Assert.assertEquals(output1, output2);
}
@Test
public void testSameArgs() {
final TestCacheable cached = cacheService.decorate(TestCacheable.class,
new TestCacheableInstance());
int output1 = cached.random(10);
int output2 = cached.random(10);
Assert.assertEquals(output1, output2);
}
@Test
public void testDifferentArgs() {
final TestCacheable cached = cacheService.decorate(TestCacheable.class,
new TestCacheableInstance());
int output1 = cached.random(10);
int output2 = cached.random(20);
Assert.assertFalse(output1 == output2);
}
@Test
public void testIgnoreCaching() {
final TestCacheable cached = cacheService.decorate(TestCacheable.class,
new TestCacheableInstance());
int output1 = cached.notCached();
int output2 = cached.notCached();
Assert.assertFalse(output1 == output2);
}
public interface TestCacheable extends Cacheable {
public int random();
public int random(int arg);
@IgnoreCaching
public int notCached();
}
public static class TestCacheableInstance implements TestCacheable {
private final Random random = new Random();
@Override
public int random() {
return random.nextInt(Integer.MAX_VALUE);
}
@Override
public int random(int arg) {
return random.nextInt(Integer.MAX_VALUE);
}
@Override
public int notCached() {
return random.nextInt(Integer.MAX_VALUE);
}
}
}

View File

@@ -11,8 +11,12 @@ import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Scopes;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.db.dao.mysql5.DAOModuleMySQL5;
import com.l2jserver.model.id.factory.CharacterIDFactory;
import com.l2jserver.model.id.factory.IDFactoryModule;
import com.l2jserver.model.id.factory.ItemIDFactory;
import com.l2jserver.model.world.Item;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.item.ItemDropEvent;
import com.l2jserver.model.world.item.ItemEvent;
import com.l2jserver.model.world.item.ItemListener;
@@ -30,9 +34,13 @@ public class WorldEventDispatcherImplTest {
private WorldService world;
private WorldEventDispatcher dispatcher;
private CharacterIDFactory cidFactory;
private ItemIDFactory iidFactory;
@Before
public void tearUp() throws ServiceStartException {
Injector injector = Guice.createInjector(new BasicServiceModule(),
new DAOModuleMySQL5(), new IDFactoryModule(),
new AbstractModule() {
@Override
protected void configure() {
@@ -44,6 +52,9 @@ public class WorldEventDispatcherImplTest {
}
});
cidFactory = injector.getInstance(CharacterIDFactory.class);
iidFactory = injector.getInstance(ItemIDFactory.class);
world = injector.getInstance(WorldService.class);
dispatcher = injector.getInstance(WorldEventDispatcher.class);
Assert.assertNotNull(world);
@@ -52,44 +63,56 @@ public class WorldEventDispatcherImplTest {
}
@Test
public void testListeners1() {
public void testListeners1() throws InterruptedException {
final L2Character character1 = new L2Character();
character1.setID(cidFactory.createID());
final L2Character character2 = new L2Character();
character2.setID(cidFactory.createID());
final Item item1 = new Item();
item1.setID(iidFactory.createID());
world.add(character1);
world.add(character2);
world.add(item1);
final AtomicBoolean bool = new AtomicBoolean();
Assert.assertFalse(bool.get());
character1.addListener(new PlayerListener() {
dispatcher.addListener(character1, new PlayerListener() {
@Override
protected void dispatch(PlayerEvent e) {
protected boolean dispatch(PlayerEvent e) {
bool.set(true);
e.getPlayer().removeListener(this);
return false;
}
});
character1.addListener(new PlayerListener() {
dispatcher.addListener(character1, new PlayerListener() {
@Override
protected void dispatch(PlayerEvent e) {
// bool.set(true);
protected boolean dispatch(PlayerEvent e) {
bool.set(true);
return false;
}
});
dispatcher.dispatch(new PlayerSpawnEvent(character1, null));
Thread.sleep(100); // wait a bit for dispatching to happen
Assert.assertTrue(bool.get());
bool.set(false);
dispatcher.dispatch(new PlayerSpawnEvent(character1, null));
Thread.sleep(100); // wait a bit for dispatching to happen
Assert.assertFalse(bool.get());
}
@Test
public void testListeners2() {
public void testListeners2() throws InterruptedException {
final L2Character character1 = new L2Character();
character1.setID(cidFactory.createID());
final L2Character character2 = new L2Character();
character2.setID(cidFactory.createID());
final Item item1 = new Item();
item1.setID(iidFactory.createID());
final Item item2 = new Item();
item2.setID(iidFactory.createID());
world.add(character1);
world.add(character2);
world.add(item1);
@@ -101,20 +124,24 @@ public class WorldEventDispatcherImplTest {
Assert.assertFalse(bool1.get());
Assert.assertFalse(bool2.get());
character1.addListener(new PlayerListener() {
dispatcher.addListener(character1, new PlayerListener() {
@Override
public void dispatch(PlayerEvent e) {
public boolean dispatch(PlayerEvent e) {
bool1.set(true);
return true;
}
});
item1.addListener(new ItemListener() {
dispatcher.addListener(item1, new ItemListener() {
@Override
public void dispatch(ItemEvent e) {
public boolean dispatch(ItemEvent e) {
bool2.set(true);
return true;
}
});
dispatcher.dispatch(new ItemDropEvent(character1, item1));
Thread.sleep(100); // wait a bit for dispatching to happen
Assert.assertTrue(bool1.get());
Assert.assertTrue(bool2.get());
}