com.astamuse.asta4d.render.test.RendererTester Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of asta4d-core Show documentation
Show all versions of asta4d-core Show documentation
core functionalities of asta4d framework, including template and snippt implemention
package com.astamuse.asta4d.render.test;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.nodes.Element;
import com.astamuse.asta4d.render.AttributeSetter;
import com.astamuse.asta4d.render.Renderer;
import com.astamuse.asta4d.render.transformer.Transformer;
import com.astamuse.asta4d.util.collection.ListConvertUtil;
import com.astamuse.asta4d.util.collection.RowConvertor;
public class RendererTester {
private Renderer renderer;
private RendererTester(Renderer renderer) {
this.renderer = renderer;
}
public final static RendererTester forRenderer(Renderer renderer) {
return new RendererTester(renderer);
}
public final static List forRendererList(List rendererList) {
return ListConvertUtil.transform(rendererList, new RowConvertor() {
@Override
public RendererTester convert(int rowIndex, Renderer obj) {
return new RendererTester(obj);
}
});
}
private List>> retrieveNonAttrTransformer(String selector) {
List>> rtnList = new LinkedList<>();
List rendererList = renderer.asUnmodifiableList();
for (Renderer r : rendererList) {
if (r.getSelector().equals(selector)) {
List> list = r.getTransformerList();
if (list.isEmpty()) {
rtnList.add(list);
} else {
Transformer> t = list.get(0);
if (t.getContent() instanceof AttributeSetter) {
continue;
} else {
rtnList.add(list);
}
}
}
}
return rtnList;
}
private List> retrieveSingleTransformerListOnSelector(String selector) {
List>> list = retrieveNonAttrTransformer(selector);
if (list.isEmpty()) {
throw new RendererTestException("There is no value to be rendered for selector:[" + selector + "]");
} else if (list.size() > 1) {
throw new RendererTestException("There are more than one value to be rendered for selector:[" + selector +
"], maybe it is rendered multiple times?");
} else {
return list.get(0);
}
}
// following are simple render value accessores for simple test cases
private Transformer> retrieveSingleTransformerOnSelector(String selector) {
List> list = retrieveSingleTransformerListOnSelector(selector);
if (list.isEmpty()) {
throw new RendererTestException("There is no value to be rendered for selector:[" + selector +
"], maybe it is rendered as a empty list?");
} else if (list.size() > 1) {
throw new RendererTestException("There are more than one value to be rendered for selector:[" + selector +
"], maybe it is rendered as a list?");
} else {
return list.get(0);
}
}
@SuppressWarnings("rawtypes")
public Object get(String selector) {
Transformer t = retrieveSingleTransformerOnSelector(selector);
Object content = retrieveTestContentFromTransformer(t);
return content;
}
public List