0

TLDR:我想知道如何扩展 fit.TypeAdaptor 以便我可以调用一个期望参数作为默认实现的方法 TypeAdaptor 通过反射调用绑定(绑定?)方法并假设它是一个无参数方法......

更长的版本 - 我正在使用 fit 为我的系统构建一个测试工具(一个返回自定义对象排序列表的服务)。为了验证系统,我想我会使用 fit.RowFixture 来断言列表项的属性。

由于 RowFixture 期望数据是公共属性或公共方法,所以我想在我的自定义对象上使用包装器(比如 InstanceWrapper)——我还尝试实现上一个线程中给出的关于在 RowFixture 中格式化数据的建议。

问题是我的自定义对象有大约 41 个属性,我想为测试人员提供选择他们想要在这个 RowFixture 中验证哪些属性的选项。另外,除非我向我的 InstanceWrapper 类动态添加字段/方法,否则 RowFixture 将如何调用我的任何一个 getter,因为两者都希望属性名称作为参数传递(代码复制如下)?我扩展了 RowFixture 以绑定我的方法,但我不确定如何扩展 TypeAdaptor 以便它使用 attr 名称进行调用。有什么建议吗?

public class InstanceWrapper {

    private Instance instance;
    private Map<String, Object> attrs;
    public int index;

    public InstanceWrapper() {
        super();
    }

    public InstanceWrapper(Instance instance) {
        this.instance = instance;
        init(); // initialise map
    }

    private void init() {
        attrs = new HashMap<String, Object>();
        String attrName;
        for (AttrDef attrDef : instance.getModelDef().getAttrDefs()) {
            attrName = attrDef.getAttrName();
            attrs.put(attrName, instance.getChildScalar(attrName));
        }
    }

    public String getAttribute(String attr) {
        return attrs.get(attr).toString();
    }

    public String description(String attribute) {
        return instance.getChildScalar(attribute).toString();
    }
}

public class MyDisplayRules extends fit.RowFixture {

    @Override
    public Object[] query() {
        List<Instance> list = PHEFixture.hierarchyList;
        return convertInstances(list);
    }

    private Object[] convertInstances(List<Instance> instances) {
        Object[] objects = new Object[instances.size()];
        InstanceWrapper wrapper;
        int index = 0;
        for (Instance instance : instances) {
            wrapper = new InstanceWrapper(instance);
            wrapper.index = index;
            objects[index++] = wrapper;
        }
        return objects;
    }

    @Override
    public Class getTargetClass() {
        return InstanceWrapper.class;
    }

    @Override
    public Object parse(String s, Class type) throws Exception {
        return super.parse(s, type);
    }

    @Override
    protected void bind(Parse heads) {

        columnBindings = new TypeAdapter[heads.size()];
        for (int i = 0; heads != null; i++, heads = heads.more) {
            String name = heads.text();
            String suffix = "()";
            try {
                if (name.equals("")) {
                    columnBindings[i] = null;
                } else if (name.endsWith(suffix)) {
                    columnBindings[i] = bindMethod("description", name.substring(0, name.length()
                            - suffix.length()));
                } else {
                    columnBindings[i] = bindField(name);
                }
            } catch (Exception e) {
                exception(heads, e);
            }
        }
    }

    protected TypeAdapter bindMethod(String name, String attribute) throws Exception {
        Class partypes[] = new Class[1];
        partypes[0] = String.class;

        return PHETypeAdaptor.on(this, getTargetClass().getMethod("getAttribute", partypes), attribute);
    }
}
4

1 回答 1

0

对于它的价值,这是我最终解决这个问题的方法:

我创建了一个TypeAdapter带有附加公共属性 (String) 的自定义(扩展 TypeAdapter)attrName。还:

@Override
public Object invoke() throws IllegalAccessException, InvocationTargetException {

    if ("getAttribute".equals(method.getName())) {
        Object params[] = { attrName };
        return method.invoke(target, params);
    } else {
        return super.invoke();
    }
}

然后我扩展fit.RowFixture并进行了以下覆盖:

  • public getTargetClass()- 返回我的班级参考资料
  • protected TypeAdapter bindField(String name)throws Exception - 这是一个受保护的方法ColumnFixture,我在其中进行了修改,以便它使用我的类的 getter 方法:

    @Override
    protected TypeAdapter bindField(String name) throws Exception {
    
        String fieldName = camel(name);
    
        // for all attributes, use method getAttribute(String)
        Class methodParams[] = new Class[1];
        methodParams[0] = String.class;
    
        TypeAdapter a = TypeAdapter.on(this, getTargetClass().getMethod("getAttribute", methodParams));
    
        PHETypeAdapter pheAdapter = new PHETypeAdapter(fieldName);
        pheAdapter.target = a.target;
        pheAdapter.fixture = a.fixture;
        pheAdapter.field = a.field;
        pheAdapter.method = a.method;
        pheAdapter.type = a.type;
    
        return pheAdapter;
    
    }
    

我知道这不是一个很好的解决方案,但这是我能想到的最好的解决方案。也许我会在这里得到一些更好的解决方案:-)

于 2011-06-15T09:22:22.653 回答