修改fitnesse源码->自定义页面脚本中各种Table(ScriptTable,DecisionTable)的类实例变量

修改fitnesse源码,使得页面的table中实例化的类分配到自定义的变量中

只需要修改源码工程中的2个类方法

1. 需要修改fitnesse.testsystems.slim.tables.Disgracer类中的disgraceName()方法

 
  
private String disgraceName() {
  disgracedName = new StringBuffer();

  /**
   * add by yefengsong
   * 使分隔符能正常使用
   */
  if(name.contains("->")){
    return name;
  }

  for (char c : name.toCharArray()) {
    appendCharInProperCase(c);
  }

  return disgracedName.toString();
}



2. 需要修改fitnesse.slim.SlimExecutionContext类中的create()

public void create(String instanceName, String className, Object[] args)
  throws SlimError, IllegalArgumentException, IllegalAccessException, InvocationTargetException,
  InstantiationException {
  String custom_instance_name = null;

  /**
   * add by yefengsong
   * 通过分隔符->分别获取到实例名和类名
   */
  if(className.contains("->")){
    String arr[] = className.split("->");
    custom_instance_name = arr[0].trim();
    className = arr[1].trim();
  }

  Object potentialActor = variables.getStored(className);
  if (potentialActor != null && !(potentialActor instanceof String)) {
    addToInstancesOrLibrary(instanceName, potentialActor);
  } else {
    String replacedClassName = variables
      .replaceSymbolsInString(className);
    Object instance = interaction.createInstance(paths,
      replacedClassName, replaceSymbols(args));

    /**
     * add by yefengsong
     * 类实例绑定自定义变量
     */
    setVariable(custom_instance_name,instance);

    addToInstancesOrLibrary(instanceName, instance);
  }
}

修改后验证代码作用, 假设有java代码如下

public class A {
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}


public class B {
  private A a;
  private int c;

  public A getA() {
    return a;
  }

  public void setA(A a) {
    this.a = a;
  }

  public int getC() {
    return c;
  }

  public void setC(int c) {
    this.c = c;
  }


}

对应的fitnesse wiki(DecisionTable)脚本如下:

|import        |
|com.quark.test|

|a -> A|
|name  |
|测试    |

|b -> B|
|a     |
|$a    |

|script|json util      |
|show  |toJsonString|$b|


执行结果:

import
com.quark.test

a -> A
name
测试

b -> B
a
$a

script json util
show toJsonString $b {"a":{"name":"测试"},"c":0}


你可能感兴趣的:(fitnesse)