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.

MessageUtilTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.PrintStream;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.aspectj.bridge.IMessage;
  22. import org.aspectj.bridge.ISourceLocation;
  23. import org.aspectj.bridge.Message;
  24. import org.aspectj.bridge.MessageHandler;
  25. import org.aspectj.bridge.MessageUtil;
  26. import org.aspectj.bridge.SourceLocation;
  27. import junit.framework.TestCase;
  28. /**
  29. *
  30. */
  31. public class MessageUtilTest extends TestCase {
  32. public MessageUtilTest(String s) {
  33. super(s);
  34. }
  35. MessageHandler samples;
  36. List /* Exception */ exceptions;
  37. List /*ISourceLocation*/ locations;
  38. List /*String */ messageTexts;
  39. public void testMessageRendering() {
  40. MessageHandler messages = getSampleMessages();
  41. System.out.println("testMessageRendering(): run manually evaluate by inspection");
  42. PrintStream oldOut = System.out;
  43. // comment to inspect manually
  44. System.setOut(NullPrintStream.NULL_PrintStream);
  45. try {
  46. MessageUtil.print(System.out, messages, "all label -> ", MessageUtil.MESSAGE_LABEL, MessageUtil.PICK_ALL);
  47. MessageUtil.print(System.out, messages, "info short -> ", MessageUtil.MESSAGE_SHORT, MessageUtil.PICK_INFO);
  48. MessageUtil.print(System.out, messages, "fail line -> ", MessageUtil.MESSAGE_LINE, MessageUtil.PICK_FAIL);
  49. MessageUtil.print(System.out, messages, "debug wide line -> ", MessageUtil.MESSAGE_WIDELINE, MessageUtil.PICK_DEBUG);
  50. MessageUtil.print(System.out, messages, "warn no-loc label -> ", MessageUtil.MESSAGE_LABEL_NOLOC, MessageUtil.PICK_WARNING);
  51. MessageUtil.print(System.out, messages, "abort force-loc line -> ", MessageUtil.MESSAGE_LINE_FORCE_LOC, MessageUtil.PICK_ABORT);
  52. MessageUtil.print(System.out, messages, "info+ short -> ", MessageUtil.MESSAGE_SHORT, MessageUtil.PICK_INFO_PLUS);
  53. MessageUtil.print(System.out, messages, "fail+ line -> ", MessageUtil.MESSAGE_LINE, MessageUtil.PICK_FAIL_PLUS);
  54. MessageUtil.print(System.out, messages, "debug+ wide line -> ", MessageUtil.MESSAGE_WIDELINE, MessageUtil.PICK_DEBUG_PLUS);
  55. MessageUtil.print(System.out, messages, "warn+ no-loc label -> ", MessageUtil.MESSAGE_LABEL_NOLOC, MessageUtil.PICK_WARNING_PLUS);
  56. MessageUtil.print(System.out, messages, "abort+ force-loc line -> ", MessageUtil.MESSAGE_LINE_FORCE_LOC, MessageUtil.PICK_ABORT_PLUS);
  57. } finally {
  58. System.setOut(oldOut);
  59. }
  60. }
  61. List getSampleMessageTexts() {
  62. if (null == messageTexts) {
  63. ArrayList result = new ArrayList();
  64. result.addAll(Arrays.asList(new String[]
  65. { "one", "two", "now is the time for all good men..." }));
  66. messageTexts = result;
  67. }
  68. return messageTexts;
  69. }
  70. List getSampleExceptions() {
  71. if (null == exceptions) {
  72. ArrayList result = new ArrayList();
  73. int i = 1;
  74. result.add(new Error("Error " + i++));
  75. result.add(new RuntimeException("RuntimeException " + i++));
  76. result.add(new IOException("IOException " + i++));
  77. exceptions = result;
  78. }
  79. return exceptions;
  80. }
  81. List getSampleLocations() {
  82. if (null == locations) {
  83. ArrayList result = new ArrayList();
  84. File file = new File("testsrc/org/aspectj/testing/util/MessageUtilTest.java");
  85. result.add(new SourceLocation(file, 1, 2, 1));
  86. result.add(new SourceLocation(file, 100, 100, 0));
  87. locations = result;
  88. }
  89. return locations;
  90. }
  91. MessageHandler getSampleMessages() {
  92. MessageHandler result = new MessageHandler();
  93. for (Iterator kinds = IMessage.KINDS.iterator(); kinds.hasNext();) {
  94. IMessage.Kind kind = (IMessage.Kind) kinds.next();
  95. for (Iterator locs = getSampleLocations().iterator(); locs.hasNext();) {
  96. ISourceLocation sourceLoc = (ISourceLocation) locs.next();
  97. for (Iterator texts = getSampleMessageTexts().iterator();
  98. texts.hasNext();
  99. ) {
  100. String text = (String) texts.next();
  101. for (Iterator exs = getSampleExceptions().iterator();
  102. exs.hasNext();
  103. ) {
  104. Throwable thrown = (Throwable) exs.next();
  105. result.handleMessage(new Message(text, kind, thrown, sourceLoc));
  106. }
  107. result.handleMessage(new Message(text, kind, null, sourceLoc));
  108. }
  109. result.handleMessage(new Message("", kind, null, sourceLoc));
  110. }
  111. result.handleMessage(new Message("", kind, null, null));
  112. }
  113. return result;
  114. }
  115. }