PageRenderTime 124ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/domain-management/src/test/java/org/jboss/as/domain/management/security/AssertConsoleBuilder.java

http://github.com/jbossas/jboss-as
Java | 194 lines | 92 code | 21 blank | 81 comment | 9 complexity | 5013b45ed5225cc2c13531e58e6d37e5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.domain.management.security;
  23. import java.util.Iterator;
  24. import java.util.LinkedList;
  25. import java.util.Queue;
  26. import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
  27. import static org.junit.Assert.*;
  28. /**
  29. * Assert builder for the console. Use this together with the ConsoleMock object
  30. * to validate the expected messages are displayed and answers. You should always
  31. * recorded in the same order as the user gets it present.
  32. *
  33. * This example shows how you can record a chain of messages and answers
  34. *
  35. * AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().
  36. * expectedDisplayText("Please enter valid password").
  37. * expectedDisplayText("\n").
  38. * expectedInput("mysecretpassword");
  39. *
  40. * ConsoleMock consoleMock = new ConsoleMock();
  41. * consoleMock.setResponses(consoleBuilder);
  42. *
  43. * ...
  44. * ...
  45. *
  46. * consoleBuilder.validate()
  47. *
  48. *
  49. * @author <a href="mailto:flemming.harms@gmail.com">Flemming Harms</a>
  50. */
  51. public class AssertConsoleBuilder {
  52. private static String NEW_LINE = String.format("%n");
  53. private enum Type {
  54. DISPLAY, INPUT
  55. }
  56. private class AssertConsole {
  57. private String text;
  58. private Type type;
  59. private AssertConsole() {
  60. }
  61. private AssertConsole(String text, Type type) {
  62. this.text = text;
  63. this.type = type;
  64. }
  65. }
  66. private Queue<AssertConsole> queue = new LinkedList<AssertConsole>();
  67. /**
  68. * Recorded the expected display text
  69. * @param text - the display text
  70. * @return this
  71. */
  72. public AssertConsoleBuilder expectedDisplayText(String text) {
  73. AssertConsole assertConsole = new AssertConsole();
  74. assertConsole.text = text;
  75. assertConsole.type = Type.DISPLAY;
  76. queue.add(assertConsole);
  77. return this;
  78. }
  79. /**
  80. * Expected input string from the user
  81. * @param text
  82. * @return this
  83. */
  84. public AssertConsoleBuilder expectedInput(String text) {
  85. AssertConsole assertConsole = new AssertConsole();
  86. assertConsole.text = text;
  87. assertConsole.type = Type.INPUT;
  88. queue.add(assertConsole);
  89. return this;
  90. }
  91. /**
  92. * Expected error message
  93. * @param text
  94. * @return this
  95. */
  96. public AssertConsoleBuilder expectedErrorMessage(String text) {
  97. queue.add(new AssertConsole(NEW_LINE,Type.DISPLAY));
  98. queue.add(new AssertConsole(" * ",Type.DISPLAY));
  99. queue.add(new AssertConsole(MESSAGES.errorHeader(),Type.DISPLAY));
  100. queue.add(new AssertConsole(" * ",Type.DISPLAY));
  101. queue.add(new AssertConsole(NEW_LINE,Type.DISPLAY));
  102. queue.add(new AssertConsole(text,Type.DISPLAY));
  103. queue.add(new AssertConsole(NEW_LINE,Type.DISPLAY));
  104. queue.add(new AssertConsole(NEW_LINE,Type.DISPLAY));
  105. return this;
  106. }
  107. /**
  108. * Expected confirm message and answer
  109. * @param messages - expected display text, if none pass in null
  110. * @param prompt - expected text for the console prompt
  111. * @param answer - expected answer
  112. * @return this
  113. */
  114. public AssertConsoleBuilder expectedConfirmMessage(String messages, String prompt, String answer) {
  115. if (messages!=null) {
  116. queue.add(new AssertConsole(messages,Type.DISPLAY));
  117. queue.add(new AssertConsole(NEW_LINE,Type.DISPLAY));
  118. }
  119. queue.add(new AssertConsole(prompt,Type.DISPLAY));
  120. queue.add(new AssertConsole(" ",Type.DISPLAY));
  121. queue.add(new AssertConsole(answer,Type.INPUT));
  122. return this;
  123. }
  124. /**
  125. * Assert the display text from the console, with the recorded display text.
  126. * if the doses't match it will fail
  127. * @param msg - display text from the console.
  128. * @return a String with recorded display text
  129. */
  130. public String assertDisplayText(String msg) {
  131. AssertConsole assertConsole = queue.poll();
  132. if (assertConsole == null) {
  133. fail("Expected display text '"+msg+"' was not recorded");
  134. }
  135. if (!assertConsole.type.equals(Type.DISPLAY)) {
  136. fail("Wrong assert type, expect Type.DISPLAY");
  137. }
  138. assertEquals(assertConsole.text, msg);
  139. return assertConsole.text;
  140. }
  141. /**
  142. * Pop the next recorded answer to the console. if recorded is not
  143. * the Type.INPUT it will fail.
  144. * @return the recorded answer
  145. */
  146. public String popAnswer() {
  147. AssertConsole assertConsole = queue.poll();
  148. if (assertConsole == null) {
  149. fail("Expected answer was not recorded");
  150. }
  151. if (!assertConsole.type.equals(Type.INPUT)) {
  152. fail("Wrong assert type, expect Type.INPUT");
  153. }
  154. return assertConsole.text;
  155. }
  156. /**
  157. * validate if all recorded console assert has been asserted.
  158. * if not empty it will fail
  159. */
  160. public void validate() {
  161. StringBuffer notValidateAsserts = new StringBuffer();
  162. if (!queue.isEmpty()) {
  163. Iterator<AssertConsole> assertConsoleIter = queue.iterator();
  164. for (Iterator<AssertConsole> iterator = queue.iterator(); iterator.hasNext(); ) {
  165. AssertConsole assertConsole = iterator.next();
  166. notValidateAsserts.append("\"");
  167. notValidateAsserts.append(assertConsole.text);
  168. notValidateAsserts.append("\" ");
  169. }
  170. }
  171. assertTrue("There are still asserts in the queue that are not validated : "+notValidateAsserts.toString(),queue.isEmpty());
  172. }
  173. }