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.

DumpTestCase.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Matthew Webster
  10. *******************************************************************************/
  11. package org.aspectj.weaver;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.IMessageHolder;
  18. import org.aspectj.bridge.Message;
  19. import org.aspectj.bridge.MessageHandler;
  20. import junit.framework.TestCase;
  21. /**
  22. * @author websterm
  23. *
  24. * Test Dump facility. Ensure it can be configured and files contain expected contents. Testcase
  25. * returns Dump configuration to orginal state.
  26. */
  27. public class DumpTestCase extends TestCase {
  28. private File dumpFile;
  29. private IMessage.Kind savedDumpCondition;
  30. public DumpTestCase(String name) {
  31. super(name);
  32. }
  33. protected void setUp() throws Exception {
  34. super.setUp();
  35. dumpFile = null;
  36. savedDumpCondition = Dump.getDumpOnExit();
  37. }
  38. protected void tearDown() throws Exception {
  39. super.tearDown();
  40. if (dumpFile != null && dumpFile.exists()) {
  41. boolean deleted = dumpFile.delete();
  42. assertTrue("Dump file '" + dumpFile.getPath() + "' could not be deleted",deleted);
  43. }
  44. Dump.setDumpOnExit(savedDumpCondition);
  45. }
  46. public void testSetDumpOnException () {
  47. Dump.setDumpOnException(true);
  48. assertTrue("DumpOnException should be true",Dump.getDumpOnException());
  49. }
  50. public void testSetDumpOnExit () {
  51. assertTrue("Should be able to set condition 'error'",Dump.setDumpOnExit("error"));
  52. assertTrue("Should be able to set condition 'warning'",Dump.setDumpOnExit("warning"));
  53. assertFalse("Should not be able to set condition 'junk'",Dump.setDumpOnExit("junk"));
  54. }
  55. public void testDump () {
  56. String fileName = Dump.dump("testDump()");
  57. dumpFile = new File(fileName);
  58. assertTrue("Dump file '" + fileName + "' should exist",dumpFile.exists());
  59. }
  60. public void testDumpWithException () {
  61. String message = "testDumpWithException()";
  62. String fileName = recursiveCall(message,100);
  63. dumpFile = new File(fileName);
  64. assertContents(dumpFile,"Exception Information",message);
  65. }
  66. public void testDumpOnExit () {
  67. Dump.setDumpOnExit("abort");
  68. Dump.saveMessageHolder(null);
  69. String fileName = Dump.dumpOnExit();
  70. dumpFile = new File(fileName);
  71. assertTrue("Dump file '" + fileName + "' should exist",dumpFile.exists());
  72. }
  73. public void testDumpOnExitExcluded () {
  74. Dump.setDumpOnExit("abort");
  75. IMessageHolder holder = new MessageHandler();
  76. Dump.saveMessageHolder(holder);
  77. holder.handleMessage(new Message("testDumpOnExitExcluded()",IMessage.ERROR,null,null));
  78. String fileName = Dump.dumpOnExit();
  79. dumpFile = new File(fileName);
  80. assertEquals("Dump '" + fileName + "' should be excluded",Dump.DUMP_EXCLUDED,fileName);
  81. }
  82. public void testDumpOnExitIncluded () {
  83. Dump.setDumpOnExit("error");
  84. IMessageHolder holder = new MessageHandler();
  85. Dump.saveMessageHolder(holder);
  86. IMessage error = new Message("testDumpOnExitIncluded()",IMessage.ERROR,null,null);
  87. holder.handleMessage(error);
  88. String fileName = Dump.dumpOnExit();
  89. dumpFile = new File(fileName);
  90. assertContents(dumpFile,"Compiler Messages",error.getMessage());
  91. }
  92. /* Ensure dump file exists and contains certain contents under a given heading */
  93. public static void assertContents (File dumpFile, String heading, String contents) {
  94. assertTrue("Dump file '" + dumpFile.getPath() + "' should exist",dumpFile.exists());
  95. assertTrue("Dump file '" + dumpFile.getPath()+ "' should contain '" + contents + "'",fileContains(dumpFile,heading,contents));
  96. }
  97. private static boolean fileContains (File dumpFile, String heading, String contents) {
  98. boolean result = false;
  99. try {
  100. BufferedReader reader = new BufferedReader(new FileReader(dumpFile));
  101. String currentHeading = "";
  102. String record;
  103. while ((null != (record = reader.readLine())) && (result == false)) {
  104. if (record.startsWith("----")) currentHeading = record;
  105. else if ((record.indexOf(contents) != -1) && currentHeading.indexOf(heading) != -1) result = true;
  106. }
  107. reader.close();
  108. }
  109. catch (IOException ex) {
  110. fail(ex.toString());
  111. }
  112. return result;
  113. }
  114. /* Generate a big stack trace */
  115. private String recursiveCall (String message, int depth) {
  116. if (depth == 0) {
  117. Throwable th = new RuntimeException(message);
  118. return Dump.dumpWithException(th);
  119. }
  120. else {
  121. return recursiveCall(message,--depth);
  122. }
  123. }
  124. }