com.googlecode.gwt.test.internal.handlers.DeferredReplaceWithCreateHandler Maven / Gradle / Ivy
package com.googlecode.gwt.test.internal.handlers;
import com.google.gwt.core.client.GWT;
import com.googlecode.gwt.test.GwtCreateHandler;
import com.googlecode.gwt.test.internal.GwtConfig;
import com.googlecode.gwt.test.internal.ModuleData;
import com.googlecode.gwt.test.internal.ModuleData.ReplaceWithData;
import com.googlecode.gwt.test.utils.GwtReflectionUtils;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class DeferredReplaceWithCreateHandler implements GwtCreateHandler {
public Object create(Class classLiteral) throws Exception {
String moduleName = GwtConfig.get().getTestedModuleName();
List replaceWithList = ModuleData.get(moduleName).getReplaceWithListMap().get(
classLiteral.getName().replaceAll("\\$", "."));
// not handled by a element in any .gwt.xml file
if (replaceWithList == null) {
return null;
}
String replaceWith = getReplaceWithClass(replaceWithList);
return (replaceWith != null && !replaceWith.equals(classLiteral.getCanonicalName()))
? GWT.create(GwtReflectionUtils.getClass(replaceWith)) : null;
}
private String getReplaceWithClass(List replaceWithList) {
// the default , with no filter
ReplaceWithData defaultReplaceWith = null;
Set clientProperyNames = GwtConfig.get().getModuleRunner().getClientPropertyNames();
for (ReplaceWithData replaceWithData : replaceWithList) {
if ((clientProperyNames.size() == 0)) {
if (isDefault(replaceWithData)) {
// default case : nothing is specified
return replaceWithData.getReplaceWith();
}
// not a element to use
continue;
} else if (isDefault(replaceWithData)) {
// save the default , to use
defaultReplaceWith = replaceWithData;
continue;
}
// validate every when-property-is match
if (replaceWithData.hasWhenPropertyIs()) {
boolean validateProperties = true;
Iterator it = clientProperyNames.iterator();
while (it.hasNext() && validateProperties) {
String propertyName = it.next();
String value = GwtConfig.get().getModuleRunner().getClientProperty(propertyName);
validateProperties = replaceWithData.whenPropertyIsMatch(propertyName, value);
}
if (!validateProperties) {
continue;
}
}
// validate at least one any/when-property-is matches
boolean validateAnyProperty = !replaceWithData.hasAnyWhenPropertyIs();
Iterator it = clientProperyNames.iterator();
while (it.hasNext() && !validateAnyProperty) {
String propertyName = it.next();
String value = GwtConfig.get().getModuleRunner().getClientProperty(propertyName);
validateAnyProperty = replaceWithData.anyMatch(propertyName, value);
}
if (validateAnyProperty) {
return replaceWithData.getReplaceWith();
}
}
return (defaultReplaceWith != null) ? defaultReplaceWith.getReplaceWith() : null;
}
private boolean isDefault(ReplaceWithData replaceWithData) {
return !replaceWithData.hasAnyWhenPropertyIs() && !replaceWithData.hasWhenPropertyIs();
}
}