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

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