Test Wrapper for domain model.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@727574 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Britton Isbell 2008-12-17 23:17:47 +00:00
parent 04b358629c
commit 1ab2db1205
2 changed files with 160 additions and 0 deletions

View File

@ -69,6 +69,12 @@ under the License.
<groupId>org.apache.maven</groupId>
<artifactId>maven-project-builder</artifactId>
</dependency>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -0,0 +1,154 @@
package org.apache.maven.project.harness;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.maven.project.builder.PomClassicDomainModel;
import org.apache.maven.shared.model.DataSourceException;
import org.apache.maven.shared.model.ModelDataSource;
import org.apache.maven.shared.model.ModelProperty;
import org.apache.maven.shared.model.impl.DefaultModelDataSource;
import org.apache.maven.model.Model;
public class PomTestWrapper {
private PomClassicDomainModel domainModel;
private JXPathContext context;
public PomTestWrapper(PomClassicDomainModel domainModel) throws IOException {
if(domainModel == null) {
throw new IllegalArgumentException("domainModel: null");
}
this.domainModel = domainModel;
context = JXPathContext.newContext(domainModel.getModel());
}
public PomTestWrapper(File file) throws IOException {
if(file == null) {
throw new IllegalArgumentException("file: null");
}
this.domainModel = new PomClassicDomainModel(file);
context = JXPathContext.newContext(domainModel.getModel());
}
public PomTestWrapper(Model model) throws IOException {
if(model == null) {
throw new IllegalArgumentException("model: null");
}
this.domainModel = new PomClassicDomainModel(model);
context = JXPathContext.newContext(domainModel.getModel());
}
public String getValueOfProjectUri(String projectUri, boolean withResolvedValue) throws IOException {
if(projectUri.contains("#collection") || projectUri.contains("#set")) {
throw new IllegalArgumentException("projectUri: contains a collection or set");
}
return asMap(withResolvedValue).get(projectUri);
}
public void setValueOnModel(String expression, Object value) {
context.setValue(expression, value);
}
/*
public int containerCountForUri(String uri) throws IOException {
if(uri == null || uri.trim().equals("")) {
throw new IllegalArgumentException("uri: null or empty");
}
ModelDataSource source = new DefaultModelDataSource();
source.init(domainModel.getModelProperties(), null);
return source.queryFor(uri).size();
}
*/
public Iterator getIteratorForXPathExpression(String expression) {
return context.iterate(expression);
}
public boolean containsXPathExpression(String expression) {
return context.getValue(expression) != null;
}
public boolean xPathExpressionEqualsValue(String expression, String value) {
return context.getValue(expression) != null && context.getValue(expression).equals(value);
}
public Map<String, String> asMap(boolean withResolvedValues) throws IOException {
Map<String, String> map = new HashMap<String, String>();
for(ModelProperty mp : domainModel.getModelProperties()) {
if(withResolvedValues) {
map.put(mp.getUri(), mp.getResolvedValue());
} else {
map.put(mp.getUri(), mp.getValue());
}
}
return map;
}
public boolean containsModelProperty(ModelProperty modelProperty) throws IOException {
return domainModel.getModelProperties().contains(modelProperty);
}
public boolean containsAllModelPropertiesOf(List<ModelProperty> modelProperties) throws IOException {
for(ModelProperty mp : modelProperties) {
if(!containsModelProperty(mp)) {
return false;
}
}
return true;
}
public boolean matchModelProperties(List<ModelProperty> hasProperties, List<ModelProperty> doesNotHaveProperties) throws IOException {
return containsAllModelPropertiesOf(hasProperties) && containNoModelPropertiesOf(doesNotHaveProperties);
}
public boolean matchUris(List<String> hasAllUris, List<String> doesNotHaveUris) throws IOException {
return hasAllUris(hasAllUris) && hasNoUris(doesNotHaveUris);
}
public boolean containNoModelPropertiesOf(List<ModelProperty> modelProperties) throws IOException {
for(ModelProperty mp : modelProperties) {
if(containsModelProperty(mp)) {
return false;
}
}
return true;
}
public boolean hasUri(String uri) throws IOException {
for(ModelProperty mp : domainModel.getModelProperties()) {
if(mp.getValue().equals(uri)) {
return true;
}
}
return false;
}
public boolean hasAllUris(List<String> uris) throws IOException {
for(String s : uris) {
if(!hasUri(s)) {
return false;
}
}
return true;
}
public boolean hasNoUris(List<String> uris) throws IOException {
for(String s : uris) {
if(hasUri(s)) {
return false;
}
}
return true;
}
}