You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptionChecker.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.util.options;
  13. import java.util.Arrays;
  14. import org.aspectj.testing.util.LangUtil;
  15. import junit.framework.Assert;
  16. /**
  17. * Drivers to test a given set of Options.
  18. * They now throw AssertionFailedError on failure,
  19. * but subclasses can reimplement
  20. * <code>assertionFailed(String)</code>
  21. * to handle failures differently.
  22. */
  23. public class OptionChecker {
  24. private final Options options;
  25. public OptionChecker(Options options) {
  26. this.options = options;
  27. LangUtil.throwIaxIfNull(options, "options");
  28. }
  29. /**
  30. * Subclasses override this to throw different exceptions
  31. * on assertion failures.
  32. * This implementation delegates to
  33. * <code>Assert.assertTrue(label, false)</code>.
  34. * @param label the String message for the assertion
  35. *
  36. */
  37. public void assertionFailed(String label) {
  38. Assert.assertTrue(label, false);
  39. }
  40. public void checkAssertion(String label, boolean test) {
  41. if (!test) {
  42. assertionFailed(label);
  43. }
  44. }
  45. public void checkOptions(String[] input, String[] expected) {
  46. checkOptions(input, expected, true);
  47. }
  48. public void checkOptions(
  49. String[] input,
  50. String[] expected,
  51. boolean resolve) {
  52. Values values = getValues(input);
  53. if (resolve) {
  54. String err = values.resolve();
  55. checkAssertion("error: \"" + err + "\"", null == err);
  56. }
  57. String[] actual = values.render(); // Value.render(values);
  58. checkEqual(expected, actual);
  59. }
  60. public void checkOptionsNegative(
  61. String[] input,
  62. String expectedMissedMatchErr,
  63. String expectedResolveErr) {
  64. checkOptionsNegative(
  65. input,
  66. null,
  67. expectedMissedMatchErr,
  68. expectedResolveErr);
  69. }
  70. public void checkOptionsNegative(
  71. String[] input,
  72. String expectedInValuesException,
  73. String expectedMissedMatchErr,
  74. String expectedResolveErr) {
  75. Values values =
  76. getValuesNegative(input, expectedInValuesException);
  77. if (null == expectedInValuesException) {
  78. String err = Options.missedMatchError(input, values);
  79. checkContains(expectedMissedMatchErr, err);
  80. err = values.resolve();
  81. checkContains(expectedResolveErr, err);
  82. }
  83. }
  84. private Values getValuesNegative(
  85. String[] input,
  86. String expectedInExceptionMessage,
  87. Options options) {
  88. try {
  89. return options.acceptInput(input);
  90. } catch (Option.InvalidInputException e) {
  91. String m = e.getFullMessage();
  92. boolean ok =
  93. (null != expectedInExceptionMessage)
  94. && (-1 != m.indexOf(expectedInExceptionMessage));
  95. if (!ok) {
  96. e.printStackTrace(System.err);
  97. if (null != expectedInExceptionMessage) {
  98. m =
  99. "expected \""
  100. + expectedInExceptionMessage
  101. + "\" in "
  102. + m;
  103. }
  104. assertionFailed(m);
  105. }
  106. return null; // actually never executed
  107. }
  108. }
  109. private Values getValuesNegative(
  110. String[] input,
  111. String expectedInExceptionMessage) {
  112. return getValuesNegative(
  113. input,
  114. expectedInExceptionMessage,
  115. options);
  116. }
  117. // private Values getValues(String[] input, Options options) {
  118. // return getValuesNegative(input, null, options);
  119. // }
  120. private Values getValues(String[] input) {
  121. return getValuesNegative(input, null);
  122. }
  123. private void checkContains(String expected, String expectedIn) {
  124. if (null == expected) {
  125. if (null != expectedIn) {
  126. assertionFailed("did not expect \"" + expectedIn + "\"");
  127. }
  128. } else {
  129. if ((null == expectedIn)
  130. || (-1 == expectedIn.indexOf(expected))) {
  131. assertionFailed(
  132. "expected \""
  133. + expected
  134. + "\" in \""
  135. + expectedIn
  136. + "\"");
  137. }
  138. }
  139. }
  140. private String safeString(String[] ra) {
  141. return (null == ra ? "null" : Arrays.asList(ra).toString());
  142. }
  143. private void checkEqual(String[] expected, String[] actual) {
  144. if (!isEqual(expected, actual)) {
  145. assertionFailed(
  146. "expected \""
  147. + safeString(expected)
  148. + "\" got \""
  149. + safeString(actual)
  150. + "\"");
  151. }
  152. }
  153. private boolean isEqual(String[] expected, String[] actual) {
  154. if (null == expected) {
  155. return (null == actual ? true : false);
  156. } else if (null == actual) {
  157. return false;
  158. } else if (expected.length != actual.length) {
  159. return false;
  160. }
  161. for (int i = 0; i < actual.length; i++) {
  162. String e = expected[i];
  163. String a = actual[i];
  164. if (null == e) {
  165. if (null != a) {
  166. return false;
  167. }
  168. } else if (null == a) {
  169. return false;
  170. } else if (!(e.equals(a))) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. }