PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/fitnesse/slim/SlimServiceTestBase.java

http://github.com/unclebob/fitnesse
Java | 216 lines | 176 code | 36 blank | 4 comment | 5 complexity | be5c624700924b4c0eda376fa506ffdd MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, GPL-2.0
  1. // Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
  2. // Released under the terms of the CPL Common Public License version 1.0.
  3. package fitnesse.slim;
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertNull;
  6. import static org.junit.Assert.assertTrue;
  7. import static util.ListUtility.list;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.junit.After;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. // Extracted Test class to be implemented by all Java based Slim ports
  15. // The tests for PhpSlim and JsSlim implement this class
  16. public abstract class SlimServiceTestBase {
  17. protected List<Object> statements;
  18. protected SlimClient slimClient;
  19. protected abstract void startSlimService() throws Exception;
  20. protected abstract String getImport();
  21. protected abstract String expectedExceptionMessage();
  22. protected abstract String expectedStopTestExceptionMessage();
  23. @Before
  24. public void setUp() throws Exception {
  25. createSlimService();
  26. slimClient = new SlimClient("localhost", 8099);
  27. statements = new ArrayList<Object>();
  28. slimClient.connect();
  29. }
  30. protected void createSlimService() throws Exception {
  31. while (!tryCreateSlimService())
  32. Thread.sleep(10);
  33. }
  34. private boolean tryCreateSlimService() throws Exception {
  35. try {
  36. startSlimService();
  37. return true;
  38. } catch (Exception e) {
  39. return false;
  40. }
  41. }
  42. @After
  43. public void after() throws Exception {
  44. teardown();
  45. }
  46. protected void teardown() throws Exception {
  47. slimClient.sendBye();
  48. slimClient.close();
  49. }
  50. @Test
  51. public void emptySession() throws Exception {
  52. assertTrue("Connected", slimClient.isConnected());
  53. }
  54. @Test
  55. public void versionNumberShouldBeDetected() throws Exception {
  56. double slimVersion = Double.parseDouble(SlimVersion.VERSION);
  57. assertEquals(slimVersion, slimClient.getServerVersion(), .0001);
  58. }
  59. @Test
  60. public void callOneMethod() throws Exception {
  61. addImportAndMake();
  62. addEchoInt("id", "1");
  63. Map<String, Object> result = slimClient.invokeAndGetResponse(statements);
  64. assertEquals("1", result.get("id"));
  65. }
  66. private void addEchoInt(String id, String number) {
  67. statements.add(list(id, "call", "testSlim", "echoInt", number));
  68. }
  69. private void addImportAndMake() {
  70. statements.add(list("i1", "import", getImport()));
  71. statements.add(list("m1", "make", "testSlim", "TestSlim"));
  72. }
  73. @Test
  74. public void makeManyCallsInOrderToTestLongSequencesOfInstructions() throws Exception {
  75. addImportAndMake();
  76. for (int i = 0; i < 1000; i++)
  77. addEchoInt(String.format("id_%d", i), Integer.toString(i));
  78. Map<String, Object> result = slimClient.invokeAndGetResponse(statements);
  79. for (int i = 0; i < 1000; i++)
  80. assertEquals(i, Integer.parseInt((String) result.get(String.format("id_%d", i))));
  81. }
  82. @Test
  83. public void callWithLineBreakInStringArgument() throws Exception {
  84. addImportAndMake();
  85. statements.add(list("id", "call", "testSlim", "echoString", "hello\nworld\n"));
  86. Map<String, Object> result = slimClient.invokeAndGetResponse(statements);
  87. assertEquals("hello\nworld\n", result.get("id"));
  88. }
  89. @Test
  90. public void callWithMultiByteChar() throws Exception {
  91. addImportAndMake();
  92. statements.add(list("id", "call", "testSlim", "echoString", "K\u00f6ln"));
  93. Map<String, Object> result = slimClient.invokeAndGetResponse(statements);
  94. assertEquals("K\u00f6ln", result.get("id"));
  95. }
  96. @Test
  97. public void makeManyIndividualCalls() throws Exception {
  98. addImportAndMake();
  99. slimClient.invokeAndGetResponse(statements);
  100. for (int i = 0; i < 100; i++) {
  101. statements.clear();
  102. addEchoInt("id", "42");
  103. Map<String, Object> result = slimClient.invokeAndGetResponse(statements);
  104. assertEquals(1, result.size());
  105. assertEquals("42", result.get("id"));
  106. }
  107. }
  108. @Test
  109. public void callFunctionThatDoesntExist() throws Exception {
  110. addImportAndMake();
  111. statements.add(list("id", "call", "testSlim", "noSuchFunction"));
  112. Map<String, Object> results = slimClient.invokeAndGetResponse(statements);
  113. assertContainsException("message:<<NO_METHOD_IN_CLASS", "id", results);
  114. }
  115. private void assertContainsException(String message, String id, Map<String, Object> results) {
  116. String result = (String) results.get(id);
  117. assertTrue(result, result.contains(SlimServer.EXCEPTION_TAG)
  118. && result.contains(message));
  119. }
  120. @Test
  121. public void makeClassThatDoesntExist() throws Exception {
  122. statements.add(list("m1", "make", "me", "NoSuchClass"));
  123. Map<String, Object> results = slimClient.invokeAndGetResponse(statements);
  124. assertContainsException("message:<<COULD_NOT_INVOKE_CONSTRUCTOR", "m1", results);
  125. }
  126. @Test
  127. public void useInstanceThatDoesntExist() throws Exception {
  128. addImportAndMake();
  129. statements.add(list("id", "call", "noInstance", "f"));
  130. Map<String, Object> results = slimClient.invokeAndGetResponse(statements);
  131. assertContainsException("message:<<NO_INSTANCE", "id", results);
  132. }
  133. @Test
  134. public void verboseArgument() throws Exception {
  135. String args[] = {"-v", "99"};
  136. assertTrue(SlimService.parseCommandLine(args));
  137. assertTrue(SlimService.verbose);
  138. }
  139. @Test
  140. public void notStopTestExceptionThrown() throws Exception {
  141. addImportAndMake();
  142. statements.add(list("id", "call", "testSlim", "throwNormal"));
  143. statements.add(list("id2", "call", "testSlim", "throwNormal"));
  144. Map<String, Object> results = slimClient.invokeAndGetResponse(statements);
  145. assertContainsException("__EXCEPTION__:" + expectedExceptionMessage(), "id", results);
  146. assertContainsException("__EXCEPTION__:" + expectedExceptionMessage(), "id2", results);
  147. }
  148. @Test
  149. public void stopTestExceptionThrown() throws Exception {
  150. addImportAndMake();
  151. statements.add(list("id", "call", "testSlim", "throwStopping"));
  152. statements.add(list("id2", "call", "testSlim", "throwNormal"));
  153. Map<String, Object> results = slimClient.invokeAndGetResponse(statements);
  154. assertContainsException("__EXCEPTION__:" + expectedStopTestExceptionMessage(), "id", results);
  155. assertNull(results.get("id2"));
  156. }
  157. @Test
  158. public void canSpecifyAnInteractionClass() {
  159. SlimService.parseCommandLine(new String[]{"-i", "fitnesse.slim.fixtureInteraction.DefaultInteraction"});
  160. assertEquals("fitnesse.slim.fixtureInteraction.DefaultInteraction", SlimService.getInteractionClass().getName());
  161. }
  162. @Test
  163. public void canSpecifyAComplexCommandLine() {
  164. String commandLine = "-v -i fitnesse.slim.fixtureInteraction.DefaultInteraction 7890";
  165. String[] args = commandLine.split(" ");
  166. assertTrue("should parse correctly", SlimService.parseCommandLine(args));
  167. assertEquals("should have interaction class set", "fitnesse.slim.fixtureInteraction.DefaultInteraction", SlimService.getInteractionClass().getName());
  168. assertTrue("should be verbose", SlimService.verbose);
  169. assertEquals("should have set port", 7890, SlimService.port);
  170. }
  171. @Test
  172. public void canSpecifyComplexArgs() {
  173. String commandLine = "-v -i fitnesse.slim.fixtureInteraction.DefaultInteraction 7890";
  174. String[] args = commandLine.split(" ");
  175. assertTrue("should parse correctly", SlimService.parseCommandLine(args));
  176. assertEquals("should have interaction class set", "fitnesse.slim.fixtureInteraction.DefaultInteraction", SlimService.getInteractionClass().getName());
  177. assertTrue("should be verbose", SlimService.verbose);
  178. assertEquals("should have set port", 7890, SlimService.port);
  179. }
  180. }