001    package org.cocome.tradingsystem.testdriver;
002    
003    import javax.jms.JMSException;
004    import javax.naming.NamingException;
005    
006    import org.cocome.tradingsystem.systests.interfaces.IBank;
007    import org.cocome.tradingsystem.systests.interfaces.ICashDesk;
008    import org.cocome.tradingsystem.systests.interfaces.IEnterprise;
009    import org.cocome.tradingsystem.systests.interfaces.IStorePC;
010    import org.cocome.tradingsystem.systests.interfaces.ITestDriver;
011    
012    /**
013     * This is the test driver for the reference implementation of the system.
014     * 
015     * @author Benjamin Hummel
016     * @author $Author: hummel $
017     * @version $Rev: 63 $
018     * @levd.rating GREEN Rev: 63
019     */
020    public class TestDriver implements ITestDriver {
021    
022            /** The bank we are using. */
023            private Bank bank;
024    
025            /** The next used store id (= the number of stores created so far). */
026            private int currentStore = 0;
027    
028            /**
029             * The maximal number of stores supported (as prepared in the properties
030             * files).
031             */
032            private static final int MAX_STORES = 3;
033    
034            /** The preloaded store PCs. */
035            private StorePC[] storePCs = new StorePC[MAX_STORES];
036    
037            /**
038             * For each store the next used cash desk id (= number of cash desks of the
039             * store).
040             */
041            private final int[] currentCashdesk = new int[MAX_STORES];
042    
043            /**
044             * The maximal number of cash desks supported (as prepared in the properties
045             * files).
046             */
047            private static final int MAX_CASHDESKS = 3;
048    
049            /** The enterprise. */
050            private Enterprise enterprise = null;
051    
052            /** {@inheritDoc} */
053            public ICashDesk createCashDesk(IStorePC store) throws NamingException,
054                            JMSException {
055                    StorePC storePC = (StorePC) store;
056                    int storeIndex = storePC.getIndex();
057                    if (storeIndex > 0) {
058                            throw new IllegalStateException(
059                                            "Cash desks are only available for the first store (0)!");
060                    }
061                    if (currentCashdesk[storeIndex] >= MAX_CASHDESKS) {
062                            throw new IllegalStateException(
063                                            "Maximal number of cash desks for store " + storeIndex
064                                                            + " already created!");
065                    }
066                    return new CashDesk(storePC, currentCashdesk[storeIndex]++);
067            }
068    
069            /** {@inheritDoc} */
070            public IStorePC createStore() {
071                    if (currentStore >= MAX_STORES) {
072                            throw new IllegalStateException(
073                                            "Maximal number of stores already created!");
074                    }
075                    return storePCs[currentStore++];
076            }
077    
078            /** {@inheritDoc} */
079            public IEnterprise initializeSystem() throws Exception {
080                    enterprise = new Enterprise();
081                    bank = new Bank();
082    
083                    // precreate stores (so they always get the same store id)
084                    for (int i = 0; i < MAX_STORES; ++i) {
085                            storePCs[i] = new StorePC(i, enterprise);
086                    }
087    
088                    return enterprise;
089            }
090    
091            /** {@inheritDoc} */
092            public void shutdownSystem() {
093                    enterprise = null;
094                    try {
095                            bank.unregister();
096                    } catch (Exception e) {
097                            // nothing we could handle here
098                    }
099                    bank = null;
100            }
101    
102            /** {@inheritDoc} */
103            public IBank getBank() throws Exception {
104                    return bank;
105            }
106    }