1   package org.paneris.jammyjoes.mvp;
2   
3   import java.sql.Timestamp;
4   import java.util.Date;
5   import java.util.HashMap;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Vector;
10  
11  import junit.framework.TestCase;
12  
13  import org.melati.poem.Selectable;
14  import org.paneris.jammyjoes.controller.MonthlyTimesliceFactory;
15  import org.paneris.jammyjoes.model.Graphable;
16  import org.paneris.jammyjoes.model.OrderStatus;
17  
18  import com.mockobjects.dynamic.C;
19  import com.mockobjects.dynamic.Mock;
20  
21  public class FufilledOrdersByMonthTest extends TestCase {
22  
23    private static final Date SOMETIME_IN_AUGUST = new Date(1061525758061L);
24  
25    private static final Date SOME_OTHERTIME_IN_SEPTEMBER = new Date(1062525658062L);
26  
27    private static final Date SOMETIME_IN_SEPTEMBER = new Date(1064525758061L);
28  
29    public void testPresenter() {
30  
31      Mock contextControl = makeContext();
32      Context context = (Context) contextControl.proxy();
33  
34      Presenter unit = new OrdersByMonthPresenter(null, context, null);
35      assertEquals(OrderSelection.class, unit.createSelection().getClass());
36      assertEquals(CollectingCommand.class, unit.createCommand().getClass());
37  
38      contextControl.verify();
39  
40    }
41  
42    private Mock makeContext() {
43      Mock contextControl = new Mock(Context.class);
44      contextControl.expect("put", C.args(C.eq("months"), C.isA(Map.class)));
45      contextControl.expect("put", C.args(C.eq("max"), C.isA(TimesliceValue.class)));
46      contextControl.expect("put", C.args(C.eq("sorter"), C.isA(CollectionSorter.class)));
47      contextControl.expect("put", C
48          .args(C.eq("reversesorter"), C.isA(ReverseCollectionSorter.class)));
49      return contextControl;
50    }
51  
52    public void testAllOrdersSelection() {
53  
54      OrderStatus matchingOrderStatus = new OrderStatus();
55      List matchingOrderStatusList = new LinkedList();
56      matchingOrderStatusList.add(matchingOrderStatus);
57  
58      Mock orderControllerOne = new Mock(Graphable.class);
59      orderControllerOne.expectAndReturn("hasStatus", C.isA(List.class), true);
60      Mock orderControllerTwo = new Mock(Graphable.class);
61      orderControllerTwo.expectAndReturn("hasStatus", C.isA(List.class), false);
62  
63      Graphable expected = (Graphable) orderControllerOne.proxy();
64  
65      Vector orders = new Vector();
66      orders.add(expected);
67      orders.add(orderControllerTwo.proxy());
68  
69      Mock commandController = new Mock(Command.class);
70      commandController.expect("visit", expected);
71  
72      Mock ordersTableController = new Mock(Selectable.class);
73      ordersTableController.expectAndReturn("selection", orders.elements());
74  
75      Selection unit = new OrderSelection((Selectable) ordersTableController.proxy(),
76          matchingOrderStatusList, null, null, null);
77      unit.visitEach((Command) commandController.proxy());
78  
79      ordersTableController.verify();
80      orderControllerOne.verify();
81      orderControllerTwo.verify();
82      commandController.verify();
83  
84    }
85  
86    public void testMonthAdaptor() {
87  
88      EquatableComparable unit = new MonthAdaptor(SOMETIME_IN_SEPTEMBER);
89      EquatableComparable same = new MonthAdaptor(SOME_OTHERTIME_IN_SEPTEMBER);
90      EquatableComparable different = new MonthAdaptor(SOMETIME_IN_AUGUST);
91      EquatableComparable isNull = new MonthAdaptor(null);
92      assertTrue(unit.equals(same));
93      assertTrue(!unit.equals(different));
94      assertTrue(!unit.equals(isNull));
95      assertTrue(!unit.equals(null));
96      assertTrue(!unit.equals(new Object()));
97      assertTrue(!isNull.equals(unit));
98      assertEquals("", isNull.toString());
99      assertEquals(0, isNull.hashCode());
100     assertEquals(-1, different.compareTo(unit));
101     assertEquals(1, unit.compareTo(different));
102     assertEquals(0, unit.compareTo(same));
103   }
104 
105   public void testCollectingCommand() {
106     Double expectedValue = new Double(12);
107     TimesliceValue expectedSeptember = new TimesliceValue(new MonthAdaptor(SOMETIME_IN_SEPTEMBER),
108         new Double(expectedValue.doubleValue() * 2));
109     TimesliceValue expectedAugust = new TimesliceValue(new MonthAdaptor(SOMETIME_IN_AUGUST),
110         expectedValue);
111 
112     Map bucket = new HashMap();
113     TimesliceValue max = new TimesliceValue(null, 0);
114 
115     Command unit = new CollectingCommand(bucket, max, new MonthlyTimesliceFactory(null), new AffiliateTotals());
116 
117     Mock orderControlOne = new Mock(Graphable.class);
118     orderControlOne.expectAndReturn("getDate", new Timestamp(SOMETIME_IN_SEPTEMBER.getTime()));
119     orderControlOne.expectAndReturn("getAmountUK", expectedValue);
120     unit.visit(orderControlOne.proxy());
121 
122     Mock orderControlTwo = new Mock(Graphable.class);
123     orderControlTwo
124         .expectAndReturn("getDate", new Timestamp(SOME_OTHERTIME_IN_SEPTEMBER.getTime()));
125     orderControlTwo.expectAndReturn("getAmountUK", expectedValue);
126     unit.visit(orderControlTwo.proxy());
127 
128     Mock orderControlThree = new Mock(Graphable.class);
129     orderControlThree.expectAndReturn("getDate", new Timestamp(SOMETIME_IN_AUGUST.getTime()));
130     orderControlThree.expectAndReturn("getAmountUK", expectedValue);
131     unit.visit(orderControlThree.proxy());
132 
133     assertEquals(expectedSeptember, bucket.get(new MonthAdaptor(SOMETIME_IN_SEPTEMBER)));
134     assertEquals(expectedAugust, bucket.get(new MonthAdaptor(SOMETIME_IN_AUGUST)));
135     assertEquals(expectedSeptember, max);
136     orderControlOne.verify();
137   }
138 
139   public void testMonthValue() {
140     TimesliceValue value = new TimesliceValue(new MonthAdaptor(SOMETIME_IN_AUGUST), 12);
141     TimesliceValue unit = new TimesliceValue(new MonthAdaptor(SOMETIME_IN_SEPTEMBER), 23);
142     unit.copyInto(value);
143     assertEquals(unit, value);
144     assertEquals("09/2003", unit.getDisplayMonth());
145     assertEquals(new Double(23), unit.getDisplayValue());
146   }
147 
148   public void testMonthValueCompare() {
149 
150   }
151 
152   public void testHandleInteraction() {
153     Mock contextControl = makeContext();
154     Mock selectionController = new Mock(Selection.class);
155     Mock commandController = new Mock(Command.class);
156     Command command = (Command) commandController.proxy();
157     selectionController.expect("visitEach", command);
158     Presenter unit = new OrdersByMonthPresenter(null, (Context) contextControl.proxy(), null);
159     unit.handleInteraction((Selection) selectionController.proxy(), command);
160     selectionController.verify();
161   }
162 
163 }