001    package sysModel.fish.tests;
002    
003    import controller.IScrollAdapter;
004    import junit.framework.TestCase;
005    import model.ILambda;
006    import sysModel.ICmdFactory;
007    import sysModel.ISecurityAdapter;
008    import sysModel.env.*;
009    import sysModel.fish.*;
010    import sysModel.parser.Lexer;
011    
012    import java.awt.*;
013    import java.io.PrintWriter;
014    
015    /**
016     * Test cases for AFish.
017     *
018     * @author Mathias Ricken
019     */
020    public class Test_Fish extends TestCase {
021        private IBlockedCommand _dummyB;
022        private IOpenCommand _dummyO;
023    
024        private class TestFish extends AFish {
025            protected void move() {
026                // dummy
027            }
028    
029            public void paint(Graphics2D g, Component comp) {
030                // dummy
031            }
032    
033            public void age() {
034                super.age();
035            }
036    
037            public TestFish(Color c) {
038                super(c, new IFishDisplay() {
039                    public void draw(Graphics2D g, Component comp, Color fishColor) {
040                    }
041                });
042            }
043        }
044    
045        private ICmdFactory _cmdFactory;
046        private ISecurityAdapter _sm;
047        private TestFish _fish;
048        private TestGlobalEnv _env;
049        private AGlobalEnv.ALocalEnv _localEnv;
050    
051        private static final ILambda _notify = new ILambda() {
052            public Object apply(Object param) {
053                return "notifyCmd";
054            }
055        };
056        private static final ILambda _delete = new ILambda() {
057            public Object apply(Object param) {
058                return "deleteCmd";
059            }
060        };
061        private static final ILambda _add = new ILambda() {
062            public Object apply(Object param) {
063                return "addCmd";
064            }
065        };
066    
067        private final class TestGlobalEnv extends AGlobalEnv {
068            public TestGlobalEnv(ICmdFactory cmdFactory, ISecurityAdapter sm) {
069                super(cmdFactory, sm);
070            }
071    
072            protected ILambda editFish(AGlobalEnv.ALocalEnv localEnv, IFishFactory fishFactory, int button) {
073                return null;
074            }
075    
076            protected void addFishToInternalData(AGlobalEnv.ALocalEnv localEnv, AFish fish) {
077            }
078    
079            protected void removeFishFromInternalData(AGlobalEnv.ALocalEnv localEnv) {
080            }
081    
082            protected AGlobalEnv parseEnvironment(Lexer l) {
083                return null;
084            }
085    
086            protected void parseFish(Lexer l) {
087            }
088    
089            public AGlobalEnv.ALocalEnv makeLocalEnv(Point.Double p) {
090                return new AGlobalEnv.ALocalEnv() {
091                    void checkFish(AFish fish) {
092                        if (fish != _fish) {
093                            throw new IllegalArgumentException("Wrong fish!");
094                        }
095                    }
096    
097                    public Object tryMoveFwd(AFish fish, IBlockedCommand blockedCmd, IOpenCommand openCmd) {
098                        checkFish(fish);
099                        throw new RuntimeException("tryMoveFwd called");
100                    }
101    
102                    public Object tryBreedFwd(AFish fish, IBlockedCommand blockedCmd, IOpenCommand openCmd) {
103                        checkFish(fish);
104                        throw new RuntimeException("tryBreedFwd called");
105                    }
106    
107                    public void drawFish(AFish fish, Graphics2D g, Component comp) {
108                        checkFish(fish);
109                        throw new RuntimeException("drawFish called");
110                    }
111    
112                    public void turnRight(AFish fish, double radians) {
113                        checkFish(fish);
114                        throw new RuntimeException("turnRight called, radians=" + radians);
115                    }
116    
117                    public void removeFish(AFish fish) {
118                        checkFish(fish);
119                        throw new RuntimeException("removeFish called");
120                    }
121    
122                    public Object execute(AGlobalEnv.ILocalEnvVisitor visitor, Object param) {
123                        throw new RuntimeException("execute called");
124                    }
125    
126                    public void setState(ILocalEnvState state) {
127                    }
128    
129                    protected ILambda makeMoveLambda(AGlobalEnv.ALocalEnv le) {
130                        return null;
131                    }
132    
133                    protected AGlobalEnv.ALocalEnv makeMoveFwdLocalEnv() {
134                        return null;
135                    }
136                };
137            }
138    
139            public AEnvFactory makeEnvFactory() {
140                return null;
141            }
142    
143            protected void printHeader(PrintWriter pw) {
144            }
145    
146            public Dimension getDisplaySize() {
147                return null;
148            }
149    
150            public void returnHome(IScrollAdapter sa) {
151            }
152    
153            public Point.Double getViewPosition(Point.Double pos) {
154                return null;
155            }
156    
157            public Point.Double getPanDelta(Point.Double delta) {
158                return null;
159            }
160    
161            public ILambda getToolTipText(Point.Double p) {
162                return null;
163            }
164        }
165    
166        public void setUp() throws Exception {
167            super.setUp();
168            _fish = new TestFish(Color.RED);
169    
170            _cmdFactory = new ICmdFactory() {
171                public ILambda makeNotifyCmd(ILambda lambda) {
172                    return _notify;
173                }
174    
175                public ILambda makeDeleteCmd(AGlobalEnv.ALocalEnv env) {
176                    return _delete;
177                }
178    
179                public ILambda makeAddCmd(AFish fish) {
180                    return _add;
181                }
182            };
183            _sm = new ISecurityAdapter() {
184                public void setProtected(boolean _protected) {
185                }
186                public ThreadGroup getFishThreadGroup() {
187                    return null;
188                }
189                public ClassLoader getClassLoader() {
190                    return null;
191                }
192                public void handleException(Throwable t) {
193                }
194            };
195    
196            _env = new TestGlobalEnv(_cmdFactory, _sm);
197    
198            _localEnv = _env.makeLocalEnv(new Point.Double(0, 0));
199            _dummyB = new IBlockedCommand() {
200                public Object apply(Object param) {
201                    return null;
202                }
203            };
204            _dummyO = new IOpenCommand() {
205                public Object apply(Object param) {
206                    return null;
207                }
208            };
209            _fish.setLocalEnvironment(_localEnv);
210        }
211    
212        /**
213         * Test fish to local environment delegation for tryMoveFwd.
214         */
215        public void testTryMoveFwd() {
216            try {
217                _fish.tryMoveFwd(_dummyB, _dummyO);
218                fail("Error in delegation.");
219            }
220            catch (RuntimeException e) {
221                if (!"tryMoveFwd called".equals(e.getMessage())) {
222                    fail("Wrong method called.");
223                }
224            }
225        }
226    
227        /**
228         * Test fish to local environment delegation for tryBreedFwd.
229         */
230        public void testTryBreedFwd() {
231            try {
232                _fish.tryBreedFwd(_dummyB, _dummyO);
233                fail("Error in delegation.");
234            }
235            catch (RuntimeException e) {
236                if (!"tryBreedFwd called".equals(e.getMessage())) {
237                    fail("Wrong method called.");
238                }
239            }
240        }
241    
242        /**
243         * Test fish to local environment delegation for draw.
244         */
245        public void testDraw() {
246            try {
247                _fish.draw(null, null);
248                fail("Error in delegation.");
249            }
250            catch (RuntimeException e) {
251                if (!"drawFish called".equals(e.getMessage())) {
252                    fail("Wrong method called.");
253                }
254            }
255        }
256    
257        /**
258         * Test fish to local environment delegation for turnRight.
259         */
260        public void testTurnRight() {
261            try {
262                _fish.turnRight(3);
263                fail("Error in delegation.");
264            }
265            catch (RuntimeException e) {
266                if (!"turnRight called, radians=3.0".equals(e.getMessage())) {
267                    fail("Wrong method called. " + e);
268                }
269            }
270        }
271    
272        /**
273         * Test fish to local environment delegation for turnLeft.
274         */
275        public void testTurnLeft() {
276            try {
277                _fish.turnLeft(4.5);
278                fail("Error in delegation.");
279            }
280            catch (RuntimeException e) {
281                if (!"turnRight called, radians=-4.5".equals(e.getMessage())) {
282                    fail("Wrong method called. " + e);
283                }
284            }
285        }
286    
287        /**
288         * Test fish to local environment delegation for age.
289         */
290        public void testDie() {
291            _fish.setProbOfDying(2);
292            try {
293                _fish.die();
294                fail("Error in delegation.");
295            }
296            catch (RuntimeException e) {
297                if (!"removeFish called".equals(e.getMessage())) {
298                    fail("Wrong method called.");
299                }
300            }
301        }
302    }