1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2010 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.client.actions;
14 
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 
20 import org.junit.After;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 
24 import games.stendhal.client.ClientSingletonRepository;
25 import games.stendhal.client.MockStendhalClient;
26 import games.stendhal.client.StendhalClient;
27 import games.stendhal.client.gui.MockUserInterface;
28 import games.stendhal.client.gui.UserInterface;
29 import games.stendhal.client.scripting.ChatLineParser;
30 import games.stendhal.client.util.UserInterfaceTestHelper;
31 import marauroa.common.game.RPAction;
32 
33 /**
34  * Test the SummonAtAction class.
35  *
36  * @author Martin Fuchs
37  */
38 public class SummonAtActionTest {
39 	@BeforeClass
init()40 	public static void init() {
41 		UserInterfaceTestHelper.resetUserInterface();
42 	}
43 
44 	@After
tearDown()45 	public void tearDown() throws Exception {
46 		StendhalClient.resetClient();
47 	}
48 
getInterface()49 	private static MockUserInterface getInterface() {
50 		// Check the message
51 		UserInterface ui = ClientSingletonRepository.getUserInterface();
52 		// sanity check
53 		if (ui instanceof MockUserInterface) {
54 			return (MockUserInterface) ui;
55 		}
56 		fail();
57 		// just for the compiler
58 		return null;
59 	}
60 
61 	@Test
testInvalidAmount()62 	public void testInvalidAmount() {
63 		// create client
64 		new MockStendhalClient() {
65 			@Override
66 			public void send(final RPAction action) {
67 				assertEquals("summonat", action.get("type"));
68 				assertEquals("player", action.get("target"));
69 				assertEquals("bag", action.get("slot"));
70 				assertEquals(1, action.getInt("amount"));
71 				assertEquals("5x", action.get("item"));
72 			}
73 		};
74 
75 		final SummonAtAction action = new SummonAtAction();
76 
77 		// issue "/summonat bag 5x money"
78 		assertTrue(action.execute(new String[]{"player", "bag", "5x"}, "money"));
79 
80 		assertEquals("Invalid amount: 5x", getInterface().getLastEventLine());
81 	}
82 
83 	/**
84 	 * Tests for execute.
85 	 */
86 	@Test
testExecute()87 	public void testExecute() {
88 		// create client
89 		new MockStendhalClient() {
90 			@Override
91 			public void send(final RPAction action) {
92 				assertEquals("summonat", action.get("type"));
93 				assertEquals("player", action.get("target"));
94 				assertEquals("bag", action.get("slot"));
95 				assertEquals(5, action.getInt("amount"));
96 				assertEquals("money", action.get("item"));
97 			}
98 		};
99 
100 		// issue "/summonat bag 5 money"
101 		final SummonAtAction action = new SummonAtAction();
102 		assertTrue(action.execute(new String[]{"player", "bag", "5"}, "money"));
103 		assertNull(getInterface().getLastEventLine());
104 	}
105 
106 	/**
107 	 * Tests for spaceHandling.
108 	 */
109 	@Test
testSpaceHandling()110 	public void testSpaceHandling() {
111 		// create client
112 		new MockStendhalClient() {
113 			@Override
114 			public void send(final RPAction action) {
115 				assertEquals("summonat", action.get("type"));
116 				assertEquals("player", action.get("target"));
117 				assertEquals("bag", action.get("slot"));
118 				assertEquals(1, action.getInt("amount"));
119 				assertEquals("silver sword", action.get("item"));
120 			}
121 		};
122 
123 		// issue "/summonat bag silver sword"
124 		final SummonAtAction action = new SummonAtAction();
125 		assertTrue(action.execute(new String[]{"player", "bag", "silver"}, "sword"));
126 		assertNull(getInterface().getLastEventLine());
127 	}
128 
129 	/**
130 	 * Tests for getMaximumParameters.
131 	 */
132 	@Test
testGetMaximumParameters()133 	public void testGetMaximumParameters() {
134 		final SummonAtAction action = new SummonAtAction();
135 		assertEquals(3, action.getMaximumParameters());
136 	}
137 
138 	/**
139 	 * Tests for getMinimumParameters.
140 	 */
141 	@Test
testGetMinimumParameters()142 	public void testGetMinimumParameters() {
143 		final SummonAtAction action = new SummonAtAction();
144 		assertEquals(3, action.getMinimumParameters());
145 	}
146 
147 	/**
148 	 * Tests for fromChatline.
149 	 */
150 	@Test
testFromChatline()151 	public void testFromChatline() {
152 		// create client
153 		new MockStendhalClient() {
154 			@Override
155 			public void send(final RPAction action) {
156 				assertEquals("summonat", action.get("type"));
157 				assertEquals("memem", action.get("target"));
158 				assertEquals("bag", action.get("slot"));
159 				assertEquals(3, action.getInt("amount"));
160 				assertEquals("greater potion", action.get("item"));
161 			}
162 		};
163 		SlashActionRepository.register();
164 		ChatLineParser.parseAndHandle("/summonat memem bag 3 greater potion");
165 	}
166 }
167