From 571b8be7ee44439907a39aa8d696acdd37f75a5a Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sat, 8 Dec 2018 15:00:35 +0100 Subject: [PATCH] Test for Account class --- nbproject/project.properties | 2 +- src/opensesim/sesim/Assets/DummyAsset.java | 53 ++++++++ src/opensesim/util/Scollection.java | 7 +- src/opensesim/world/AbstractAsset.java | 23 +++- src/opensesim/world/Account.java | 28 +++-- .../world/{AssetVol.java => AssetPack.java} | 10 +- src/opensesim/world/DefaultFeeModel.java | 4 +- test/opensesim/world/AccountTest.java | 118 ++++++++++++++++++ test/opensesim/world/ExchangeTest.java | 4 +- 9 files changed, 221 insertions(+), 28 deletions(-) create mode 100644 src/opensesim/sesim/Assets/DummyAsset.java rename src/opensesim/world/{AssetVol.java => AssetPack.java} (88%) create mode 100644 test/opensesim/world/AccountTest.java diff --git a/nbproject/project.properties b/nbproject/project.properties index 1a73840..10e74de 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Sat, 08 Dec 2018 12:17:24 +0100 +#Sat, 08 Dec 2018 14:53:59 +0100 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/opensesim/sesim/Assets/DummyAsset.java b/src/opensesim/sesim/Assets/DummyAsset.java new file mode 100644 index 0000000..40c6b23 --- /dev/null +++ b/src/opensesim/sesim/Assets/DummyAsset.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package opensesim.sesim.Assets; + +import opensesim.world.AbstractAsset; +import opensesim.world.GodWorld; +import org.json.JSONObject; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class DummyAsset extends AbstractAsset { + + @Override + public String getTypeName() { + return "DummyAsset"; + } + + DummyAsset(GodWorld gw, JSONObject cfg) { + super(gw, cfg); + } + + public DummyAsset(String symbol, String name, int decimals) { + setSymbol(symbol); + setDecimals(decimals); + setName(name); + } + +} diff --git a/src/opensesim/util/Scollection.java b/src/opensesim/util/Scollection.java index 8033dc7..03187b5 100644 --- a/src/opensesim/util/Scollection.java +++ b/src/opensesim/util/Scollection.java @@ -37,16 +37,17 @@ import java.util.HashSet; */ public class Scollection { - HashSet byObj = new HashSet<>(); + // HashSet byObj = new HashSet<>(); HashMap byKey = new HashMap<>(); public void add(KEY key, OBJ obj) { - byObj.add(obj); + // byObj.add(obj); byKey.put(key, obj); } public Collection getCollection(){ - return Collections.unmodifiableCollection(byObj); + + return Collections.unmodifiableCollection(byKey.values()); } public OBJ get(KEY key){ diff --git a/src/opensesim/world/AbstractAsset.java b/src/opensesim/world/AbstractAsset.java index aeffa82..5c9f77c 100644 --- a/src/opensesim/world/AbstractAsset.java +++ b/src/opensesim/world/AbstractAsset.java @@ -66,24 +66,41 @@ public abstract class AbstractAsset implements GetJson { this.world = world; } + + + public AbstractAsset(){ + + } public abstract String getTypeName(); - public int getDecimals() { + public final int getDecimals() { return decimals; } + + protected final void setDecimals(int decimals){ + this.decimals=decimals; + } protected void setDescription(String description) { this.description = description; } - public String getSymbol() { + public final String getSymbol() { return symbol; } + + protected final void setSymbol(String symbol){ + this.symbol=symbol; + } - public String getName() { + public final String getName() { return name; } + + protected final void setName(String name){ + this.name=name; + } public String getDescription() { return description; diff --git a/src/opensesim/world/Account.java b/src/opensesim/world/Account.java index cf62edd..80227d6 100644 --- a/src/opensesim/world/Account.java +++ b/src/opensesim/world/Account.java @@ -25,13 +25,9 @@ */ package opensesim.world; -import java.util.Collection; import java.util.Collections; import java.util.Map; -import opensesim.world.Exchange; import java.util.concurrent.ConcurrentHashMap; -import static jdk.vm.ci.code.CodeUtil.K; -import opensesim.sesim.interfaces.Asset; import opensesim.sesim.interfaces.Trader; /** @@ -39,27 +35,33 @@ import opensesim.sesim.interfaces.Trader; * @author 7u83 <7u83@mail.ru> */ public class Account { - ConcurrentHashMap assets; + ConcurrentHashMap assets = new ConcurrentHashMap<>(); Trader owner; Exchange exchange; - public Map getAssets() { - return Collections.unmodifiableMap(assets); + public Map getAssets() { + return Collections.unmodifiableMap(assets); } public Trader getOwner() { return owner; } - - public Exchange getExchange() { - return exchange; - } Account(Exchange exchange){ - assets = new ConcurrentHashMap<>(); + // assets = new ConcurrentHashMap<>(); } - + Account(){ + + } + + void add(AssetPack pack){ + assets.put( pack.asset, get(pack.asset)+pack.volume); + } + + public double get(AbstractAsset asset){ + return assets.getOrDefault(asset, 0.0); + } } diff --git a/src/opensesim/world/AssetVol.java b/src/opensesim/world/AssetPack.java similarity index 88% rename from src/opensesim/world/AssetVol.java rename to src/opensesim/world/AssetPack.java index 5b7e06a..1158f12 100644 --- a/src/opensesim/world/AssetVol.java +++ b/src/opensesim/world/AssetPack.java @@ -25,18 +25,20 @@ */ package opensesim.world; -import opensesim.world.AbstractAsset; - /** * * @author 7u83 <7u83@mail.ru> */ -public class AssetVol { +public class AssetPack { public AbstractAsset asset; public double volume; - public AssetVol(AbstractAsset asset, double volume){ + public AssetPack(AbstractAsset asset, double volume){ this.asset=asset; this.volume=volume; } + + public AssetPack(World world, String symbol,double volume){ + this(world.getAssetBySymbol(symbol),volume); + } } diff --git a/src/opensesim/world/DefaultFeeModel.java b/src/opensesim/world/DefaultFeeModel.java index e3fd05b..792cbdc 100644 --- a/src/opensesim/world/DefaultFeeModel.java +++ b/src/opensesim/world/DefaultFeeModel.java @@ -34,9 +34,9 @@ import opensesim.sesim.interfaces.FeeModel; public class DefaultFeeModel implements FeeModel{ - public AssetVol xgetTakerFee(AssetPair pair) { + public AssetPack xgetTakerFee(AssetPair pair) { double vol = 5; - return new AssetVol(pair.getCurrency(),vol); + return new AssetPack(pair.getCurrency(),vol); } diff --git a/test/opensesim/world/AccountTest.java b/test/opensesim/world/AccountTest.java new file mode 100644 index 0000000..60cee39 --- /dev/null +++ b/test/opensesim/world/AccountTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2018, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package opensesim.world; + +import java.util.Map; +import opensesim.sesim.Assets.CurrencyAsset; +import opensesim.sesim.Assets.DummyAsset; +import opensesim.sesim.interfaces.Trader; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class AccountTest { + + public AccountTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getAssets method, of class Account. + */ + /* @Test + public void testGetAssets() { + System.out.println("getAssets"); + Account instance = new Account(); + Map expResult = null; + Map result = instance.getAssets(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + */ + /** + * Test of getOwner method, of class Account. + */ + @Test + public void testGetOwner() { + System.out.println("getOwner"); + Account instance = new Account(); + Trader expResult = null; + Trader result = instance.getOwner(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of add method, of class Account. + */ + @Test + public void testAdd() { + + System.out.println("add"); + + AbstractAsset c = new DummyAsset("EUR", "Euro", 2); + + Double expResult = 123.0; + AssetPack pack = new AssetPack(c, expResult); + Account account = new Account(); + + account.add(pack); + Double result; + + result = account.get(c); + assertEquals(expResult, result); + + account.add(pack); + result = account.get(c); + expResult *= 2; + assertEquals(expResult, result); + } + +} diff --git a/test/opensesim/world/ExchangeTest.java b/test/opensesim/world/ExchangeTest.java index 500afa8..5b8df21 100644 --- a/test/opensesim/world/ExchangeTest.java +++ b/test/opensesim/world/ExchangeTest.java @@ -94,9 +94,9 @@ public class ExchangeTest { Account result = instance.createAccount(); - assertEquals(expResult, result); + // assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); + // fail("The test case is a prototype."); } }