/*
* Copyright(C) 2002 Mika Riekkinen, Joni Suominen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or(at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package alt.jiapi;
import junit.framework.TestCase;
import alt.jiapi.instrumentor.CombInstrumentor;
import alt.jiapi.reflect.instruction.InstructionList;
import alt.jiapi.reflect.JiapiClass;
import alt.jiapi.reflect.JiapiMethod;
import alt.jiapi.reflect.Loader;
/**
* A JUnit test case for Instrumentors, that do not modify bytecode,
* but selects parts of it forwards them. This strategy cannot be
* applied to all such Instrumentors.
*
* @author Mika Riekkinen
* @author Joni Suominen
* @version $Revision: 1.4 $ $Date: 2002/05/03 16:02:45 $
*/
public class InstrumentorTest extends TestCase {
JiapiMethod[] methods;
public InstrumentorTest(String name) {
super(name);
}
protected void setUp() {
try {
Loader loader = new Loader();
JiapiClass clazz = loader.loadClass("alt.jiapi.InstrumentorTest");
methods = clazz.getDeclaredMethods();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testInstrumentors() {
combTest0(); // 0x1 : forwards all
combTest1(); // 0x2 : block, forward, block, forward, ...
}
private void combTest0() {
TestCombInstrumentor combInstrumentor =
new TestCombInstrumentor(0x1); // pattern 1; forwards all.
for(int i = 0; i < methods.length; i++) {
combInstrumentor.instrument(methods[i].getInstructionList());
assertTrue(!combInstrumentor.isBlocked()); // Should forward
}
}
private void combTest1() {
TestCombInstrumentor combInstrumentor =
new TestCombInstrumentor(0x2); // pattern 10
for(int i = 0; i < methods.length; i++) {
combInstrumentor.instrument(methods[i].getInstructionList());
assertTrue(combInstrumentor.isBlocked()); // Should block
if (i < methods.length - 1) {
combInstrumentor.instrument(methods[i+1].getInstructionList());
assertTrue(!combInstrumentor.isBlocked()); // Should forward
}
}
}
// Used in testing. Grep'n'Split.
private void someMethod() {
foo();
bar();
}
// Couple of empty methods for testing. Comb'n'Null.
private void foo() {}
private void bar() {}
private void baz() {}
private void foobar() {}
private void foobaz() {}
//
// Inner classes to be used by this test
//
class TestCombInstrumentor extends CombInstrumentor {
public TestCombInstrumentor(int i) {
super(i);
}
private boolean forwardCalled = false;
public void forward(InstructionList il) {
forwardCalled = true;
}
public boolean isBlocked() {
boolean retVal = true;
if (forwardCalled) {
retVal = false;
}
forwardCalled = false;
return retVal;
}
}
}