Further cleanup: Upgraded Dropwizard to the latest, also Jersey to 2.x. Removed Spring dependencies (we haven't utilized it apart from a few IoC-related annotations, which I have replaced with a few lines of code in FreeMarkerOnlineTester, following the Dropwizard convention). Some other minor cleanups.
diff --git a/build.gradle b/build.gradle
index c700e9b..9a010ee 100644
--- a/build.gradle
+++ b/build.gradle
@@ -55,30 +55,16 @@
compile libraries.dropwizard_views
compile libraries.dropwizard_views_freemarker
compile libraries.dropwizard_assets
- compile libraries.springCore
- compile libraries.springContext
- compile libraries.springContextSupport
- compile libraries.springExpression
- compile libraries.springSecurityCore
- compile libraries.springTransaction
- compile libraries.springBeans
- compile libraries.springWeb
-
- testCompile libraries.dropwizard_testing
- testCompile libraries.springTest
- testCompile libraries.junit
- testCompile libraries.mockito
- testCompile libraries.hamcrest
-
compile libraries.freemarker
compile libraries.jackson_databind
compile libraries.commonLangs
compile libraries.findBugs
- testCompile libraries.selenium_java
+ testCompile libraries.dropwizard_testing
+ testCompile libraries.junit
+ testCompile libraries.mockito
+ testCompile libraries.hamcrest
testCompile libraries.jersey_grrizle
- testCompile libraries.jersey_client
- testCompile libraries.springJersey
}
compileJava {
diff --git a/dependencies.gradle b/dependencies.gradle
index b5d1598..a27abc2 100644
--- a/dependencies.gradle
+++ b/dependencies.gradle
@@ -17,51 +17,32 @@
* under the License.
*/
-project.ext.set("libraries", "")
-ext.dw_version = "1.1.1";
-ext.spring_version = "4.3.9.RELEASE";
+ext.dropwizard_version = "1.1.1";
ext.jackson_version = "2.8.9";
+ext.jeresy_version = "2.25";
+ext.libraries = [
+ // Dropwizard:
+ dropwizard: "io.dropwizard:dropwizard-core:$dropwizard_version",
+ dropwizard_client: "io.dropwizard:dropwizard-client:$dropwizard_version",
+ dropwizard_views: "io.dropwizard:dropwizard-views:$dropwizard_version",
+ dropwizard_views_freemarker: "io.dropwizard:dropwizard-views-freemarker:$dropwizard_version",
+ dropwizard_assets: "io.dropwizard:dropwizard-assets:$dropwizard_version",
-project.libraries = [
- dropwizard: "io.dropwizard:dropwizard-core:$dw_version",
- dropwizard_client: "io.dropwizard:dropwizard-client:$dw_version",
- dropwizard_views: "io.dropwizard:dropwizard-views:$dw_version",
- dropwizard_views_freemarker: "io.dropwizard:dropwizard-views-freemarker:$dw_version",
- dropwizard_assets: "io.dropwizard:dropwizard-assets:$dw_version",
- guava: 'com.google.guava:guava:13.0.1',
- jersey_core: 'com.sun.jersey:jersey-core:1.1.4.1',
-
- //Spring
- springCore: "org.springframework:spring-core:$spring_version",
- springContext: "org.springframework:spring-context:$spring_version",
- springContextSupport: "org.springframework:spring-context-support:$spring_version",
- springExpression: "org.springframework:spring-expression:$spring_version",
- springTransaction: "org.springframework:spring-tx:$spring_version",
- springBeans: "org.springframework:spring-beans:$spring_version",
- springJersey: "com.sun.jersey.contribs:jersey-spring:1.7",
- springWeb: "org.springframework:spring-web:$spring_version",
-
- // Security
- // Spring and Spring Security support for dropwizard
- springSecurityCore: "org.springframework.security:spring-security-core:3.1.4.RELEASE",
- dropwizardAuth: "io.dropwizard:dropwizard-auth:$dw_version",
-
- // Spring test
- springTest: "org.springframework:spring-test:$spring_version",
-
- //Test libs
- dropwizard_testing: "io.dropwizard:dropwizard-testing:$dw_version",
- junit: 'junit:junit-dep:4.11',
- mockito: 'org.mockito:mockito-core:1.9.0',
- hamcrest: 'org.hamcrest:hamcrest-library:1.3',
- selenium_java: 'org.seleniumhq.selenium:selenium-java:2.41.0',
- jersey_client: 'com.sun.jersey:jersey-client:1.17.1',
- jersey_grrizle: 'com.sun.jersey.jersey-test-framework:jersey-test-framework-grizzly:1.17',
-
- // Others
- freemarker: 'org.freemarker:freemarker:2.3.26-incubating',
+ // App. specific:
+ freemarker: "org.freemarker:freemarker:2.3.26-incubating",
+ jersey_core: "org.glassfish.jersey.core:jersey-core:$jeresy_version",
jackson_databind: "com.fasterxml.jackson.core:jackson-databind:$jackson_version",
- commonLangs: "org.apache.commons:commons-lang3:3.3.2",
- findBugs: "com.google.code.findbugs:annotations:3.0.0"
+ // Commons:
+ findBugs: "com.google.code.findbugs:annotations:3.0.0",
+ commonLangs: "org.apache.commons:commons-lang3:3.3.2",
+ guava: "com.google.guava:guava:13.0.1",
+
+ // Test:
+ dropwizard_testing: "io.dropwizard:dropwizard-testing:$dropwizard_version",
+ dropwizard_client: "io.dropwizard:dropwizard-client:$dropwizard_version",
+ junit: "junit:junit-dep:4.11",
+ mockito: "org.mockito:mockito-core:1.9.0",
+ hamcrest: "org.hamcrest:hamcrest-library:1.3",
+ jersey_grrizle: "org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:$jeresy_version"
]
\ No newline at end of file
diff --git a/src/main/java/org/apache/freemarker/onlinetester/dropwizard/FreeMarkerOnlineTester.java b/src/main/java/org/apache/freemarker/onlinetester/dropwizard/FreeMarkerOnlineTester.java
index 3c48efd..529a2da 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/dropwizard/FreeMarkerOnlineTester.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/dropwizard/FreeMarkerOnlineTester.java
@@ -1,19 +1,16 @@
package org.apache.freemarker.onlinetester.dropwizard;
-import com.codahale.metrics.health.HealthCheck;
+import org.apache.freemarker.onlinetester.healthchecks.FreeMarkerOnlineTesterOverallHealthCheck;
+import org.apache.freemarker.onlinetester.resources.ExecuteApiResource;
+import org.apache.freemarker.onlinetester.resources.WebPageResource;
+import org.apache.freemarker.onlinetester.services.FreeMarkerService;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
-import org.apache.freemarker.onlinetester.spring.SpringConfiguration;
-import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
-
-
-import javax.ws.rs.Path;
-import java.util.Map;
public class FreeMarkerOnlineTester extends Application<FreeMarkerOnlineTesterConfiguration> {
@@ -28,31 +25,15 @@
@Override
public void run(FreeMarkerOnlineTesterConfiguration configuration, Environment environment) throws Exception {
- AnnotationConfigWebApplicationContext context = initializeSpringContext();
-
-
- Map<String, Object> resources = context.getBeansWithAnnotation(Path.class);
- for(Map.Entry<String,Object> entry : resources.entrySet()) {
- environment.jersey().register(entry.getValue());
- }
-
- Map<String, HealthCheck> healthChecks = context.getBeansOfType(HealthCheck.class);
- for(Map.Entry<String,HealthCheck> entry : healthChecks.entrySet()) {
- environment.healthChecks().register("", entry.getValue());
- }
- }
-
- private AnnotationConfigWebApplicationContext initializeSpringContext() {
- AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
- springContext.register(SpringConfiguration.class);
- springContext.refresh();
- springContext.start();
- return springContext;
+ FreeMarkerService service = new FreeMarkerService.Builder().build();
+ environment.jersey().register(new ExecuteApiResource(service));
+ environment.jersey().register(new WebPageResource());
+ environment.healthChecks().register("overall", new FreeMarkerOnlineTesterOverallHealthCheck());
}
@Override
public void initialize(Bootstrap<FreeMarkerOnlineTesterConfiguration> bootstrap) {
- bootstrap.addBundle(new ViewBundle());
+ bootstrap.addBundle(new ViewBundle<>());
bootstrap.addBundle(new AssetsBundle());
}
}
diff --git a/src/main/java/org/apache/freemarker/onlinetester/healthchecks/MyProjectHealthCheck.java b/src/main/java/org/apache/freemarker/onlinetester/healthchecks/FreeMarkerOnlineTesterOverallHealthCheck.java
similarity index 89%
rename from src/main/java/org/apache/freemarker/onlinetester/healthchecks/MyProjectHealthCheck.java
rename to src/main/java/org/apache/freemarker/onlinetester/healthchecks/FreeMarkerOnlineTesterOverallHealthCheck.java
index 6322100..cf0ab8e 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/healthchecks/MyProjectHealthCheck.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/healthchecks/FreeMarkerOnlineTesterOverallHealthCheck.java
@@ -20,10 +20,8 @@
package org.apache.freemarker.onlinetester.healthchecks;
import com.codahale.metrics.health.HealthCheck;
-import org.springframework.stereotype.Component;
-@Component
-public class MyProjectHealthCheck extends HealthCheck {
+public class FreeMarkerOnlineTesterOverallHealthCheck extends HealthCheck {
@Override
protected Result check() throws Exception {
diff --git a/src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResource.java b/src/main/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResource.java
similarity index 97%
rename from src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResource.java
rename to src/main/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResource.java
index c850720..3303e13 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResource.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResource.java
@@ -36,13 +36,10 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.freemarker.onlinetester.model.ErrorCode;
-import org.apache.freemarker.onlinetester.model.ExecuteResourceProblem;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
import org.apache.freemarker.onlinetester.model.ErrorResponse;
import org.apache.freemarker.onlinetester.model.ExecuteRequest;
import org.apache.freemarker.onlinetester.model.ExecuteResourceField;
+import org.apache.freemarker.onlinetester.model.ExecuteResourceProblem;
import org.apache.freemarker.onlinetester.model.ExecuteResponse;
import org.apache.freemarker.onlinetester.services.AllowedSettingValuesMaps;
import org.apache.freemarker.onlinetester.services.FreeMarkerService;
@@ -54,8 +51,7 @@
import freemarker.core.OutputFormat;
@Path("/api/execute")
-@Component
-public class FreeMarkerOnlineExecuteResource {
+public class ExecuteApiResource {
private static final int MAX_TEMPLATE_INPUT_LENGTH = 10000;
private static final int MAX_DATA_MODEL_INPUT_LENGTH = 10000;
@@ -77,8 +73,11 @@
static final String DATA_MODEL_ERROR_MESSAGE_FOOTER = "Note: This is NOT a FreeMarker error message. "
+ "The data model syntax is specific to this online service.";
- @Autowired
- private FreeMarkerService freeMarkerService;
+ private final FreeMarkerService freeMarkerService;
+
+ public ExecuteApiResource(FreeMarkerService freeMarkerService) {
+ this.freeMarkerService = freeMarkerService;
+ }
@POST
@Produces(MediaType.APPLICATION_JSON)
@@ -141,7 +140,7 @@
private Map<String, Object> getDataModel(ExecuteRequest req, List<ExecuteResourceProblem> problems) {
String dataModel = req.getDataModel();
-
+
if (dataModel.length() > MAX_DATA_MODEL_INPUT_LENGTH) {
String error = formatMessage(
MAX_DATA_MODEL_INPUT_LENGTH_EXCEEDED_ERROR_MESSAGE, MAX_DATA_MODEL_INPUT_LENGTH);
diff --git a/src/main/java/org/apache/freemarker/onlinetester/resources/TestResource.java b/src/main/java/org/apache/freemarker/onlinetester/resources/TestResource.java
deleted file mode 100644
index df790bf..0000000
--- a/src/main/java/org/apache/freemarker/onlinetester/resources/TestResource.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package org.apache.freemarker.onlinetester.resources;
-
-import org.springframework.stereotype.Component;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-
-@Path("/test")
-@Component
-public class TestResource {
- @GET
- @Produces(MediaType.TEXT_HTML)
- public String blankForm() {
- return "Hello";
- }
-}
diff --git a/src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResource.java b/src/main/java/org/apache/freemarker/onlinetester/resources/WebPageResource.java
similarity index 94%
rename from src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResource.java
rename to src/main/java/org/apache/freemarker/onlinetester/resources/WebPageResource.java
index ebf82a1..1f7a802 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResource.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/resources/WebPageResource.java
@@ -27,13 +27,13 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
-import org.springframework.stereotype.Component;
-
import org.apache.freemarker.onlinetester.view.FreeMarkerOnlineView;
+/**
+ * The HTML web page shown in the browser.
+ */
@Path("/")
-@Component
-public class FreeMarkerOnlineResource {
+public class WebPageResource {
@GET
@Produces(MediaType.TEXT_HTML)
diff --git a/src/main/java/org/apache/freemarker/onlinetester/services/FreeMarkerService.java b/src/main/java/org/apache/freemarker/onlinetester/services/FreeMarkerService.java
index 4487abe..c1efce4 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/services/FreeMarkerService.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/services/FreeMarkerService.java
@@ -34,16 +34,12 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import javax.annotation.PostConstruct;
-
import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.freemarker.onlinetester.util.LengthLimitExceededException;
import org.apache.freemarker.onlinetester.util.LengthLimitedWriter;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Service;
-
-import org.apache.freemarker.onlinetester.util.LengthLimitExceededException;
import freemarker.core.FreeMarkerInternalsAccessor;
import freemarker.core.OutputFormat;
@@ -55,13 +51,11 @@
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
-@Service
public class FreeMarkerService {
private static final int DEFAULT_MAX_OUTPUT_LENGTH = 100000;
private static final int DEFAULT_MAX_THREADS = Math.max(2,
(int) Math.round(Runtime.getRuntime().availableProcessors() * 3.0 / 4));
- /** Not implemented yet, will need 2.3.22, even then a _CoreAPI call. */
private static final long DEFAULT_MAX_TEMPLATE_EXECUTION_TIME = 2000;
private static final int MIN_DEFAULT_MAX_QUEUE_LENGTH = 2;
private static final int MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS = 30000;
@@ -75,17 +69,32 @@
private static final Logger logger = LoggerFactory.getLogger(FreeMarkerService.class);
- private final Configuration freeMarkerConfig;
-
- private ExecutorService templateExecutor;
-
- private int maxOutputLength = DEFAULT_MAX_OUTPUT_LENGTH;
-
- private int maxThreads = DEFAULT_MAX_THREADS;
- private Integer maxQueueLength;
- private long maxTemplateExecutionTime = DEFAULT_MAX_TEMPLATE_EXECUTION_TIME;
+ private final int maxOutputLength;
+ private final int maxThreads;
+ private final Integer maxQueueLength;
+ private final long maxTemplateExecutionTime;
- public FreeMarkerService() {
+ private final Configuration freeMarkerConfig;
+ private final ExecutorService templateExecutor;
+
+ private FreeMarkerService(Builder bulder) {
+ maxOutputLength = bulder.getMaxOutputLength();
+ maxThreads = bulder.getMaxThreads();
+ maxQueueLength = bulder.getMaxQueueLength();
+ maxTemplateExecutionTime = bulder.getMaxTemplateExecutionTime();
+
+ int actualMaxQueueLength = maxQueueLength != null
+ ? maxQueueLength
+ : Math.max(
+ MIN_DEFAULT_MAX_QUEUE_LENGTH,
+ (int) (MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS / maxTemplateExecutionTime));
+ ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
+ maxThreads, maxThreads,
+ THREAD_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
+ new BlockingArrayQueue<Runnable>(actualMaxQueueLength));
+ threadPoolExecutor.allowCoreThreadTimeOut(true);
+ templateExecutor = threadPoolExecutor;
+
freeMarkerConfig = new Configuration(Configuration.getVersion());
freeMarkerConfig.setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
freeMarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
@@ -192,33 +201,17 @@
return maxOutputLength;
}
- public void setMaxOutputLength(int maxOutputLength) {
- this.maxOutputLength = maxOutputLength;
- }
-
public int getMaxThreads() {
return maxThreads;
}
- public void setMaxThreads(int maxThreads) {
- this.maxThreads = maxThreads;
- }
-
public int getMaxQueueLength() {
return maxQueueLength;
}
- public void setMaxQueueLength(int maxQueueLength) {
- this.maxQueueLength = maxQueueLength;
- }
-
public long getMaxTemplateExecutionTime() {
return maxTemplateExecutionTime;
}
-
- public void setMaxTemplateExecutionTime(long maxTemplateExecutionTime) {
- this.maxTemplateExecutionTime = maxTemplateExecutionTime;
- }
/**
* Returns the time zone used by the FreeMarker templates.
@@ -232,21 +225,6 @@
return new FreeMarkerServiceResponse.Builder().buildForFailure(e);
}
- @PostConstruct
- public void postConstruct() {
- int actualMaxQueueLength = maxQueueLength != null
- ? maxQueueLength
- : Math.max(
- MIN_DEFAULT_MAX_QUEUE_LENGTH,
- (int) (MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS / maxTemplateExecutionTime));
- ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
- maxThreads, maxThreads,
- THREAD_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
- new BlockingArrayQueue<Runnable>(actualMaxQueueLength));
- threadPoolExecutor.allowCoreThreadTimeOut(true);
- templateExecutor = threadPoolExecutor;
- }
-
private class CalculateTemplateOutput implements Callable<FreeMarkerServiceResponse> {
private boolean templateExecutionStarted;
@@ -361,4 +339,47 @@
}
+ public static class Builder {
+ private int maxOutputLength = DEFAULT_MAX_OUTPUT_LENGTH;
+ private int maxThreads = DEFAULT_MAX_THREADS;
+ private Integer maxQueueLength;
+ private long maxTemplateExecutionTime = DEFAULT_MAX_TEMPLATE_EXECUTION_TIME;
+
+ public int getMaxOutputLength() {
+ return maxOutputLength;
+ }
+
+ public void setMaxOutputLength(int maxOutputLength) {
+ this.maxOutputLength = maxOutputLength;
+ }
+
+ public int getMaxThreads() {
+ return maxThreads;
+ }
+
+ public void setMaxThreads(int maxThreads) {
+ this.maxThreads = maxThreads;
+ }
+
+ public Integer getMaxQueueLength() {
+ return maxQueueLength;
+ }
+
+ public void setMaxQueueLength(Integer maxQueueLength) {
+ this.maxQueueLength = maxQueueLength;
+ }
+
+ public long getMaxTemplateExecutionTime() {
+ return maxTemplateExecutionTime;
+ }
+
+ public void setMaxTemplateExecutionTime(long maxTemplateExecutionTime) {
+ this.maxTemplateExecutionTime = maxTemplateExecutionTime;
+ }
+
+ public FreeMarkerService build() {
+ return new FreeMarkerService(this);
+ }
+ }
+
}
diff --git a/src/main/java/org/apache/freemarker/onlinetester/spring/SpringConfiguration.java b/src/main/java/org/apache/freemarker/onlinetester/spring/SpringConfiguration.java
deleted file mode 100644
index a4fe003..0000000
--- a/src/main/java/org/apache/freemarker/onlinetester/spring/SpringConfiguration.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.apache.freemarker.onlinetester.spring;
-
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-@ComponentScan(basePackages = {"org.apache.freemarker.onlinetester"})
-public class SpringConfiguration {}
diff --git a/src/main/java/org/apache/freemarker/onlinetester/util/DataModelParser.java b/src/main/java/org/apache/freemarker/onlinetester/util/DataModelParser.java
index b356be6..4f203db 100644
--- a/src/main/java/org/apache/freemarker/onlinetester/util/DataModelParser.java
+++ b/src/main/java/org/apache/freemarker/onlinetester/util/DataModelParser.java
@@ -34,7 +34,7 @@
import javax.xml.parsers.DocumentBuilder;
-import org.springframework.util.StringUtils;
+import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
@@ -85,7 +85,7 @@
}
public static Map<String, Object> parse(String src, TimeZone timeZone) throws DataModelParsingException {
- if (!StringUtils.hasText(src)) {
+ if (StringUtils.isBlank(src)) {
return Collections.emptyMap();
}
diff --git a/src/main/resources/freemarker-online.yml b/src/main/resources/freemarker-online.yml
index f7606d0..eac938b 100644
--- a/src/main/resources/freemarker-online.yml
+++ b/src/main/resources/freemarker-online.yml
@@ -33,6 +33,4 @@
# logFormat:
server:
requestLog:
- appenders:
- - type: console
- threshold: OFF
\ No newline at end of file
+ appenders: []
diff --git a/src/main/resources/spring/bootstrap-context.xml b/src/main/resources/spring/bootstrap-context.xml
deleted file mode 100644
index 8d8e518..0000000
--- a/src/main/resources/spring/bootstrap-context.xml
+++ /dev/null
@@ -1,38 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
--->
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
-
- <context:component-scan base-package="org.apache.freemarker.onlinetester"/>
-
- <context:annotation-config/>
-
- <!-- Use the system properties (initalized by DW) to configure spring context files -->
- <bean id="propertyPlaceholderConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="ignoreUnresolvablePlaceholders" value="true"/>
- <property name="ignoreResourceNotFound" value="true"/>
- </bean>
-
-</beans>
\ No newline at end of file
diff --git a/src/test/java/org/apache/freemarker/onlinetester/platform/DropWizardServiceTest.java b/src/test/java/org/apache/freemarker/onlinetester/ApplicationStartsTest.java
similarity index 71%
rename from src/test/java/org/apache/freemarker/onlinetester/platform/DropWizardServiceTest.java
rename to src/test/java/org/apache/freemarker/onlinetester/ApplicationStartsTest.java
index c467a04..a422016 100644
--- a/src/test/java/org/apache/freemarker/onlinetester/platform/DropWizardServiceTest.java
+++ b/src/test/java/org/apache/freemarker/onlinetester/ApplicationStartsTest.java
@@ -17,25 +17,32 @@
* under the License.
*/
-package org.apache.freemarker.onlinetester.platform;
+package org.apache.freemarker.onlinetester;
-import com.google.common.io.Resources;
-import io.dropwizard.testing.junit.DropwizardAppRule;
+import static org.junit.Assert.assertEquals;
import org.apache.freemarker.onlinetester.dropwizard.FreeMarkerOnlineTester;
import org.apache.freemarker.onlinetester.dropwizard.FreeMarkerOnlineTesterConfiguration;
import org.junit.ClassRule;
import org.junit.Test;
-import org.junit.rules.TestRule;
-public class DropWizardServiceTest {
+import com.google.common.io.Resources;
+
+import io.dropwizard.testing.junit.DropwizardAppRule;
+
+public class ApplicationStartsTest {
+
@ClassRule
- public static TestRule testRule = new DropwizardAppRule<FreeMarkerOnlineTesterConfiguration>(FreeMarkerOnlineTester.class,
+ public final static DropwizardAppRule RULE = new DropwizardAppRule<FreeMarkerOnlineTesterConfiguration>
+ (FreeMarkerOnlineTester.class,
Resources.getResource("freemarker-online.yml").getPath());
@Test
- public void testServerIsUp() throws Exception {
- ((DropwizardAppRule) testRule).getApplication();
+ public void test() throws Exception {
+ assertEquals(
+ 200,
+ RULE.client().target("http://localhost:" + RULE.getLocalPort() + "/").request().get().getStatus());
}
+
}
diff --git a/src/test/java/org/apache/freemarker/onlinetester/platform/YamlPropertiesPersister.java b/src/test/java/org/apache/freemarker/onlinetester/platform/YamlPropertiesPersister.java
deleted file mode 100644
index 4257e40..0000000
--- a/src/test/java/org/apache/freemarker/onlinetester/platform/YamlPropertiesPersister.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.freemarker.onlinetester.platform;
-
-import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
-import org.springframework.util.PropertiesPersister;
-import org.springframework.util.StringUtils;
-
-import java.io.*;
-import java.util.Map;
-import java.util.Properties;
-
-public class YamlPropertiesPersister implements PropertiesPersister {
- @Override
- public void load(Properties props, InputStream is) throws IOException {
- load(props, new InputStreamReader(is));
- }
-
- /**
- * We want to traverse map representing Yaml object and each time we find String=String pair we want to
- * save it as Property. As we are going deeper into map we generate compound key as path-like String
- *
- * @see org.springframework.util.PropertiesPersister#load(java.util.Properties, java.io.Reader)
- */
- @Override
- public void load(Properties props, Reader reader) throws IOException {
- Yaml yaml = new Yaml();
- @SuppressWarnings("unchecked")
- Map<String, Object> map = (Map<String, Object>) yaml.load(reader);
- // now we can populate supplied props
- assignProperties(props, map, null);
- }
-
- @SuppressWarnings("unchecked")
- public void assignProperties(Properties props, Map<String, Object> map, String path) {
- for (Map.Entry<String, Object> entry : map.entrySet()) {
- String key = entry.getKey();
- if (!StringUtils.isEmpty(path))
- key = path + "." + key;
- Object val = entry.getValue();
- if (val instanceof String) {
- // see if we need to create a compound key
- props.put(key, val);
- } else if (val instanceof Map) {
- assignProperties(props, (Map<String, Object>) val, key);
- }
- }
- }
-
- @Override
- public void store(Properties props, OutputStream os, String header) throws IOException {
- throw new IllegalStateException("Current implementation is a read-only");
- }
-
- @Override
- public void store(Properties props, Writer writer, String header) throws IOException {
- throw new IllegalStateException("Current implementation is a read-only");
- }
-
- @Override
- public void loadFromXml(Properties props, InputStream is) throws IOException {
- throw new IllegalStateException("Use DefaultPropertiesPersister if you want to read/write XML");
- }
-
- @Override
- public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
- throw new IllegalStateException("Use DefaultPropertiesPersister if you want to load/store to XML");
- }
-
- @Override
- public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
- throw new IllegalStateException("Use DefaultPropertiesPersister if you want to read/write XML");
- }
-}
diff --git a/src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResourceTest.java b/src/test/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResourceTest.java
similarity index 61%
rename from src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResourceTest.java
rename to src/test/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResourceTest.java
index d0f81cb..7fa3fb4 100644
--- a/src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineExecuteResourceTest.java
+++ b/src/test/java/org/apache/freemarker/onlinetester/resources/ExecuteApiResourceTest.java
@@ -19,61 +19,39 @@
package org.apache.freemarker.onlinetester.resources;
-import static org.hamcrest.Matchers.containsString;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Response;
import org.apache.freemarker.onlinetester.model.ExecuteRequest;
import org.apache.freemarker.onlinetester.model.ExecuteResourceField;
import org.apache.freemarker.onlinetester.model.ExecuteResourceProblem;
import org.apache.freemarker.onlinetester.model.ExecuteResponse;
import org.junit.Test;
-import org.springframework.web.context.ContextLoaderListener;
-import org.springframework.web.context.request.RequestContextListener;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
-import com.sun.jersey.test.framework.AppDescriptor;
-import com.sun.jersey.test.framework.JerseyTest;
-import com.sun.jersey.test.framework.WebAppDescriptor;
-
-public class FreeMarkerOnlineExecuteResourceTest extends JerseyTest {
+public class ExecuteApiResourceTest extends ResourceTest {
private static final String DATA_MODEL = "user=John";
private static final String TEMPLATE_WITH_VARIABLE = "Welcome ${user}";
private static final String TEMPLATE_PLAIN = "Welcome John";
private static final String MALFORMED_DATA_MODEL = "userJohn";
- private static final String EXECUTE_API = "api/execute";
- @Override
- protected AppDescriptor configure() {
- return new WebAppDescriptor.Builder("org.apache.freemarker.onlinetester.resources")
- .contextPath("/")
- .contextListenerClass(ContextLoaderListener.class)
- .contextParam("contextConfigLocation", "classpath:spring/bootstrap-context.xml")
- .servletClass(SpringServlet.class)
- .requestListenerClass(RequestContextListener.class)
- .build();
- }
@Test
public void testSuccessRequest() throws Exception {
ExecuteRequest req = new ExecuteRequest(TEMPLATE_WITH_VARIABLE, DATA_MODEL);
- ClientResponse resp = client().resource(getBaseURI().toString() + EXECUTE_API)
- .header("Content-Type", "application/json").entity(req).post(ClientResponse.class);
+ Response resp = postJSON(req);
assertEquals(200, resp.getStatus());
- ExecuteResponse response = resp.getEntity(ExecuteResponse.class);
+ ExecuteResponse response = resp.readEntity(ExecuteResponse.class);
assertNull(response.getProblems());
}
@Test
public void testMalformedDataModel() throws Exception {
ExecuteRequest req = new ExecuteRequest(TEMPLATE_PLAIN, MALFORMED_DATA_MODEL);
- ClientResponse resp = client().resource(getBaseURI().toString() + EXECUTE_API)
- .header("Content-Type", "application/json").entity(req).post(ClientResponse.class);
+ Response resp = postJSON(req);
assertEquals(200, resp.getStatus());
- ExecuteResponse response = resp.getEntity(ExecuteResponse.class);
+ ExecuteResponse response = resp.readEntity(ExecuteResponse.class);
assertNotNull(response.getProblems());
assertTrue(containsProblem(response, ExecuteResourceField.DATA_MODEL));
}
@@ -81,10 +59,9 @@
@Test
public void testLongDataModel() throws Exception {
ExecuteRequest req = new ExecuteRequest(TEMPLATE_PLAIN, create30KString());
- ClientResponse resp = client().resource(getBaseURI().toString() + EXECUTE_API)
- .header("Content-Type", "application/json").entity(req).post(ClientResponse.class);
+ Response resp = postJSON(req);
assertEquals(200, resp.getStatus());
- ExecuteResponse response = resp.getEntity(ExecuteResponse.class);
+ ExecuteResponse response = resp.readEntity(ExecuteResponse.class);
assertNotNull(response.getProblems());
assertTrue(containsProblem(response, ExecuteResourceField.DATA_MODEL));
String problemMessage = getProblemMessage(response, ExecuteResourceField.DATA_MODEL);
@@ -95,10 +72,9 @@
@Test
public void testLongTemplate() throws Exception {
ExecuteRequest req = new ExecuteRequest(create30KString(), DATA_MODEL);
- ClientResponse resp = client().resource(getBaseURI().toString() + EXECUTE_API)
- .header("Content-Type", "application/json").entity(req).post(ClientResponse.class);
+ Response resp = postJSON(req);
assertEquals(200, resp.getStatus());
- ExecuteResponse response = resp.getEntity(ExecuteResponse.class);
+ ExecuteResponse response = resp.readEntity(ExecuteResponse.class);
assertNotNull(response.getProblems());
assertTrue(containsProblem(response, ExecuteResourceField.TEMPLATE));
String problemMessage = getProblemMessage(response, ExecuteResourceField.TEMPLATE);
@@ -112,12 +88,11 @@
req.setOutputFormat("wrongOutputFormat");
req.setLocale("wrongLocale");
req.setTimeZone("wrongTimeZone");
-
- ClientResponse resp = client().resource(getBaseURI() + EXECUTE_API)
- .header("Content-Type", "application/json").entity(req).post(ClientResponse.class);
+
+ Response resp = postJSON(req);
assertEquals(200, resp.getStatus());
- ExecuteResponse response = resp.getEntity(ExecuteResponse.class);
+ ExecuteResponse response = resp.readEntity(ExecuteResponse.class);
assertNotNull(response.getProblems());
assertThat(getProblemMessage(response, ExecuteResourceField.TEMPLATE), containsString("limit"));
assertThat(getProblemMessage(response, ExecuteResourceField.DATA_MODEL), containsString("limit"));
@@ -127,15 +102,11 @@
}
private String create30KString() {
- final String veryLongString;
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 30000 / 10; i++) {
- sb.append("0123456789");
- }
- veryLongString = sb.toString();
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 30000 / 10; i++) {
+ sb.append("0123456789");
}
- return veryLongString;
+ return sb.toString();
}
private boolean containsProblem(ExecuteResponse response, ExecuteResourceField field) {
@@ -155,5 +126,9 @@
}
return null;
}
-
+
+ protected Response postJSON(ExecuteRequest req) {
+ return RULE.target("/api/execute").request().post(Entity.json(req));
+ }
+
}
diff --git a/src/test/java/org/apache/freemarker/onlinetester/resources/ResourceTest.java b/src/test/java/org/apache/freemarker/onlinetester/resources/ResourceTest.java
new file mode 100644
index 0000000..4872cbf
--- /dev/null
+++ b/src/test/java/org/apache/freemarker/onlinetester/resources/ResourceTest.java
@@ -0,0 +1,17 @@
+package org.apache.freemarker.onlinetester.resources;
+
+import org.apache.freemarker.onlinetester.services.FreeMarkerService;
+import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
+import org.junit.ClassRule;
+
+import io.dropwizard.testing.junit.ResourceTestRule;
+
+public abstract class ResourceTest {
+
+ @ClassRule
+ public static final ResourceTestRule RULE = ResourceTestRule.builder()
+ .setTestContainerFactory(new GrizzlyWebTestContainerFactory())
+ .addResource(new ExecuteApiResource(new FreeMarkerService.Builder().build()))
+ .build();
+
+}
diff --git a/src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResourceTest.java b/src/test/java/org/apache/freemarker/onlinetester/resources/WebPageResourceTest.java
similarity index 88%
rename from src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResourceTest.java
rename to src/test/java/org/apache/freemarker/onlinetester/resources/WebPageResourceTest.java
index 4f6ef7c..739628f 100644
--- a/src/test/java/org/apache/freemarker/onlinetester/resources/FreeMarkerOnlineResourceTest.java
+++ b/src/test/java/org/apache/freemarker/onlinetester/resources/WebPageResourceTest.java
@@ -40,10 +40,10 @@
import freemarker.core.OutputFormat;
@RunWith(MockitoJUnitRunner.class)
-public class FreeMarkerOnlineResourceTest {
+public class WebPageResourceTest {
@InjectMocks
- FreeMarkerOnlineResource freeMarkerOnlineResultResource;
+ WebPageResource webPageResource;
@Mock
FreeMarkerService freeMarkerService;
@@ -53,7 +53,7 @@
when(freeMarkerService.calculateTemplateOutput(
anyString(), anyMap(), any(OutputFormat.class), any(Locale.class), any(TimeZone.class)))
.thenThrow(new AssertionError());
- FreeMarkerOnlineView view = freeMarkerOnlineResultResource.blankForm();
+ FreeMarkerOnlineView view = webPageResource.blankForm();
assertEquals(view.getTemplate(), "");
assertEquals(view.getDataModel(), "");
}
@@ -63,8 +63,9 @@
when(freeMarkerService.calculateTemplateOutput(
anyString(), anyMap(), any(OutputFormat.class), any(Locale.class), any(TimeZone.class)))
.thenThrow(new AssertionError());
- FreeMarkerOnlineView view = freeMarkerOnlineResultResource.formResult(null, null, null, null, null);
+ FreeMarkerOnlineView view = webPageResource.formResult(null, null, null, null, null);
assertEquals(view.getTemplate(), "");
assertEquals(view.getDataModel(), "");
}
+
}
diff --git a/src/test/java/org/apache/freemarker/onlinetester/services/FreeMarkerServiceTest.java b/src/test/java/org/apache/freemarker/onlinetester/services/FreeMarkerServiceTest.java
index fcaf358..37c52c9 100644
--- a/src/test/java/org/apache/freemarker/onlinetester/services/FreeMarkerServiceTest.java
+++ b/src/test/java/org/apache/freemarker/onlinetester/services/FreeMarkerServiceTest.java
@@ -19,14 +19,8 @@
package org.apache.freemarker.onlinetester.services;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.hamcrest.Matchers.instanceOf;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.not;
-import static org.hamcrest.Matchers.startsWith;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Collections;
@@ -44,9 +38,6 @@
import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,7 +49,6 @@
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
-@RunWith(MockitoJUnitRunner.class)
public class FreeMarkerServiceTest {
private static final Logger LOG = LoggerFactory.getLogger(FreeMarkerServiceTest.class);
@@ -70,21 +60,24 @@
private static final int BLOCKING_TEST_TIMEOUT = 5000;
private static final String TRUNCATION_TEST_TEMPLATE = "12345";
-
- @InjectMocks
- private FreeMarkerService freeMarkerService;
-
+
+ private FreeMarkerService.Builder serviceBuilder;
+
@Before
- public void initializeSpringBeans() {
- freeMarkerService.setMaxQueueLength(MAX_QUEUE_LENGTH);
- freeMarkerService.setMaxThreads(MAX_THREADS);
- freeMarkerService.postConstruct();
- freeMarkerService.setMaxTemplateExecutionTime(MAX_TEMPLATE_EXECUTION_TIME);
+ public void initializeService() {
+ serviceBuilder = new FreeMarkerService.Builder();
+ serviceBuilder.setMaxQueueLength(MAX_QUEUE_LENGTH);
+ serviceBuilder.setMaxThreads(MAX_THREADS);
+ serviceBuilder.setMaxTemplateExecutionTime(MAX_TEMPLATE_EXECUTION_TIME);
+ }
+
+ private FreeMarkerService getService() {
+ return serviceBuilder.build();
}
@Test
public void testCalculationOfATemplateWithNoDataModel() {
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
"test", Collections.<String, Object>emptyMap(), null, null, null);
assertThat(serviceResponse.isSuccesful(), is(true));
assertThat(serviceResponse.getTemplateOutput(), is("test"));
@@ -95,7 +88,7 @@
HashMap<String, Object> dataModel = new HashMap<>();
dataModel.put("var1", "val1");
String templateSourceCode = "${var1}";
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
templateSourceCode, dataModel, null, null, null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("val1"));
}
@@ -106,7 +99,7 @@
dataModel.put("var1", "val1");
dataModel.put("var2", "val2");
String template = "${var1?capitalize} ${var2?cap_first}";
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, dataModel, null, null, null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("Val1 Val2"));
}
@@ -115,12 +108,12 @@
public void testOutputFormatParamterMatters() {
String template = "${'&'}";
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, null, null, null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("&"));
}
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, HTMLOutputFormat.INSTANCE, null, null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("&"));
}
@@ -130,12 +123,12 @@
public void testLocaleParameterMatters() {
String template = "${.locale}";
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, null, new Locale("en", "US"), null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("en_US"));
}
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, null, new Locale("ru", "RU"), null);
assertThat(serviceResponse.getTemplateOutput(), equalTo("ru_RU"));
}
@@ -147,14 +140,14 @@
String gmt1Result;
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, null, null, TimeZone.getTimeZone("GMT+01"));
gmt1Result = serviceResponse.getTemplateOutput();
}
String gmt2Result;
{
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
template, null, null, new Locale("ru", "RU"), null);
gmt2Result = serviceResponse.getTemplateOutput();
}
@@ -164,7 +157,7 @@
@Test
public void testTemplateWithSyntaxError() {
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
"test ${xx", Collections.<String, Object>emptyMap(), null, null, null);
assertThat(serviceResponse.isSuccesful(), is(false));
assertThat(serviceResponse.getFailureReason(), instanceOf(ParseException.class));
@@ -172,7 +165,7 @@
@Test
public void testTemplateWithEvaluationError() {
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
"test ${x}", Collections.<String, Object>emptyMap(), null, null, null);
assertThat(serviceResponse.isSuccesful(), is(false));
assertThat(serviceResponse.getFailureReason(), instanceOf(TemplateException.class));
@@ -180,8 +173,8 @@
@Test
public void testResultAlmostTruncation() {
- freeMarkerService.setMaxOutputLength(5);
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ serviceBuilder.setMaxOutputLength(5);
+ FreeMarkerServiceResponse serviceResponse = getService().calculateTemplateOutput(
TRUNCATION_TEST_TEMPLATE, Collections.<String, Object>emptyMap(), null, null, null);
assertThat(serviceResponse.isSuccesful(), is(true));
assertThat(serviceResponse.isTemplateOutputTruncated(), is(false));
@@ -190,20 +183,21 @@
@Test
public void testResultTruncation() {
- freeMarkerService.setMaxOutputLength(4);
- FreeMarkerServiceResponse serviceResponse = freeMarkerService.calculateTemplateOutput(
+ serviceBuilder.setMaxOutputLength(4);
+ FreeMarkerService service = getService();
+ FreeMarkerServiceResponse serviceResponse = service.calculateTemplateOutput(
TRUNCATION_TEST_TEMPLATE, Collections.<String, Object>emptyMap(), null, null, null);
assertThat(serviceResponse.isSuccesful(), is(true));
assertThat(serviceResponse.isTemplateOutputTruncated(), is(true));
assertThat(serviceResponse.getTemplateOutput(),
- startsWith(TRUNCATION_TEST_TEMPLATE.substring(0, freeMarkerService.getMaxOutputLength())));
- assertThat(serviceResponse.getTemplateOutput().charAt(freeMarkerService.getMaxOutputLength()),
- not(equalTo(TRUNCATION_TEST_TEMPLATE.charAt(freeMarkerService.getMaxOutputLength()))));
+ startsWith(TRUNCATION_TEST_TEMPLATE.substring(0, service.getMaxOutputLength())));
+ assertThat(serviceResponse.getTemplateOutput().charAt(service.getMaxOutputLength()),
+ not(equalTo(TRUNCATION_TEST_TEMPLATE.charAt(service.getMaxOutputLength()))));
}
@Test
public void testTemplateExecutionTimeout() throws InterruptedException, ExecutionException {
- freeMarkerService.setMaxTemplateExecutionTime(200);
+ serviceBuilder.setMaxTemplateExecutionTime(200);
// To avoid blocking the CI server forever without giving error:
Future<FreeMarkerServiceResponse> future = Executors.newSingleThreadExecutor().submit(
@@ -211,7 +205,7 @@
@Override
public FreeMarkerServiceResponse call() throws Exception {
- return freeMarkerService.calculateTemplateOutput(
+ return getService().calculateTemplateOutput(
"<#list 1.. as _></#list>", Collections.<String, Object>emptyMap(), null, null, null);
}
@@ -232,11 +226,12 @@
final Map<String, BlockerDirective> blockerDataModel = Collections.singletonMap("blocker", blocker);
try {
// Fill all available task "slots":
+ FreeMarkerService service = getService();
for (int i = 0; i < MAX_THREADS + MAX_QUEUE_LENGTH; i++) {
new Thread(new Runnable() {
@Override
public void run() {
- freeMarkerService.calculateTemplateOutput("<@blocker/>", blockerDataModel, null, null, null);
+ service.calculateTemplateOutput("<@blocker/>", blockerDataModel, null, null, null);
}
}).start();
}
@@ -256,9 +251,9 @@
// Because the others are waiting in the queue, and weren't started:
assertThat(blocker.getEntered(), not(greaterThan(MAX_THREADS)));
- // Souldn't accept on more tasks:
+ // Shouldn't accept on more tasks:
try {
- freeMarkerService.calculateTemplateOutput("<@blocker/>", blockerDataModel, null, null, null);
+ service.calculateTemplateOutput("<@blocker/>", blockerDataModel, null, null, null);
fail("Expected RejectedExecutionException, but nothing was thrown.");
} catch (RejectedExecutionException e) {
// Expected
diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..8c9b84d
--- /dev/null
+++ b/src/test/resources/logback-test.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<configuration>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} %-5level [%c] %C{20}:%L - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+ <!-- logger name="org.apache.freemarker" level="TRACE" / -->
+
+ <root level="debug">
+ <appender-ref ref="STDOUT" />
+ </root>
+
+</configuration>
\ No newline at end of file
diff --git a/src/test/resources/spring/test-context.xml b/src/test/resources/spring/test-context.xml
deleted file mode 100644
index 8f798a0..0000000
--- a/src/test/resources/spring/test-context.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
--->
-
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
-
-</beans>
\ No newline at end of file