/* * Copyright 2007 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 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 General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2.Seq; import java.io.IOException; import java.io.ObjectOutput; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import Sequenic.P2.StringFormater; import Sequenic.T2.Pool; import Sequenic.T2.Msg.T2Error; /** * This represents a test-step where we update a field of a target object. */ public class UPDATE_FIELD extends TraceStep { public Field field; public MkValStep val; /** * @param f The field to update. * @param v A MkVal step producing a new value for the field f. */ public UPDATE_FIELD(Field f, MkValStep v) { field = f; val = v; } public String getName() { return field.getName(); } @Override public void write(ObjectOutput out) throws IOException { new ObsoleteTraceStep( StringList.create(field.getDeclaringClass().getName(), field.getName()) , val).write(out); } /** * @see Sequenic.T2.Seq.TraceStep#exec */ public ExecResult exec(Class CUT, Pool pool, Object targetObj, int stepNumber, List classinvs, ReportersPool reporters) { SlotsInjector.injectAtTestStep(targetObj, this); field.setAccessible(true); ExecResult result = new ExecResult(CUT,this,targetObj); Object newFieldVal = null ; // System.out.println(">" + step_.field.getName()) ; // System.out.println(">" + MkVal(pool,step_.val)) ; // System.out.println("+" + targetObj) ; try { newFieldVal = val.exec(CUT,pool); result.args = new Object[] { newFieldVal } ; assert (targetObj != null); field.set(targetObj, newFieldVal); result.execClassInv(classinvs, targetObj); } catch (InvocationTargetException e) { // First mark argument failure: result.argumentFailure = e.getCause(); // Then categorize the exception: // result.checkException(e); } catch (Exception e) { throw new T2Error("Fail to update field " + this , e); } // Reporting, unless if NULLreporter is used: reporters.reportStep(result,stepNumber) ; return result; } @Override public String toString() { return "UPDATE_FIELD, " + field.getName() + " with " + StringFormater.indentButFirst(val.toString(),4) ; } @Override public boolean isObsolete() { return false; } }