Repository: incubator-freemarker-online-tester Updated Branches: refs/heads/master 90a6e05f5 -> 8b699eb51
http://git-wip-us.apache.org/repos/asf/incubator-freemarker-online-tester/blob/8b699eb5/src/test/java/org/apache/freemarker/onlinetester/services/FreeMarkerServiceTest.java ---------------------------------------------------------------------- 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 java.util.concurrent.TimeoutException; 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.TemplateDirectiveModel; 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 @@ public class FreeMarkerServiceTest { 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 @@ public class FreeMarkerServiceTest { 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 @@ public class FreeMarkerServiceTest { 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 class FreeMarkerServiceTest { 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 class FreeMarkerServiceTest { 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 @@ public class FreeMarkerServiceTest { 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 @@ public class FreeMarkerServiceTest { @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 @@ public class FreeMarkerServiceTest { @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 @@ public class FreeMarkerServiceTest { @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 @@ public class FreeMarkerServiceTest { @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 @@ public class FreeMarkerServiceTest { @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 @@ public class FreeMarkerServiceTest { 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 @@ public class FreeMarkerServiceTest { // 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 http://git-wip-us.apache.org/repos/asf/incubator-freemarker-online-tester/blob/8b699eb5/src/test/resources/logback-test.xml ---------------------------------------------------------------------- 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 http://git-wip-us.apache.org/repos/asf/incubator-freemarker-online-tester/blob/8b699eb5/src/test/resources/spring/test-context.xml ---------------------------------------------------------------------- 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