/interpreter/tags/at2dist110511/test/edu/vub/at/trace/TraceTest.java

http://ambienttalk.googlecode.com/ · Java · 192 lines · 130 code · 24 blank · 38 comment · 4 complexity · 45295ab8664a20d1defd0e9a63e7614a MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * TraceTest.java created on 22 nov 2009 at 20:30:13
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.trace;
  29. import edu.vub.at.actors.natives.NATActorMirror;
  30. import edu.vub.at.actors.natives.NATAsyncMessage;
  31. import edu.vub.at.eval.Evaluator;
  32. import edu.vub.at.eval.InvocationStack;
  33. import edu.vub.at.exceptions.InterpreterException;
  34. import edu.vub.at.exceptions.XIllegalIndex;
  35. import edu.vub.at.exceptions.XIllegalOperation;
  36. import edu.vub.at.objects.natives.NATNumber;
  37. import edu.vub.at.objects.natives.NATObject;
  38. import edu.vub.at.objects.natives.NATTable;
  39. import edu.vub.at.objects.natives.grammar.AGMethodInvocationCreation;
  40. import edu.vub.at.objects.natives.grammar.AGSymbol;
  41. import edu.vub.at.parser.SourceLocation;
  42. import java.io.BufferedReader;
  43. import java.io.IOException;
  44. import java.io.InputStream;
  45. import java.io.InputStreamReader;
  46. import java.io.StringWriter;
  47. import junit.framework.TestCase;
  48. /**
  49. * @author tvcutsem
  50. *
  51. * Tests the tracing implementation for Causeway.
  52. */
  53. public class TraceTest extends TestCase {
  54. private Tracer log_;
  55. private StringWriter output_;
  56. private class TestMarker implements Marker {
  57. private int turnCounter = 0;
  58. public Anchor apply() { return new Anchor(new Turn("testLoop", 0), turnCounter++); }
  59. }
  60. public void setUp() throws IOException {
  61. output_ = new StringWriter();
  62. log_ = new Tracer(output_, new TestMarker());
  63. }
  64. public void tearDown() {
  65. log_ = null;
  66. output_ = null;
  67. }
  68. /**
  69. * Loads the content of a file and returns that content as a Java String.
  70. */
  71. public static final String loadFile(Class forTestClass, String fileName) throws IOException {
  72. InputStream in = forTestClass.getResource(fileName).openStream();
  73. try {
  74. BufferedReader dis = new BufferedReader(new InputStreamReader(in));;
  75. StringBuffer fBuf = new StringBuffer();
  76. String line;
  77. while ( (line = dis.readLine()) != null) {
  78. fBuf.append(line + "\n");
  79. }
  80. return fBuf.toString();
  81. } finally {
  82. if (in != null) in.close();
  83. }
  84. }
  85. private final void assertOutputEquals(String fileName) throws IOException {
  86. log_.close();
  87. assertEquals(loadFile(this.getClass(), fileName).replaceAll("\\s", ""),
  88. output_.toString().replaceAll("\\s", ""));
  89. }
  90. public void testComment() throws IOException {
  91. log_.comment("theComment");
  92. assertOutputEquals("comment.json");
  93. }
  94. public void testFulfilled() throws IOException {
  95. log_.fulfilled("theCondition", Evaluator.getNil(), Evaluator.getNil());
  96. assertOutputEquals("fulfilled.json");
  97. }
  98. public void testGot() throws IOException, InterpreterException {
  99. log_.got("theMessage",
  100. // <-name(arg)@[]
  101. new NATActorMirror.NATLetter(
  102. null,
  103. new NATObject(),
  104. new NATAsyncMessage(AGSymbol.jAlloc("selector"),
  105. NATTable.of(NATNumber.ONE),
  106. NATTable.EMPTY)));
  107. assertOutputEquals("got.json");
  108. }
  109. public void testProblem() throws IOException {
  110. log_.problem(new XIllegalOperation("theProblem", new XIllegalIndex("theCause")));
  111. assertOutputEquals("problem.json");
  112. }
  113. public void testProgressed() throws IOException {
  114. log_.progressed("theCondition");
  115. assertOutputEquals("progressed.json");
  116. }
  117. public void testRejected() throws IOException {
  118. log_.rejected("theCondition",
  119. new XIllegalOperation("theReason"),
  120. Evaluator.getNil(), Evaluator.getNil());
  121. assertOutputEquals("rejected.json");
  122. }
  123. public void testResolved() throws IOException {
  124. log_.resolved("theCondition");
  125. assertOutputEquals("resolved.json");
  126. }
  127. public void testReturned() throws IOException {
  128. log_.returned("theMessage");
  129. assertOutputEquals("returned.json");
  130. }
  131. public void testSent() throws IOException {
  132. log_.sent("theMessage");
  133. assertOutputEquals("sent.json");
  134. }
  135. public void testSentIf() throws IOException {
  136. log_.sentIf("theMessage", "theCondition");
  137. assertOutputEquals("sentif.json");
  138. }
  139. public void testStackTrace() throws IOException, InterpreterException {
  140. // invoked nil.name(arg := 1)
  141. InvocationStack.getInvocationStack().methodInvoked(
  142. new AGMethodInvocationCreation(
  143. AGSymbol.jAlloc("name"),
  144. NATTable.of(AGSymbol.jAlloc("arg")),
  145. NATTable.EMPTY),
  146. Evaluator.getNil(),
  147. NATTable.of(NATNumber.ONE));
  148. AGMethodInvocationCreation inv = new AGMethodInvocationCreation(
  149. AGSymbol.jAlloc("name"),
  150. NATTable.of(AGSymbol.jAlloc("arg")),
  151. NATTable.EMPTY);
  152. inv.impl_setLocation(new SourceLocation(42, 0, "foo.at"));
  153. InvocationStack.getInvocationStack().methodInvoked(
  154. inv,
  155. Evaluator.getNil(),
  156. NATTable.of(NATNumber.ONE));
  157. log_.comment("theComment");
  158. // clean up after ourselves (make sure InvocationStack is empty)
  159. InvocationStack.getInvocationStack().methodReturned(null);
  160. InvocationStack.getInvocationStack().methodReturned(null);
  161. assertOutputEquals("stacktrace.json");
  162. }
  163. public void testTwoComments() throws IOException {
  164. log_.comment("comment1");
  165. log_.comment("comment2");
  166. assertOutputEquals("comments.json");
  167. }
  168. }