Archived
0

Create repository

This commit is contained in:
iMoHax
2014-07-16 16:38:48 +04:00
committed by iMoHax
commit 15bfe2e0f3
79 changed files with 5741 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
package ru.trader;
import org.junit.Assert;
import java.util.Collection;
import java.util.Iterator;
public class TestUtil {
@SafeVarargs
public static <T> void assertCollectionEquals(Collection<T> collection, T... items){
assertSize(collection, items);
int curIndx=0;
for (T actual : collection) {
if (!actual.equals(items[curIndx])){
Assert.fail(String.format("Entry by index %d is different. Expected: %s Actual: %s", curIndx, items[curIndx], actual));
return;
}
curIndx++;
}
}
private static <T> void assertSize(Collection<T> collection, T[] items){
int expectedSize = items.length;
int actualSize = collection.size();
if (actualSize!=expectedSize) {
Assert.fail(String.format("Collection size differed. Expected: %d Actual: %d", expectedSize, actualSize));
}
}
private static <T> void assertSize(Iterable<T> collection, T[] items){
int expectedSize = items.length;
Iterator<T> iterator = collection.iterator();
int actualSize=0;
while (iterator.hasNext()){actualSize++;iterator.next();}
if (actualSize!=expectedSize) {
Assert.fail(String.format("Collection size differed. Expected: %d Actual: %d", expectedSize, actualSize));
}
}
private static <T> void checkContains(Collection<T> collection, boolean all, T[] items){
if (all) assertSize(collection, items);
for (T item : items) {
if (!collection.contains(item)){
Assert.fail(String.format("Collection should include an item %s", item));
return;
}
}
}
private static <T> boolean contains(Iterable<T> collection, T item){
for (T t : collection) {
if (item.equals(t)) return true;
}
return false;
}
private static <T> void checkContains(Iterable<T> collection, boolean all, T[] items){
if (all) assertSize(collection, items);
for (T item : items) {
if (!contains(collection, item)){
Assert.fail(String.format("Collection should include an item %s", item));
return;
}
}
}
@SafeVarargs
public static <T> void assertCollectionNoContain(Collection<T> collection, T... items){
for (T item : items) {
if (collection.contains(item)){
Assert.fail(String.format("Collection must not contain item %s", item));
return;
}
}
}
@SafeVarargs
public static <T> void assertCollectionContain(Collection<T> collection, T... items){
checkContains(collection, false, items);
}
@SafeVarargs
public static <T> void assertCollectionContainAll(Collection<T> collection, T... items){
checkContains(collection, true, items);
}
@SafeVarargs
public static <T> void assertIterableContain(Iterable<T> collection, T... items){
checkContains(collection, false, items);
}
@SafeVarargs
public static <T> void assertIterableContainAll(Iterable<T> collection, T... items){
checkContains(collection, true, items);
}
}

View File

@@ -0,0 +1,43 @@
package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ItemStatTest extends Assert {
private final static Item ITEM1 = new Item("Item1");
private ItemStat itemSellStat = new ItemStat(ITEM1, OFFER_TYPE.SELL);
private ItemStat itemBuyStat = new ItemStat(ITEM1, OFFER_TYPE.BUY);
@Before
public void fill(){
itemSellStat.put(new Offer(OFFER_TYPE.SELL, ITEM1, 10));
itemSellStat.put(new Offer(OFFER_TYPE.SELL, ITEM1, 20));
itemSellStat.put(new Offer(OFFER_TYPE.SELL, ITEM1, 30));
itemSellStat.put(new Offer(OFFER_TYPE.SELL, ITEM1, 40));
itemBuyStat.put(new Offer(OFFER_TYPE.BUY, ITEM1, 100));
itemBuyStat.put(new Offer(OFFER_TYPE.BUY, ITEM1, 200));
itemBuyStat.put(new Offer(OFFER_TYPE.BUY, ITEM1, 300));
itemBuyStat.put(new Offer(OFFER_TYPE.BUY, ITEM1, 400));
}
@Test
public void testSell(){
assertEquals(itemSellStat.getAvg(), (10+20+30+40)/4, 0);
assertEquals(itemSellStat.getBest().getPrice(), 10d, 0);
}
@Test
public void testBuy(){
assertEquals(itemBuyStat.getAvg(), (100+200+300+400)/4, 0);
assertEquals(itemBuyStat.getBest().getPrice(), 400d, 0);
}
}

View File

@@ -0,0 +1,84 @@
package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class MarketTest1 extends Assert {
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Item ITEM3 = new Item("Item3");
private final static Offer bestSellOffer1 = new Offer(OFFER_TYPE.SELL,ITEM1,10);
private final static Offer bestSellOffer2 = new Offer(OFFER_TYPE.SELL,ITEM2,15);
private final static Offer bestSellOffer3 = new Offer(OFFER_TYPE.SELL,ITEM3,20);
private final static Offer bestBuyOffer1 = new Offer(OFFER_TYPE.BUY,ITEM1,100);
private final static Offer bestBuyOffer2 = new Offer(OFFER_TYPE.BUY,ITEM2,200);
private final static Offer bestBuyOffer3 = new Offer(OFFER_TYPE.BUY,ITEM3,100);
private final static Offer bestBuyOffer4 = new Offer(OFFER_TYPE.BUY,ITEM2,150);
private final static Vendor sellVendor1 = new SimpleVendor();
private final static Vendor sellVendor2 = new SimpleVendor();
private final static Vendor sellVendor3 = new SimpleVendor();
private final static Vendor sellVendor4 = new SimpleVendor();
private final static Vendor buyVendor1 = new SimpleVendor();
private final static Vendor buyVendor2 = new SimpleVendor();
private final static Vendor buyVendor3 = new SimpleVendor();
private final static Vendor buyVendor4 = new SimpleVendor();
private Market market;
@Before
public void fillMarket(){
sellVendor1.add(bestSellOffer1);
sellVendor1.add(new Offer(OFFER_TYPE.SELL,ITEM2,100));
sellVendor2.add(new Offer(OFFER_TYPE.SELL,ITEM3,200));
sellVendor2.add(bestSellOffer2);
sellVendor3.add(new Offer(OFFER_TYPE.SELL,ITEM1,300));
sellVendor3.add(new Offer(OFFER_TYPE.SELL,ITEM2,300));
sellVendor3.add(bestSellOffer3);
sellVendor4.add(new Offer(OFFER_TYPE.SELL,ITEM2,150));
buyVendor1.add(new Offer(OFFER_TYPE.BUY,ITEM2,50));
buyVendor1.add(bestBuyOffer1);
buyVendor2.add(new Offer(OFFER_TYPE.BUY,ITEM1,40));
buyVendor2.add(bestBuyOffer2);
buyVendor2.add(new Offer(OFFER_TYPE.BUY,ITEM3,50));
buyVendor3.add(bestBuyOffer3);
buyVendor3.add(new Offer(OFFER_TYPE.BUY,ITEM2,20));
buyVendor4.add(new Offer(OFFER_TYPE.BUY,ITEM1,80));
buyVendor4.add(bestBuyOffer4);
market = new SimpleMarket();
market.add(sellVendor1);
market.add(sellVendor2);
market.add(sellVendor3);
market.add(sellVendor4);
market.add(buyVendor1);
market.add(buyVendor2);
market.add(buyVendor3);
market.add(buyVendor4);
}
@Test
public void testBestSell(){
Offer test = market.getStatSell(ITEM1).getBest();
assertEquals(test, bestSellOffer1);
test = market.getStatSell(ITEM2).getBest();
assertEquals(test, bestSellOffer2);
test = market.getStatSell(ITEM3).getBest();
assertEquals(test, bestSellOffer3);
}
@Test
public void testBestBuy(){
Offer test = market.getStatBuy(ITEM1).getBest();
assertEquals(test, bestBuyOffer1);
test = market.getStatBuy(ITEM2).getBest();
assertEquals(test, bestBuyOffer2);
test = market.getStatBuy(ITEM3).getBest();
assertEquals(test, bestBuyOffer3);
}
}

View File

@@ -0,0 +1,86 @@
package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class VendorTest extends Assert {
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Offer SELL_OFFER = new Offer(OFFER_TYPE.SELL, ITEM1, 10);
private final static Offer BUY_OFFER = new Offer(OFFER_TYPE.BUY, ITEM1, 10);
private Vendor sellVendor;
private Vendor buyVendor;
@Before
public void fillVendor(){
sellVendor = new SimpleVendor();
sellVendor.add(SELL_OFFER);
buyVendor = new SimpleVendor();
buyVendor.add(BUY_OFFER);
}
@Test
public void testAddSellOffer(){
final Iterable<Offer> offers = sellVendor.getAllSellOffers();
Offer test = offers.iterator().next();
assertEquals(test, SELL_OFFER);
}
@Test
public void testAddSellOffer2(){
final Iterable<Offer> offers = sellVendor.getAllBuyOffers();
assertFalse(offers.iterator().hasNext());
}
@Test
public void testAddBuyOffer(){
final Iterable<Offer> offers = buyVendor.getAllSellOffers();
assertFalse(offers.iterator().hasNext());
}
@Test
public void testAddBuyOffer2(){
final Iterable<Offer> offers = buyVendor.getAllBuyOffers();
Offer test = offers.iterator().next();
assertEquals(test, BUY_OFFER);
}
@Test
public void testGetSellOfferOnSellVendor(){
Offer test = sellVendor.getSell(ITEM1);
assertEquals(test, SELL_OFFER);
}
@Test
public void testGetSellOfferOnBuyVendor(){
Offer test = buyVendor.getSell(ITEM1);
assertNull(test);
}
@Test
public void testGetWrongItemSellOfferOnSellVendor(){
Offer test = buyVendor.getSell(ITEM2);
assertNull(test);
}
@Test
public void testGetBuyOfferOnSellVendor(){
Offer test = sellVendor.getBuy(ITEM1);
assertNull(test);
}
@Test
public void testGetBuyOffersOnBuyVendor(){
Offer test = buyVendor.getBuy(ITEM1);
assertEquals(test, BUY_OFFER);
}
@Test
public void testGetWrongItemBuyOfferOnBuyVendor(){
Offer test = sellVendor.getBuy(ITEM2);
assertNull(test);
}
}

View File

@@ -0,0 +1,66 @@
package ru.trader.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import ru.trader.TestUtil;
import java.util.Collection;
public class VendorTest2 extends Assert {
private final static Item ITEM1 = new Item("Item1");
private final static Item ITEM2 = new Item("Item2");
private final static Item ITEM3 = new Item("Item3");
private final static Offer SELL_OFFER1 = new Offer(OFFER_TYPE.SELL, ITEM1, 10);
private final static Offer SELL_OFFER2 = new Offer(OFFER_TYPE.SELL, ITEM2, 10);
private final static Offer SELL_OFFER3 = new Offer(OFFER_TYPE.SELL, ITEM3, 10);
private final static Offer DUBLE_SELL_OFFER1 = new Offer(OFFER_TYPE.SELL, ITEM1, 100);
private final static Offer BUY_OFFER1 = new Offer(OFFER_TYPE.BUY, ITEM1, 100);
private final static Offer BUY_OFFER2 = new Offer(OFFER_TYPE.BUY, ITEM2, 10);
private final static Offer BUY_OFFER3 = new Offer(OFFER_TYPE.BUY, ITEM3, 10);
private final static Offer DUBLE_BUY_OFFER1 = new Offer(OFFER_TYPE.BUY, ITEM1, 10);
private Vendor sellVendor;
private Vendor buyVendor;
@Before
public void fillVendor(){
sellVendor = new SimpleVendor();
sellVendor.add(SELL_OFFER1);
sellVendor.add(SELL_OFFER2);
sellVendor.add(SELL_OFFER3);
sellVendor.add(DUBLE_SELL_OFFER1);
buyVendor = new SimpleVendor();
buyVendor.add(BUY_OFFER1);
buyVendor.add(BUY_OFFER2);
buyVendor.add(BUY_OFFER3);
buyVendor.add(DUBLE_BUY_OFFER1);
}
@Test
public void testGetSellOffer(){
final Offer test = sellVendor.getSell(ITEM1);
assertEquals(test, DUBLE_SELL_OFFER1);
}
@Test
public void testGetBuyOffer(){
final Offer test = buyVendor.getBuy(ITEM1);
assertEquals(test, DUBLE_BUY_OFFER1);
}
@Test
public void testGetAllSellOffer(){
final Collection<Offer> test = sellVendor.getAllSellOffers();
TestUtil.assertCollectionContainAll(test, DUBLE_SELL_OFFER1, SELL_OFFER2, SELL_OFFER3);
}
@Test
public void testGetAllBuyOffer(){
final Collection<Offer> test = buyVendor.getAllBuyOffers();
TestUtil.assertCollectionContainAll(test, DUBLE_BUY_OFFER1, BUY_OFFER3, BUY_OFFER2);
}
}

View File

@@ -0,0 +1,26 @@
package ru.trader.store;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
import ru.trader.core.Market;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
public class LoadTest extends Assert {
@Test
public void testLoad(){
InputStream is = getClass().getResourceAsStream("/test.xml");
Market world;
try {
world = Store.loadFromFile(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new AssertionError(e);
}
assertNotNull(world);
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<market xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "../../main/resources/store/trader.xsd">
<items>
<item name="Item 1" id="i1"/>
<item name="Item 2" id="i2"/>
<item name="Item 3" id="i3"/>
<item name="Item 4" id="i4"/>
<item name="Item 5" id="i5"/>
<item name="Item 6" id="i6"/>
</items>
<vendors>
<vendor name="Vendor 1">
<offer item="i1" type="SELL" price="140"/>
</vendor>
</vendors>
</market>