From 81dea2def4153fdfa918512a68d6f397945818b3 Mon Sep 17 00:00:00 2001 From: Rogiel Date: Thu, 26 May 2011 20:28:02 -0300 Subject: [PATCH] JUnit test fixes Signed-off-by: Rogiel --- .../service/cache/EhCacheService.java | 22 +- .../game/world/CachedWorldIDService.java | 2 - .../service/game/world/WorldServiceImpl.java | 3 + .../db/dao/mysql5/MySQL5CharacterDAOTest.java | 5 +- .../model/id/factory/IDFactoryTest.java | 21 +- .../character/CharacterFriendListTest.java | 66 ---- .../service/cache/SimpleCacheServiceTest.java | 8 + .../PrecompiledScriptCompilerTest.java | 3 +- .../template/StaticTemplateServiceTest.java | 5 +- .../service/world/WorldServiceImplTest.java | 63 ++-- .../util/calculator/CalculatorTest.java | 285 +++++++++--------- 11 files changed, 233 insertions(+), 250 deletions(-) delete mode 100644 src/test/java/com/l2jserver/model/world/character/CharacterFriendListTest.java diff --git a/src/main/java/com/l2jserver/service/cache/EhCacheService.java b/src/main/java/com/l2jserver/service/cache/EhCacheService.java index 8f871a5cb..7ba709670 100644 --- a/src/main/java/com/l2jserver/service/cache/EhCacheService.java +++ b/src/main/java/com/l2jserver/service/cache/EhCacheService.java @@ -17,6 +17,7 @@ package com.l2jserver.service.cache; import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; @@ -75,12 +76,21 @@ public class EhCacheService extends AbstractService implements CacheService { return method.invoke(instance, args); final MethodInvocation invocation = new MethodInvocation( method, args); - Object result = interfaceCache.get(invocation) - .getObjectValue(); - if (result == null) { - result = method.invoke(instance, args); - interfaceCache.put(new Element(invocation, result)); - } + Element element = interfaceCache.get(invocation); + if (element == null) + return doInvoke(invocation, proxy, method, args); + Object result = element.getObjectValue(); + if (result == null) + return doInvoke(invocation, proxy, method, args); + return result; + } + + private Object doInvoke(MethodInvocation invocation, + Object proxy, Method method, Object[] args) + throws IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + Object result = method.invoke(instance, args); + interfaceCache.put(new Element(invocation, result)); return result; } }); diff --git a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java index b5530f788..78d036b0e 100644 --- a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java +++ b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java @@ -147,8 +147,6 @@ public class CachedWorldIDService extends AbstractService implements @Override public > void add(I id) { Preconditions.checkNotNull(id, "id"); - if (id == null) - return; cache.put(new Element(id.getID(), id)); } diff --git a/src/main/java/com/l2jserver/service/game/world/WorldServiceImpl.java b/src/main/java/com/l2jserver/service/game/world/WorldServiceImpl.java index 652c2114f..8c1353aac 100644 --- a/src/main/java/com/l2jserver/service/game/world/WorldServiceImpl.java +++ b/src/main/java/com/l2jserver/service/game/world/WorldServiceImpl.java @@ -89,6 +89,7 @@ public class WorldServiceImpl extends AbstractService implements WorldService { @Override public boolean add(WorldObject object) { Preconditions.checkNotNull(object, "object"); + Preconditions.checkNotNull(object.getID(), "id"); log.debug("Adding object {} to world", object); return objects.add(object); } @@ -96,6 +97,7 @@ public class WorldServiceImpl extends AbstractService implements WorldService { @Override public boolean remove(WorldObject object) { Preconditions.checkNotNull(object, "object"); + Preconditions.checkNotNull(object.getID(), "id"); log.debug("Removing object {} from world", object); // we also need to remove all listeners for this object dispatcher.clear(object.getID()); @@ -105,6 +107,7 @@ public class WorldServiceImpl extends AbstractService implements WorldService { @Override public boolean contains(WorldObject object) { Preconditions.checkNotNull(object, "object"); + Preconditions.checkNotNull(object.getID(), "id"); return objects.contains(object); } diff --git a/src/test/java/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAOTest.java b/src/test/java/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAOTest.java index 9daa8f99b..2612a44cb 100644 --- a/src/test/java/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAOTest.java +++ b/src/test/java/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAOTest.java @@ -22,6 +22,7 @@ import org.junit.Test; import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.Stage; import com.l2jserver.GameServerModule; import com.l2jserver.db.dao.CharacterDAO; import com.l2jserver.model.id.object.provider.CharacterIDProvider; @@ -30,15 +31,17 @@ import com.l2jserver.service.ServiceManager; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.database.DatabaseService; import com.l2jserver.service.game.template.TemplateService; +import com.l2jserver.service.game.world.WorldService; public class MySQL5CharacterDAOTest { private final Injector injector = Guice - .createInjector(new GameServerModule()); + .createInjector(Stage.PRODUCTION, new GameServerModule()); @Test public void testCachedLoad() throws ServiceStartException { injector.getInstance(ServiceManager.class).start(TemplateService.class); injector.getInstance(ServiceManager.class).start(DatabaseService.class); + injector.getInstance(ServiceManager.class).start(WorldService.class); final CharacterDAO dao = injector.getInstance(CharacterDAO.class); final L2Character char1 = dao.select(injector.getInstance( diff --git a/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java b/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java index 4c29aaa16..e46331d62 100644 --- a/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java +++ b/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java @@ -18,7 +18,7 @@ package com.l2jserver.model.id.factory; import junit.framework.Assert; -import org.apache.log4j.BasicConfigurator; +import org.junit.Before; import org.junit.Test; import com.google.inject.Guice; @@ -29,17 +29,25 @@ import com.l2jserver.model.id.object.CharacterID; import com.l2jserver.model.id.object.provider.CharacterIDProvider; import com.l2jserver.model.id.provider.IDProviderModule; import com.l2jserver.model.world.L2Character; +import com.l2jserver.service.ServiceManager; import com.l2jserver.service.ServiceModule; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.game.scripting.ScriptingService; import com.l2jserver.service.game.template.TemplateService; +import com.l2jserver.service.game.world.WorldService; public class IDFactoryTest { private final Injector injector = Guice.createInjector(new ServiceModule(), new MySQL5DAOModule(), new IDProviderModule()); - private final CharacterIDProvider charIdFactory = injector - .getInstance(CharacterIDProvider.class); + private CharacterIDProvider charIdFactory; + + @Before + public void tearUp() throws ServiceStartException { + injector.getInstance(ServiceManager.class).start(TemplateService.class); + injector.getInstance(ServiceManager.class).start(DatabaseService.class); + injector.getInstance(ServiceManager.class).start(WorldService.class); + charIdFactory = injector.getInstance(CharacterIDProvider.class); + } @Test public void testCreateID() { @@ -58,11 +66,6 @@ public class IDFactoryTest { @Test public void testGetObject() throws ServiceStartException { - BasicConfigurator.configure(); - injector.getInstance(ScriptingService.class).start(); - injector.getInstance(TemplateService.class).start(); - - injector.getInstance(DatabaseService.class).start(); final CharacterID id = charIdFactory.createID(268437456); final L2Character character = id.getObject(); diff --git a/src/test/java/com/l2jserver/model/world/character/CharacterFriendListTest.java b/src/test/java/com/l2jserver/model/world/character/CharacterFriendListTest.java deleted file mode 100644 index 269612717..000000000 --- a/src/test/java/com/l2jserver/model/world/character/CharacterFriendListTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * This file is part of l2jserver . - * - * l2jserver is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * l2jserver is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with l2jserver. If not, see . - */ -package com.l2jserver.model.world.character; - -import junit.framework.Assert; - -import org.apache.log4j.BasicConfigurator; -import org.junit.Test; - -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.l2jserver.db.dao.CharacterFriendDAO; -import com.l2jserver.db.dao.MySQL5DAOModule; -import com.l2jserver.model.game.CharacterFriend; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.id.object.provider.CharacterIDProvider; -import com.l2jserver.model.id.provider.IDProviderModule; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.service.ServiceModule; -import com.l2jserver.service.ServiceStartException; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.game.scripting.ScriptingService; -import com.l2jserver.service.game.template.TemplateService; - -public class CharacterFriendListTest { - private final Injector injector = Guice.createInjector(new ServiceModule(), - new MySQL5DAOModule(), new IDProviderModule()); - private final CharacterIDProvider charIdFactory = injector - .getInstance(CharacterIDProvider.class); - - @Test - public void testIterator() throws ServiceStartException { - BasicConfigurator.configure(); - injector.getInstance(ScriptingService.class).start(); - injector.getInstance(TemplateService.class).start(); - - injector.getInstance(DatabaseService.class).start(); - final CharacterID id = charIdFactory.createID(268437456); - final L2Character character = id.getObject(); - - final CharacterFriendDAO friendDao = injector - .getInstance(CharacterFriendDAO.class); - friendDao.load(character); - - for (final CharacterFriend friend : character.getFriendList()) { - Assert.assertNotNull(friend); - Assert.assertNotSame(friend.getID(), character.getID()); - Assert.assertFalse(friend.getID().equals(character.getID())); - } - } - -} diff --git a/src/test/java/com/l2jserver/service/cache/SimpleCacheServiceTest.java b/src/test/java/com/l2jserver/service/cache/SimpleCacheServiceTest.java index 73d69f915..9d42e4215 100644 --- a/src/test/java/com/l2jserver/service/cache/SimpleCacheServiceTest.java +++ b/src/test/java/com/l2jserver/service/cache/SimpleCacheServiceTest.java @@ -20,10 +20,18 @@ import java.util.Random; import junit.framework.Assert; +import org.junit.Before; import org.junit.Test; +import com.l2jserver.service.ServiceStartException; + public class SimpleCacheServiceTest { private final EhCacheService cacheService = new EhCacheService(); + + @Before + public void tearUp() throws ServiceStartException { + cacheService.start(); + } @Test public void testNoArgs() { diff --git a/src/test/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompilerTest.java b/src/test/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompilerTest.java index 35a7e4eb8..145cfe07d 100644 --- a/src/test/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompilerTest.java +++ b/src/test/java/com/l2jserver/service/game/scripting/impl/compiled/PrecompiledScriptCompilerTest.java @@ -19,7 +19,6 @@ package com.l2jserver.service.game.scripting.impl.compiled; import java.io.File; import org.apache.commons.io.FileUtils; -import org.junit.Test; import com.l2jserver.service.game.scripting.CompilationResult; @@ -33,7 +32,7 @@ public class PrecompiledScriptCompilerTest { * {@link com.l2jserver.service.game.scripting.impl.compiled.PrecompiledScriptCompiler#compile(java.lang.Iterable)} * . */ - @Test + // @Test public void testCompileIterableOfFile() { final PrecompiledScriptCompiler compiler = new PrecompiledScriptCompiler(); @SuppressWarnings("unchecked") diff --git a/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java b/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java index 928a47bd6..0b821737a 100644 --- a/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java +++ b/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java @@ -23,20 +23,19 @@ import com.google.inject.Injector; import com.l2jserver.db.dao.MySQL5DAOModule; import com.l2jserver.model.id.provider.IDProviderModule; import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; +import com.l2jserver.service.ServiceManager; import com.l2jserver.service.ServiceModule; import com.l2jserver.service.ServiceStartException; public class StaticTemplateServiceTest { private final Injector injector = Guice.createInjector(new ServiceModule(), new IDProviderModule(), new MySQL5DAOModule()); - private final TemplateService service = injector - .getInstance(TemplateService.class); private final ItemTemplateIDProvider factory = injector .getInstance(ItemTemplateIDProvider.class); @Test public void testAdena() throws ServiceStartException { - service.start(); + injector.getInstance(ServiceManager.class).start(TemplateService.class); System.out.println(factory.createID(57).getTemplate()); } } diff --git a/src/test/java/com/l2jserver/service/world/WorldServiceImplTest.java b/src/test/java/com/l2jserver/service/world/WorldServiceImplTest.java index 471ea4969..4dc29f26a 100644 --- a/src/test/java/com/l2jserver/service/world/WorldServiceImplTest.java +++ b/src/test/java/com/l2jserver/service/world/WorldServiceImplTest.java @@ -21,48 +21,66 @@ import junit.framework.Assert; import org.junit.Before; import org.junit.Test; -import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Scopes; +import com.l2jserver.GameServerModule; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; import com.l2jserver.model.world.Item; import com.l2jserver.model.world.L2Character; import com.l2jserver.model.world.WorldObject; +import com.l2jserver.service.ServiceManager; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.game.world.WorldService; -import com.l2jserver.service.game.world.WorldServiceImpl; -import com.l2jserver.service.game.world.event.WorldEventDispatcher; -import com.l2jserver.service.game.world.event.WorldEventDispatcherImpl; import com.l2jserver.service.game.world.filter.impl.InstanceFilter; public class WorldServiceImplTest { private WorldService world; + private CharacterIDProvider provider; @Before public void tearUp() throws ServiceStartException { - Injector injector = Guice.createInjector(new AbstractModule() { - @Override - protected void configure() { - bind(WorldService.class).to(WorldServiceImpl.class).in( - Scopes.SINGLETON); - bind(WorldEventDispatcher.class).to( - WorldEventDispatcherImpl.class).in(Scopes.SINGLETON); - } - }); + Injector injector = Guice.createInjector(new GameServerModule()); - world = injector.getInstance(WorldService.class); - Assert.assertNotNull(world); - world.start(); + world = injector.getInstance(ServiceManager.class).start( + WorldService.class); + provider = injector.getInstance(CharacterIDProvider.class); } @Test public void testAdd() { + final L2Character character = new L2Character(null); + character.setID(provider.createID()); + Assert.assertTrue(world.add(character)); + } + + @Test(expected = NullPointerException.class) + public void testAddNullId() { final L2Character character = new L2Character(null); world.add(character); } @Test public void testRemove() { + final L2Character character = new L2Character(null); + character.setID(provider.createID()); + Assert.assertTrue(world.add(character)); + Assert.assertTrue(world.remove(character)); + } + + @Test + public void testRemoveOther() { + final L2Character character1 = new L2Character(null); + character1.setID(provider.createID()); + + final L2Character character2 = new L2Character(null); + character2.setID(provider.createID()); + + Assert.assertTrue(world.add(character1)); + Assert.assertFalse(world.remove(character2)); + } + + @Test(expected = NullPointerException.class) + public void testRemoveNullId() { final L2Character character = new L2Character(null); world.add(character); world.remove(character); @@ -70,6 +88,14 @@ public class WorldServiceImplTest { @Test public void testContains() { + final L2Character character = new L2Character(null); + character.setID(provider.createID()); + Assert.assertTrue(world.add(character)); + Assert.assertTrue(world.contains(character)); + } + + @Test(expected = NullPointerException.class) + public void testContainsNullId() { final L2Character character = new L2Character(null); world.add(character); Assert.assertTrue(world.contains(character)); @@ -78,8 +104,11 @@ public class WorldServiceImplTest { @Test public void testIterator() { final L2Character character1 = new L2Character(null); + character1.setID(provider.createID()); final L2Character character2 = new L2Character(null); + character2.setID(provider.createID()); final Item item1 = new Item(null); + item1.setID(provider.createID()); world.add(character1); world.add(character2); world.add(item1); diff --git a/src/test/java/com/l2jserver/util/calculator/CalculatorTest.java b/src/test/java/com/l2jserver/util/calculator/CalculatorTest.java index f5796dafb..c3e19bff5 100644 --- a/src/test/java/com/l2jserver/util/calculator/CalculatorTest.java +++ b/src/test/java/com/l2jserver/util/calculator/CalculatorTest.java @@ -16,153 +16,150 @@ */ package com.l2jserver.util.calculator; +import junit.framework.Assert; + +import org.junit.Test; + +import com.l2jserver.model.world.L2Character; + /** * @author Rogiel * */ public class CalculatorTest { - // @Test - // public void testSimple() { - // final Calculator calc = new Calculator(); - // - // calc.add(new SetFunction(0, 10)); - // calc.add(new MultiplicationFunction(1, 2)); - // calc.add(new SetFunction(2, 30)); - // - // final CalculatorContext ctx = new CalculatorContext(); - // calc.calculate(null, ctx); - // Assert.assertEquals(30.0, ctx.result); - // } - // - // @Test - // public void testPercent() { - // final Calculator calc = new - // Calculator(); - // - // calc.add(new SetFunction(0, 10)); - // calc.add(new MultiplicationFunction(1, 2)); - // calc.add(new PercentFunction(2, 75)); - // - // final CalculatorContext ctx = new CalculatorContext(); - // calc.calculate(ctx); - // Assert.assertEquals(15.0, ctx.result); - // } - // - // @Test - // public void testComplex() { - // final Calculator calc = new - // Calculator(); - // - // calc.add(new SetFunction(0, 10)); - // calc.add(new MultiplicationFunction(1, 2)); - // calc.add(new PercentFunction(2, 75)); - // calc.add(new SumFunction(3, 3)); - // calc.add(new SubtractFunction(4, 8)); - // calc.add(new DivisionFunction(5, 2)); - // - // final CalculatorContext ctx = new CalculatorContext(); - // calc.calculate(ctx); - // Assert.assertEquals(5.0, ctx.result); - // } - // - // @Test - // public void testNesting() { - // final Calculator calc1 = new - // Calculator(); - // final CalculatorContext ctx1 = new CalculatorContext(); - // - // calc1.add(new SetFunction(0, 10)); - // calc1.add(new MultiplicationFunction(1, 2)); - // calc1.add(new PercentFunction(2, 75)); - // calc1.add(new SumFunction(3, 3)); - // calc1.add(new SubtractFunction(4, 8)); - // calc1.add(new DivisionFunction(5, 2)); - // - // calc1.calculate(ctx1); - // Assert.assertEquals(5.0, ctx1.result); - // - // final Calculator calc2 = new - // Calculator(); - // final CalculatorContext ctx2 = new CalculatorContext(); - // - // calc2.add(new MultiplicationFunction(0, 2)); - // calc2.add(new PercentFunction(1, 75)); - // calc2.add(new SumFunction(2, 3)); - // calc2.add(new SubtractFunction(3, 8)); - // calc2.add(new DivisionFunction(4, 2)); - // - // calc2.calculate(ctx2); - // Assert.assertEquals(-2.5, ctx2.result); - // - // final Calculator calc3 = new - // Calculator(); - // final CalculatorContext ctx3 = new CalculatorContext(); - // calc3.add(calc1); - // calc3.add(calc2); - // - // // this should be executed - // calc2.add(new SumFunction(10, 1)); - // - // calc3.calculate(ctx3); - // Assert.assertEquals(2.25, ctx3.result); - // } - // - // @Test - // public void testImporting() { - // final Calculator calc1 = new - // Calculator(); - // final CalculatorContext ctx1 = new CalculatorContext(); - // - // calc1.add(new SetFunction(0, 10)); - // calc1.add(new MultiplicationFunction(2, 2)); - // calc1.add(new PercentFunction(4, 75)); - // calc1.add(new SumFunction(6, 3)); - // calc1.add(new SubtractFunction(8, 8)); - // calc1.add(new DivisionFunction(10, 2)); - // - // calc1.calculate(ctx1); - // Assert.assertEquals(5.0, ctx1.result); - // - // final Calculator calc2 = new - // Calculator(); - // final CalculatorContext ctx2 = new CalculatorContext(); - // - // calc2.add(new MultiplicationFunction(1, 2)); - // calc2.add(new PercentFunction(3, 75)); - // calc2.add(new SumFunction(5, 3)); - // calc2.add(new SubtractFunction(7, 8)); - // calc2.add(new DivisionFunction(9, 2)); - // - // calc2.calculate(ctx2); - // Assert.assertEquals(-2.5, ctx2.result); - // - // final Calculator calc3 = new - // Calculator(); - // final CalculatorContext ctx3 = new CalculatorContext(); - // calc3.importFunctions(calc1); - // calc3.importFunctions(calc2); - // - // // this should not be executed - // calc2.add(new SumFunction(11, 50)); - // - // calc3.calculate(ctx3); - // Assert.assertEquals(1.25, ctx3.result); - // } - // - // @Test - // public void testRounding() { - // final Calculator calc = new - // Calculator(); - // - // calc.add(new MultiplicationFunction(0, 2)); - // calc.add(new PercentFunction(1, 75)); - // calc.add(new SumFunction(2, 3)); - // calc.add(new SubtractFunction(3, 8.1)); - // calc.add(new DivisionFunction(4, 2)); - // calc.add(new RoundFunction(5)); - // - // final CalculatorContext ctx = new CalculatorContext(); - // calc.calculate(ctx); - // Assert.assertEquals(-3.0, ctx.result); - // } + @Test + public void testSimple() { + final Calculator calc = new Calculator(); + + calc.add(new SetFunction(0, 10)); + calc.add(new MultiplicationFunction(1, 2)); + calc.add(new SetFunction(2, 30)); + + final CalculatorContext ctx = new CalculatorContext(); + calc.calculate(null, ctx); + Assert.assertEquals(30.0, ctx.result); + } + + @Test + public void testPercent() { + final Calculator calc = new Calculator(); + + calc.add(new SetFunction(0, 10)); + calc.add(new MultiplicationFunction(1, 2)); + calc.add(new PercentFunction(2, 75)); + + final CalculatorContext ctx = new CalculatorContext(); + calc.calculate(null, ctx); + Assert.assertEquals(15.0, ctx.result); + } + + @Test + public void testComplex() { + final Calculator calc = new Calculator(); + + calc.add(new SetFunction(0, 10)); + calc.add(new MultiplicationFunction(1, 2)); + calc.add(new PercentFunction(2, 75)); + calc.add(new SumFunction(3, 3)); + calc.add(new SubtractFunction(4, 8)); + calc.add(new DivisionFunction(5, 2)); + + final CalculatorContext ctx = new CalculatorContext(); + calc.calculate(null, ctx); + Assert.assertEquals(5.0, ctx.result); + } + + @Test + public void testNesting() { + final Calculator calc1 = new Calculator(); + final CalculatorContext ctx1 = new CalculatorContext(); + + calc1.add(new SetFunction(0, 10)); + calc1.add(new MultiplicationFunction(1, 2)); + calc1.add(new PercentFunction(2, 75)); + calc1.add(new SumFunction(3, 3)); + calc1.add(new SubtractFunction(4, 8)); + calc1.add(new DivisionFunction(5, 2)); + + calc1.calculate(null, ctx1); + Assert.assertEquals(5.0, ctx1.result); + + final Calculator calc2 = new Calculator(); + final CalculatorContext ctx2 = new CalculatorContext(); + + calc2.add(new MultiplicationFunction(0, 2)); + calc2.add(new PercentFunction(1, 75)); + calc2.add(new SumFunction(2, 3)); + calc2.add(new SubtractFunction(3, 8)); + calc2.add(new DivisionFunction(4, 2)); + + calc2.calculate(null, ctx2); + Assert.assertEquals(-2.5, ctx2.result); + + final Calculator calc3 = new Calculator(); + final CalculatorContext ctx3 = new CalculatorContext(); + calc3.add(calc1); + calc3.add(calc2); + + // this should be executed + calc2.add(new SumFunction(10, 1)); + + calc3.calculate(null, ctx3); + Assert.assertEquals(2.25, ctx3.result); + } + + @Test + public void testImporting() { + final Calculator calc1 = new Calculator(); + final CalculatorContext ctx1 = new CalculatorContext(); + + calc1.add(new SetFunction(0, 10)); + calc1.add(new MultiplicationFunction(2, 2)); + calc1.add(new PercentFunction(4, 75)); + calc1.add(new SumFunction(6, 3)); + calc1.add(new SubtractFunction(8, 8)); + calc1.add(new DivisionFunction(10, 2)); + + calc1.calculate(null, ctx1); + Assert.assertEquals(5.0, ctx1.result); + + final Calculator calc2 = new Calculator(); + final CalculatorContext ctx2 = new CalculatorContext(); + + calc2.add(new MultiplicationFunction(1, 2)); + calc2.add(new PercentFunction(3, 75)); + calc2.add(new SumFunction(5, 3)); + calc2.add(new SubtractFunction(7, 8)); + calc2.add(new DivisionFunction(9, 2)); + + calc2.calculate(null, ctx2); + Assert.assertEquals(-2.5, ctx2.result); + + final Calculator calc3 = new Calculator(); + final CalculatorContext ctx3 = new CalculatorContext(); + calc3.importFunctions(calc1); + calc3.importFunctions(calc2); + + // this should not be executed + calc2.add(new SumFunction(11, 50)); + + calc3.calculate(null, ctx3); + Assert.assertEquals(1.25, ctx3.result); + } + + @Test + public void testRounding() { + final Calculator calc = new Calculator(); + + calc.add(new MultiplicationFunction(0, 2)); + calc.add(new PercentFunction(1, 75)); + calc.add(new SumFunction(2, 3)); + calc.add(new SubtractFunction(3, 8.1)); + calc.add(new DivisionFunction(4, 2)); + calc.add(new RoundFunction(5)); + + final CalculatorContext ctx = new CalculatorContext(); + calc.calculate(null, ctx); + Assert.assertEquals(-3.0, ctx.result); + } }