/GroovyMud/src/test/java/org/groovymud/object/registry/MudObjectAttendantTest.java

http://groovymud.googlecode.com/ · Java · 271 lines · 197 code · 52 blank · 22 comment · 0 complexity · 1ec44ee90d26954ea2d070e75b074654 MD5 · raw file

  1. package org.groovymud.object.registry;
  2. import groovy.lang.GroovyClassLoader;
  3. import groovy.util.GroovyScriptEngine;
  4. import groovy.util.ResourceException;
  5. import groovy.util.ScriptException;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.InputStream;
  10. import java.net.MalformedURLException;
  11. import junit.framework.TestCase;
  12. import org.apache.log4j.Level;
  13. import org.apache.log4j.Logger;
  14. import org.codehaus.groovy.control.CompilationFailedException;
  15. import org.easymock.MockControl;
  16. import org.easymock.classextension.MockClassControl;
  17. import org.groovymud.object.Container;
  18. import org.groovymud.object.MudObject;
  19. import org.groovymud.object.ObjectLocation;
  20. import org.groovymud.object.alive.Player;
  21. import org.groovymud.object.room.Room;
  22. import org.groovymud.shell.command.CommandInterpreter;
  23. import org.groovymud.shell.telnetd.LoggingExtendedTerminalIO;
  24. import org.springframework.context.ApplicationContext;
  25. import com.thoughtworks.xstream.XStream;
  26. public class MudObjectAttendantTest extends TestCase {
  27. boolean methodCalled;
  28. protected boolean methodCalled1;
  29. protected boolean methodCalled2;
  30. MockControl ctrl;
  31. @Override
  32. protected void setUp() throws Exception {
  33. ctrl = MockControl.createControl(MudObject.class);
  34. Logger.getRootLogger().setLevel(Level.OFF);
  35. }
  36. /*
  37. * Test method for
  38. * 'org.groovymud.object.registry.MudObjectAttendant.load(String, boolean)'
  39. */
  40. public void testLoadToRegistry() throws ResourceException, ScriptException, InstantiationException, IllegalAccessException, CompilationFailedException, FileNotFoundException, MalformedURLException {
  41. final String scriptName = "/mockScriptName.groovy";
  42. final MudObject mudObject = (MudObject) ctrl.getMock();
  43. MockControl ctxCtrl = MockControl.createControl(ApplicationContext.class);
  44. final ApplicationContext mockApplicationContext = (ApplicationContext) ctxCtrl.getMock();
  45. mockApplicationContext.containsBean("beanId");
  46. ctxCtrl.setDefaultReturnValue(false);
  47. ctxCtrl.replay();
  48. MockControl regCtrl = MockClassControl.createControl(Registry.class);
  49. final Registry reg = (Registry) regCtrl.getMock();
  50. reg.getMudObject("beanId");
  51. regCtrl.setDefaultReturnValue(mudObject);
  52. regCtrl.replay();
  53. MudObjectAttendant attnd = new MudObjectAttendant() {
  54. @Override
  55. protected void loadDefinition(String scriptLocation) {
  56. assertEquals(scriptName, scriptLocation);
  57. }
  58. @Override
  59. public ApplicationContext getApplicationContext() {
  60. // TODO Auto-generated method stub
  61. return mockApplicationContext;
  62. }
  63. @Override
  64. public Registry getObjectRegistry() {
  65. // TODO Auto-generated method stub
  66. return reg;
  67. }
  68. };
  69. ObjectLocation location = new ObjectLocation();
  70. location.setDefinition(scriptName);
  71. location.setBeanId("beanId");
  72. MudObject myObj = attnd.load(location);
  73. assertEquals(mudObject.getClass(), myObj.getClass());
  74. ctxCtrl.verify();
  75. regCtrl.verify();
  76. }
  77. /*
  78. * Test method for
  79. * 'org.groovymud.object.registry.MudObjectAttendant.load(String, boolean)'
  80. */
  81. public void testLoadAsBean() throws ResourceException, ScriptException, InstantiationException, IllegalAccessException, CompilationFailedException, FileNotFoundException, MalformedURLException {
  82. final String scriptName = "/mockScriptName.groovy";
  83. final MudObject mudObject = (MudObject) ctrl.getMock();
  84. MockControl ctxCtrl = MockControl.createControl(ApplicationContext.class);
  85. final ApplicationContext mockApplicationContext = (ApplicationContext) ctxCtrl.getMock();
  86. mockApplicationContext.containsBean("beanId");
  87. ctxCtrl.setReturnValue(false);
  88. mockApplicationContext.getBean("beanId");
  89. ctxCtrl.setDefaultReturnValue(mudObject);
  90. mockApplicationContext.containsBean("beanId");
  91. ctxCtrl.setReturnValue(true);
  92. ctxCtrl.replay();
  93. MockControl regCtrl = MockClassControl.createControl(Registry.class);
  94. final Registry reg = (Registry) regCtrl.getMock();
  95. reg.getMudObject("beanId");
  96. regCtrl.setDefaultReturnValue(null);
  97. regCtrl.replay();
  98. MudObjectAttendant attnd = new MudObjectAttendant() {
  99. @Override
  100. protected void loadDefinition(String scriptLocation) {
  101. assertEquals(scriptName, scriptLocation);
  102. }
  103. @Override
  104. public ApplicationContext getApplicationContext() {
  105. // TODO Auto-generated method stub
  106. return mockApplicationContext;
  107. }
  108. @Override
  109. public Registry getObjectRegistry() {
  110. // TODO Auto-generated method stub
  111. return reg;
  112. }
  113. };
  114. ObjectLocation location = new ObjectLocation();
  115. location.setDefinition(scriptName);
  116. location.setBeanId("beanId");
  117. MudObject myObj = attnd.load(location);
  118. assertEquals(mudObject.getClass(), myObj.getClass());
  119. ctxCtrl.verify();
  120. regCtrl.verify();
  121. }
  122. /*
  123. * Test method for
  124. * 'org.groovymud.object.registry.MudObjectAttendant.loadPlayerData(String)'
  125. */
  126. public void testLoadPlayerData() throws CompilationFailedException, FileNotFoundException, ResourceException, ScriptException {
  127. final GroovyClassLoader mockClassLoader = new GroovyClassLoader() {
  128. };
  129. MockControl playerCtrl = MockControl.createControl(Player.class);
  130. final Player player = (Player) playerCtrl.getMock();
  131. final InputStream inputStream = new ByteArrayInputStream(new byte[] { '<', 'x', 'm', 'l', '/', '>' });
  132. final XStream mockXStream = new XStream() {
  133. @Override
  134. public Object fromXML(InputStream input) {
  135. // TODO Auto-generated method stub
  136. assertEquals(inputStream, input);
  137. methodCalled1 = true;
  138. return player;
  139. }
  140. };
  141. MockControl scriptCtrl = MockClassControl.createControl(GroovyScriptEngine.class, new Class[] { String.class }, new String[] { new String("file://") });
  142. final GroovyScriptEngine eng = (GroovyScriptEngine) scriptCtrl.getMock();
  143. eng.getParentClassLoader();
  144. scriptCtrl.setDefaultReturnValue(null);
  145. eng.loadScriptByName("player.Impl");
  146. scriptCtrl.setReturnValue(player.getClass());
  147. scriptCtrl.replay();
  148. MudObjectAttendant attend = new MudObjectAttendant() {
  149. protected InputStream createFileInputStream(File playerFile) throws FileNotFoundException {
  150. return inputStream;
  151. }
  152. @Override
  153. public ObjectLocation getPlayerImpl() {
  154. ObjectLocation loc = new ObjectLocation();
  155. loc.setBeanId("player.impl");
  156. loc.setDefinition("loc/def");
  157. return loc;
  158. }
  159. @Override
  160. protected File createFile(String username) {
  161. // TODO Auto-generated method stub
  162. return new File(username) {
  163. @Override
  164. public boolean exists() {
  165. // TODO Auto-generated method stub
  166. return true;
  167. }
  168. };
  169. }
  170. @Override
  171. public XStream getXStream() {
  172. // TODO Auto-generated method stub
  173. return mockXStream;
  174. }
  175. @Override
  176. public GroovyScriptEngine getGroovyScriptEngine() {
  177. // TODO Auto-generated method stub
  178. return eng;
  179. }
  180. };
  181. attend.loadPlayerData("player");
  182. assertTrue(methodCalled1);
  183. assertTrue(methodCalled2);
  184. }
  185. public void testCreateNewPlayer() throws CompilationFailedException, FileNotFoundException, InstantiationException {
  186. String username = "wombat";
  187. String upperuname = username.substring(0, 1).toUpperCase() + username.substring(1);
  188. String password = "x";
  189. MockControl mockCtrl = MockControl.createControl(Player.class);
  190. final Player mockPlayer = (Player) mockCtrl.getMock();
  191. mockPlayer.setName(username);
  192. mockCtrl.setVoidCallable();
  193. mockPlayer.setName(upperuname);
  194. mockCtrl.setVoidCallable();
  195. mockPlayer.setName(upperuname);
  196. mockCtrl.setVoidCallable();
  197. mockPlayer.getName();
  198. mockCtrl.setDefaultReturnValue(upperuname);
  199. mockPlayer.addShortName(upperuname);
  200. mockCtrl.setVoidCallable();
  201. mockPlayer.addShortName(upperuname);
  202. mockCtrl.setVoidCallable();
  203. mockPlayer.initialise();
  204. mockCtrl.setVoidCallable();
  205. MudObjectAttendant attnd = new MudObjectAttendant() {
  206. @Override
  207. public MudObject load(ObjectLocation loc) {
  208. return mockPlayer;
  209. }
  210. @Override
  211. public XStream getXStream() {
  212. // TODO Auto-generated method stub
  213. return null;
  214. }
  215. };
  216. attnd.createNewPlayer(username);
  217. assertTrue(methodCalled);
  218. }
  219. }