Monday, May 24, 2010

ADFbc:Common Util class for testing CRUD operation of an EO based VO

A simple common util class that your Junit class can extend for verifying standard CRUD operation for a a single EO based VO.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.server.AttributeListImpl;
import oracle.jbo.server.ViewObjectImpl;


public class AtkCommonVOActionsTest {
public AtkCommonVOActionsTest() {
super();
}

public Row testCreateRow(ViewObjectImpl viewObject, Map<String,Object> attributeList){
AttributeListImpl initVals = createAttributeList(attributeList);
Row row = viewObject.createAndInitRow(initVals);
return row;
}

public Row testUpdateRow(ViewObjectImpl viewObject, Map<String,Object> attributeList, Key pkObject){
if(pkObject != null){
Row[] rows = viewObject.findByKey(pkObject, 1);
if(rows != null && rows.length > 0){
Row row = rows[0];
List<String> keyList = getKeyList(attributeList);
List<Object> valueList = getValueList(attributeList);
row.setAttributeValues(keyList, valueList);
return row;
}

}
return null;
}


public Row testDeleteRow(ViewObjectImpl viewObject, Key pkObject){
if(pkObject != null){
Row[] rows = viewObject.findByKey(pkObject, 1);
if(rows != null && rows.length > 0){
Row row = rows[0];
row.remove();
return row;
}
}
return null;
}



private AttributeListImpl createAttributeList(Map<String,Object> attributeList){
AttributeListImpl attrList = new AttributeListImpl();
Iterator<String> keys = attributeList.keySet().iterator();
while(keys.hasNext()){
String key = keys.next();
Object value = attributeList.get(key);
attrList.setAttribute(key, value);
}
return attrList;
}

private List<String> getKeyList(Map<String,Object> attributeList){
List<String> keyList = new ArrayList<String>();
Iterator<String> keys = attributeList.keySet().iterator();
while(keys.hasNext()){
String key = keys.next();
keyList.add(key);
}
return keyList;
}

private List getValueList(Map<String,Object> attributeList){
List valueList = new ArrayList();
Iterator<String> keys = attributeList.keySet().iterator();
while(keys.hasNext()){
String key = keys.next();
Object value = attributeList.get(key);
valueList.add(value);
}
return valueList;
}



}

1 comment:

Anonymous said...

I don't see anything test related in here (?), looks like a generic CRUD helper.

How are you mocking out the ADF BC infrastructure classes? Or does your 'unit test' depend on a database?

Not sure what you're trying to test here :\