Skip to content

Commit

Permalink
merged pull request 'Add support for Slim symbols' #114 to fix issue #52
Browse files Browse the repository at this point in the history
  • Loading branch information
smartrics committed May 3, 2015
2 parents 00fb210 + 1156da6 commit bc0c359
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 220 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -14,7 +14,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>smartrics.restfixture</groupId>
<artifactId>smartrics-RestFixture</artifactId>
<version>3.2</version>
<version>4.0</version>
<name>RestFixture</name>
<url>https://github.com/smartrics/RestFixture</url>
<packaging>jar</packaging>
Expand Down Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>org.fitnesse</groupId>
<artifactId>fitnesse</artifactId>
<version>20140901</version>
<version>20150424</version>
</dependency>
<dependency>
<groupId>smartrics.restfixture</groupId>
Expand Down
13 changes: 8 additions & 5 deletions smartrics-RestFixture.iml
Expand Up @@ -11,16 +11,19 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.fitnesse:fitnesse:20140901" level="project" />
<orderEntry type="library" name="Maven: org.fitnesse:fitnesse:20150424" level="project" />
<orderEntry type="library" name="Maven: org.htmlparser:htmlparser:2.1" level="project" />
<orderEntry type="library" name="Maven: org.htmlparser:htmllexer:2.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.velocity:velocity:1.7" level="project" />
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.1" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.4" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.java-diff-utils:diffutils:1.2.1" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r" level="project" />
<orderEntry type="library" name="Maven: com.jcraft:jsch:0.1.46" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.java-diff-utils:diffutils:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jgit:org.eclipse.jgit:3.6.2.201501210735-r" level="project" />
<orderEntry type="library" name="Maven: com.jcraft:jsch:0.1.50" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.javaewah:JavaEWAH:0.7.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.1.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.1.4" level="project" />
<orderEntry type="library" name="Maven: smartrics.restfixture:smartrics-RestClient:2.1" level="project" />
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
Expand Down
Expand Up @@ -43,6 +43,13 @@
*
*/
public class PartsFactory {

private final BodyTypeAdapterFactory bodyTypeAdapterFactory;

public PartsFactory(final RunnerVariablesProvider variablesProvider) {
this.bodyTypeAdapterFactory = new BodyTypeAdapterFactory(variablesProvider);
}

/**
* Builds a rest client configured with the given config implementation.
*
Expand Down Expand Up @@ -115,6 +122,6 @@ public CellFormatter<?> buildCellFormatter(Runner runner) {
* {@link smartrics.rest.fitnesse.fixture.support.BodyTypeAdapter}
*/
public BodyTypeAdapter buildBodyTypeAdapter(ContentType ct, String charset) {
return BodyTypeAdapterFactory.getBodyTypeAdapter(ct, charset);
return bodyTypeAdapterFactory.getBodyTypeAdapter(ct, charset);
}
}
59 changes: 49 additions & 10 deletions src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java
Expand Up @@ -22,6 +22,9 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fitnesse.slim.StatementExecutorConsumer;
import fitnesse.slim.StatementExecutorInterface;
import smartrics.rest.client.RestClient;
import smartrics.rest.client.RestData.Header;
import smartrics.rest.client.RestRequest;
Expand All @@ -30,6 +33,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -166,7 +170,7 @@
*
* @author smartrics
*/
public class RestFixture {
public class RestFixture implements StatementExecutorConsumer, RunnerVariablesProvider {

/**
* What runner this table is running on.
Expand All @@ -190,6 +194,22 @@ public enum Runner {
*/
OTHER;
};

/* (non-Javadoc)
* @see smartrics.rest.fitnesse.fixture.RunnerVariablesProvider#createRunnerVariables()
*/
@Override
public Variables createRunnerVariables() {
switch (runner) {
case SLIM:
return new SlimVariables(config, slimStatementExecutor);
case FIT:
return new FitVariables(config);
default:
// Use FitVariables for tests
return new FitVariables(config);
}
}

private static final String LINE_SEPARATOR = "\n";

Expand Down Expand Up @@ -219,6 +239,8 @@ public enum Runner {

private Config config;

private Runner runner;

private boolean displayActualOnRight;

private boolean debugMethodCall = false;
Expand All @@ -245,12 +267,14 @@ public enum Runner {

private boolean followRedirects = true;

private StatementExecutorInterface slimStatementExecutor;

/**
* Constructor for Fit runner.
*/
public RestFixture() {
super();
this.partsFactory = new PartsFactory();
this.partsFactory = new PartsFactory(this);
this.displayActualOnRight = true;
this.minLenForCollapseToggle = -1;
this.resourceUrisAreEscaped = false;
Expand All @@ -275,7 +299,11 @@ public RestFixture(String hostName) {
* the value of cell number 3 in first row of the fixture table.
*/
public RestFixture(String hostName, String configName) {
this(new PartsFactory(), hostName, configName);
this.displayActualOnRight = true;
this.minLenForCollapseToggle = -1;
this.partsFactory = new PartsFactory(this);
this.config = Config.getConfig(configName);
this.baseUrl = new Url(stripTag(hostName));
}

/**
Expand Down Expand Up @@ -722,7 +750,7 @@ public void let() {
if (letHandler != null) {
StringTypeAdapter adapter = new StringTypeAdapter();
try {
sValue = letHandler.handle(getLastResponse(), namespaceContext, expr);
sValue = letHandler.handle(this, getLastResponse(), namespaceContext, expr);
exprCell.body(getFormatter().gray(exprCell.body()));
} catch (RuntimeException e) {
getFormatter().exception(exprCell, e.getMessage());
Expand Down Expand Up @@ -777,7 +805,7 @@ public void evalJs() {
"Missing string to evaluate)");
return;
}
JavascriptWrapper wrapper = new JavascriptWrapper();
JavascriptWrapper wrapper = new JavascriptWrapper(this);
Object result = null;
try {
result = wrapper.evaluateExpression(lastResponse, jsCell.body());
Expand Down Expand Up @@ -837,9 +865,10 @@ public void processRow(RowWrapper<?> currentRow) {
}

protected void initialize(Runner runner) {
this.runner = runner;
boolean state = validateState();
notifyInvalidState(state);
configFormatter(runner);
configFormatter();
configFixture();
configRestClient();
}
Expand Down Expand Up @@ -922,10 +951,15 @@ protected void doMethod(String method, String resUrl,
getLastRequest().setMultipartFileParameterName(
multipartFileParameterName);
String[] uri = resUrl.split("\\?", 2);

String[] thisRequestUrlParts = buildThisRequestUrl(uri[0]);
getLastRequest().setResource(thisRequestUrlParts[1]);
if (uri.length == 2) {
getLastRequest().setQuery(uri[1]);
if (uri.length > 1) {
String query = uri[1];
for (int i=2; i<uri.length; i++) {
query += "?" + uri[i]; //TODO: StringBuilder
}
getLastRequest().setQuery(query);
}
if ("Post".equals(method) || "Put".equals(method)) {
getLastRequest().setBody(rBody);
Expand Down Expand Up @@ -1089,7 +1123,7 @@ private String stripTag(String somethingWithinATag) {
return Tools.fromSimpleTag(somethingWithinATag);
}

private void configFormatter(Runner runner) {
private void configFormatter() {
formatter = partsFactory.buildCellFormatter(runner);
}

Expand All @@ -1098,7 +1132,7 @@ private void configFormatter(Runner runner) {
*/
private void configFixture() {

GLOBALS = new Variables(config);
GLOBALS = createRunnerVariables();

displayActualOnRight = config.getAsBoolean(
"restfixture.display.actual.on.right", displayActualOnRight);
Expand Down Expand Up @@ -1189,4 +1223,9 @@ private void configureCredentials() {
restClient = partsFactory.buildRestClient(newConfig);
}
}

@Override
public void setStatementExecutor(StatementExecutorInterface arg0) {
this.slimStatementExecutor = arg0;
}
}
@@ -0,0 +1,20 @@
/**
*
*/
package smartrics.rest.fitnesse.fixture;

import smartrics.rest.fitnesse.fixture.support.Variables;

/**
* The fixture provides the variables of the runner.
* This interface abstracts the fixture so that it can
* be tested easily.
*/
public interface RunnerVariablesProvider {

/**
* Get a variable store linked to the current runner environment.
*/
Variables createRunnerVariables();

}
Expand Up @@ -29,9 +29,9 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import util.Maybe;
import fitnesse.html.HtmlTag;
import fitnesse.wikitext.parser.Matcher;
import fitnesse.wikitext.parser.Maybe;
import fitnesse.wikitext.parser.Parser;
import fitnesse.wikitext.parser.Rule;
import fitnesse.wikitext.parser.Symbol;
Expand Down
Expand Up @@ -24,6 +24,8 @@
import java.util.HashMap;
import java.util.Map;

import smartrics.rest.fitnesse.fixture.RunnerVariablesProvider;

/**
* Depending on Content-Type passed in, it'll build the appropriate type adapter
* for parsing/rendering the cell content.
Expand All @@ -33,44 +35,60 @@
*/
public class BodyTypeAdapterFactory {

@SuppressWarnings("rawtypes")
private static Map<ContentType, Class> contentTypeToBodyTypeAdapter = new HashMap<ContentType, Class>();
static {
contentTypeToBodyTypeAdapter.put(ContentType.JS, JSONBodyTypeAdapter.class);
contentTypeToBodyTypeAdapter.put(ContentType.JSON, JSONBodyTypeAdapter.class);
contentTypeToBodyTypeAdapter.put(ContentType.XML, XPathBodyTypeAdapter.class);
contentTypeToBodyTypeAdapter.put(ContentType.TEXT, TextBodyTypeAdapter.class);
}

private BodyTypeAdapterFactory() {
private final RunnerVariablesProvider variablesProvider;

private Map<ContentType, BodyTypeAdapterCreator> contentTypeToBodyTypeAdapter =
new HashMap<ContentType, BodyTypeAdapterCreator>();
{
BodyTypeAdapterCreator jsonBodyTypeAdapterCreator = new BodyTypeAdapterCreator() {
@Override
public BodyTypeAdapter createBodyTypeAdapter() {
return new JSONBodyTypeAdapter(variablesProvider);
}
};
contentTypeToBodyTypeAdapter.put(ContentType.JS, jsonBodyTypeAdapterCreator);
contentTypeToBodyTypeAdapter.put(ContentType.JSON, jsonBodyTypeAdapterCreator);
contentTypeToBodyTypeAdapter.put(ContentType.XML, new BodyTypeAdapterCreator() {
@Override
public BodyTypeAdapter createBodyTypeAdapter() {
return new XPathBodyTypeAdapter();
}
});
contentTypeToBodyTypeAdapter.put(ContentType.TEXT, new BodyTypeAdapterCreator() {
@Override
public BodyTypeAdapter createBodyTypeAdapter() {
return new TextBodyTypeAdapter();
}
});;
}

public BodyTypeAdapterFactory(final RunnerVariablesProvider variablesProvider) {
this.variablesProvider = variablesProvider;
}

/**
* Returns a @link {@link BodyTypeAdapter} for the given charset and @link {@link ContentType}.
*
* @param content the contentType
* @param charset the charset.
* @return an instance of {@link BodyTypeAdapter}
*/
public static BodyTypeAdapter getBodyTypeAdapter(ContentType content, String charset) {
@SuppressWarnings("rawtypes")
Class aClass = contentTypeToBodyTypeAdapter.get(content);
if (aClass == null) {
public BodyTypeAdapter getBodyTypeAdapter(ContentType content, String charset) {
final BodyTypeAdapterCreator creator = contentTypeToBodyTypeAdapter.get(content);
if (creator == null) {
throw new IllegalArgumentException("Content-Type is UNKNOWN. Unable to find a BodyTypeAdapter to instantiate.");
}
BodyTypeAdapter instance = null;
try {
instance = (BodyTypeAdapter) aClass.newInstance();
if (charset != null) {
instance.setCharset(charset);
} else {
instance.setCharset(Charset.defaultCharset().name());
}
} catch (InstantiationException e) {
throw new IllegalStateException("Unable to instantiate a the BodyTypeAdapter for " + content + "(" + aClass.getName() + ")");
} catch (IllegalAccessException e) {
throw new IllegalStateException("Unable access ctor to instantiate a the BodyTypeAdapter for " + content + "(" + aClass.getName() + ")");
final BodyTypeAdapter instance = creator.createBodyTypeAdapter();
if (charset != null) {
instance.setCharset(charset);
} else {
instance.setCharset(Charset.defaultCharset().name());
}
return instance;
}

interface BodyTypeAdapterCreator {
BodyTypeAdapter createBodyTypeAdapter();
}

}

0 comments on commit bc0c359

Please sign in to comment.