PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 28package edu.vub.at.trace;
 29
 30import edu.vub.at.actors.natives.NATActorMirror;
 31import edu.vub.at.actors.natives.NATAsyncMessage;
 32import edu.vub.at.eval.Evaluator;
 33import edu.vub.at.eval.InvocationStack;
 34import edu.vub.at.exceptions.InterpreterException;
 35import edu.vub.at.exceptions.XIllegalIndex;
 36import edu.vub.at.exceptions.XIllegalOperation;
 37import edu.vub.at.objects.natives.NATNumber;
 38import edu.vub.at.objects.natives.NATObject;
 39import edu.vub.at.objects.natives.NATTable;
 40import edu.vub.at.objects.natives.grammar.AGMethodInvocationCreation;
 41import edu.vub.at.objects.natives.grammar.AGSymbol;
 42import edu.vub.at.parser.SourceLocation;
 43
 44import java.io.BufferedReader;
 45import java.io.IOException;
 46import java.io.InputStream;
 47import java.io.InputStreamReader;
 48import java.io.StringWriter;
 49
 50import junit.framework.TestCase;
 51
 52/**
 53 * @author tvcutsem
 54 *
 55 * Tests the tracing implementation for Causeway. 
 56 */
 57public class TraceTest extends TestCase {
 58
 59	private Tracer log_;
 60	private StringWriter output_;
 61	
 62    private class TestMarker implements Marker {
 63    	private int turnCounter = 0;
 64        public Anchor apply() { return new Anchor(new Turn("testLoop", 0), turnCounter++); }
 65    }
 66	
 67	public void setUp() throws IOException {
 68		output_ = new StringWriter();
 69		log_ = new Tracer(output_, new TestMarker());
 70	}
 71	
 72	public void tearDown() {
 73		log_ = null;
 74		output_ = null;
 75	}
 76	
 77	/**
 78	 * Loads the content of a file and returns that content as a Java String.
 79	 */
 80	public static final String loadFile(Class forTestClass, String fileName) throws IOException {
 81		InputStream in = forTestClass.getResource(fileName).openStream();
 82		try {
 83			BufferedReader dis = new BufferedReader(new InputStreamReader(in));;
 84			StringBuffer fBuf = new StringBuffer();
 85			String line;
 86
 87			while ( (line = dis.readLine()) != null) {
 88			  fBuf.append(line + "\n");
 89			}
 90		    return fBuf.toString();
 91		} finally {
 92			if (in != null) in.close();
 93		}
 94	}
 95	
 96	private final void assertOutputEquals(String fileName) throws IOException {
 97		log_.close();
 98		assertEquals(loadFile(this.getClass(), fileName).replaceAll("\\s", ""),
 99				     output_.toString().replaceAll("\\s", ""));
100	}
101	
102	public void testComment() throws IOException {
103		log_.comment("theComment");
104		assertOutputEquals("comment.json");
105	}
106	
107	public void testFulfilled() throws IOException {
108		log_.fulfilled("theCondition", Evaluator.getNil(), Evaluator.getNil());
109		assertOutputEquals("fulfilled.json");
110	}
111	
112	public void testGot() throws IOException, InterpreterException {
113		log_.got("theMessage",
114				// <-name(arg)@[]
115				new NATActorMirror.NATLetter(
116						null,
117						new NATObject(),
118						new NATAsyncMessage(AGSymbol.jAlloc("selector"),
119						            NATTable.of(NATNumber.ONE),
120								    NATTable.EMPTY)));
121		assertOutputEquals("got.json");
122	}
123	
124	public void testProblem() throws IOException {
125		log_.problem(new XIllegalOperation("theProblem", new XIllegalIndex("theCause")));
126		assertOutputEquals("problem.json");
127	}
128	
129	public void testProgressed() throws IOException {
130		log_.progressed("theCondition");
131		assertOutputEquals("progressed.json");
132	}
133	
134	public void testRejected() throws IOException {
135		log_.rejected("theCondition",
136				new XIllegalOperation("theReason"),
137				Evaluator.getNil(), Evaluator.getNil());
138		assertOutputEquals("rejected.json");
139	}
140	
141	public void testResolved() throws IOException {
142		log_.resolved("theCondition");
143		assertOutputEquals("resolved.json");
144	}
145	
146	public void testReturned() throws IOException {
147		log_.returned("theMessage");
148		assertOutputEquals("returned.json");
149	}
150	
151	public void testSent() throws IOException {
152		log_.sent("theMessage");
153		assertOutputEquals("sent.json");
154	}
155	
156	public void testSentIf() throws IOException {
157		log_.sentIf("theMessage", "theCondition");
158		assertOutputEquals("sentif.json");
159	}
160	
161	public void testStackTrace() throws IOException, InterpreterException {
162		// invoked nil.name(arg := 1)
163		InvocationStack.getInvocationStack().methodInvoked(
164				new AGMethodInvocationCreation(
165				  AGSymbol.jAlloc("name"),
166	              NATTable.of(AGSymbol.jAlloc("arg")),
167			      NATTable.EMPTY),
168				Evaluator.getNil(),
169				NATTable.of(NATNumber.ONE));
170		AGMethodInvocationCreation inv = new AGMethodInvocationCreation(
171				  AGSymbol.jAlloc("name"),
172	              NATTable.of(AGSymbol.jAlloc("arg")),
173			      NATTable.EMPTY);
174		inv.impl_setLocation(new SourceLocation(42, 0, "foo.at"));
175		InvocationStack.getInvocationStack().methodInvoked(
176				inv,
177				Evaluator.getNil(),
178				NATTable.of(NATNumber.ONE));
179		log_.comment("theComment");
180		// clean up after ourselves (make sure InvocationStack is empty)
181		InvocationStack.getInvocationStack().methodReturned(null);
182		InvocationStack.getInvocationStack().methodReturned(null);
183		assertOutputEquals("stacktrace.json");
184	}
185	
186	public void testTwoComments() throws IOException {
187		log_.comment("comment1");
188		log_.comment("comment2");
189		assertOutputEquals("comments.json");
190	}
191	
192}