diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyControlBusTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyControlBusTests.java index 531846b50b..b526e9df78 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyControlBusTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyControlBusTests.java @@ -27,6 +27,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationNotAllowedException; import org.springframework.beans.factory.BeanIsAbstractException; @@ -69,6 +70,7 @@ public class GroovyControlBusTests { @Test public void testOperationOfControlBus() { // long is > 3 + adviceCalled = 0; this.groovyCustomizer.executed = false; Message message = MessageBuilder.withPayload("def result = service.convert('aardvark'); def foo = headers.foo; result+foo").setHeader("foo", "bar").build(); this.input.send(message); @@ -170,7 +172,7 @@ public class GroovyControlBusTests { private static class MockRequestAttributes implements RequestAttributes { - private Map fakeRequest = new HashMap(); + private final Map fakeRequest = new HashMap(); public Object getAttribute(String name, int scope) { return fakeRequest.get(name); diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyRefreshTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyRefreshTests.java index 48e162977c..8eb6a3f77f 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyRefreshTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyRefreshTests.java @@ -62,7 +62,7 @@ public class GroovyRefreshTests { super.setValue(new CycleResource()); } } - + private static class CycleResource extends AbstractResource { private int count = -1; @@ -71,12 +71,12 @@ public class GroovyRefreshTests { public String getDescription() { return "CycleResource"; } - + @Override public String getFilename() throws IllegalStateException { return "CycleResource"; } - + @Override public long lastModified() throws IOException { return -1; @@ -88,6 +88,6 @@ public class GroovyRefreshTests { } return new ByteArrayInputStream(scripts[count].getBytes()); } - + } } diff --git a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java index 20dbe94cc0..c5a6ea3817 100644 --- a/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java +++ b/spring-integration-groovy/src/test/java/org/springframework/integration/groovy/config/GroovyServiceActivatorTests.java @@ -16,10 +16,12 @@ package org.springframework.integration.groovy.config; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import groovy.lang.GroovyObject; import groovy.lang.MissingPropertyException; @@ -56,13 +58,13 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class GroovyServiceActivatorTests { - + @Autowired private MessageChannel referencedScriptInput; @Autowired private MessageChannel inlineScriptInput; - + @Autowired private MessageChannel withScriptVariableGenerator; @@ -97,7 +99,7 @@ public class GroovyServiceActivatorTests { assertTrue(groovyCustomizer.executed); assertNull(replyChannel.receive(0)); } - + @Test public void withScriptVariableGenerator() throws Exception{ groovyCustomizer.executed = false; @@ -159,7 +161,7 @@ public class GroovyServiceActivatorTests { public void inlineScriptAndVariables() throws Exception{ new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-context.xml", this.getClass()); } - + @Test(expected=BeanDefinitionParsingException.class) public void variablesAndScriptVariableGenerator() throws Exception{ new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withgenerator-context.xml", this.getClass()); diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/RefreshableResourceScriptSource.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/RefreshableResourceScriptSource.java index 3f928ed35b..5348100d24 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/RefreshableResourceScriptSource.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/RefreshableResourceScriptSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import org.springframework.scripting.support.ResourceScriptSource; /** * @author Dave Syer * @author Oleg Zhurakousky + * @author Artem Bilan * @since 2.0 */ public class RefreshableResourceScriptSource implements ScriptSource { @@ -51,7 +52,10 @@ public class RefreshableResourceScriptSource implements ScriptSource { } public String getScriptAsString() throws IOException { - this.script = source.getScriptAsString(); + if (this.script == null || this.isModified()) { + this.lastModifiedChecked.set(System.currentTimeMillis()); + this.script = source.getScriptAsString(); + } return this.script; } @@ -60,15 +64,14 @@ public class RefreshableResourceScriptSource implements ScriptSource { } public boolean isModified() { - if (this.refreshDelay < 0) { - return false; - } - long time = System.currentTimeMillis(); - if (this.refreshDelay == 0 || (time - this.lastModifiedChecked.get()) > this.refreshDelay) { - this.lastModifiedChecked.set(time); - return this.source.isModified(); - } - return false; + return this.refreshDelay >= 0 && + (System.currentTimeMillis() - this.lastModifiedChecked.get()) > this.refreshDelay && + this.source.isModified(); + } + + @Override + public String toString() { + return this.source.toString(); } } diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/AbstractScriptExecutor.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/AbstractScriptExecutor.java index 061045348f..84293b719c 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/AbstractScriptExecutor.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/AbstractScriptExecutor.java @@ -1,11 +1,11 @@ /* * Copyright 2002-2011 the original author or authors. - * + * * Licensed 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. @@ -28,7 +28,7 @@ import org.springframework.util.Assert; /** * Base Class for {@link ScriptExecutor} - * + * * @author David Turanski * @author Mark Fisher * @since 2.1 @@ -44,9 +44,9 @@ abstract class AbstractScriptExecutor implements ScriptExecutor { public AbstractScriptExecutor(String language) { Assert.hasText(language, "language must not be empty"); this.language = language; - + scriptEngine = new ScriptEngineManager().getEngineByName(this.language); - + if (logger.isDebugEnabled()) { if (scriptEngine == null) { diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests-context.xml b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests-context.xml new file mode 100644 index 0000000000..b677caebc9 --- /dev/null +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests-context.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests.java new file mode 100644 index 0000000000..2bc3554f5a --- /dev/null +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Int3164Jsr223RefreshTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed 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.springframework.integration.scripting.config.jsr223; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.core.PollableChannel; +import org.springframework.integration.message.GenericMessage; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.FileCopyUtils; + +/** + * @author Artem Bilan + * @since 2.2.6 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class Int3164Jsr223RefreshTests { + + private static File workDir; + + private static File scriptFile; + + @Autowired + private MessageChannel referencedScriptInput; + + @Autowired + private PollableChannel outputChannel; + + @BeforeClass + public static void start() throws IOException { + String basePath = System.getProperty("java.io.tmpdir") + File.separator + "Int3164Jsr223RefreshTests"; + workDir = new File(basePath); + shutdown(); + workDir.mkdir(); + workDir.deleteOnExit(); + scriptFile = new File(workDir, "int3164.groovy"); + scriptFile.createNewFile(); + FileCopyUtils.copy("1".getBytes(), scriptFile); + } + + @AfterClass + public static void shutdown() { + if (workDir != null && workDir.exists()) { + for (File file : workDir.listFiles()) { + file.delete(); + } + workDir.delete(); + } + } + + @Test + public void testRefreshingScript() throws Exception { + this.referencedScriptInput.send(new GenericMessage("test")); + assertEquals(1, this.outputChannel.receive(100).getPayload()); + + FileCopyUtils.copy("2".getBytes(), scriptFile); + scriptFile.setLastModified(System.currentTimeMillis() + 10000); // force refresh + + this.referencedScriptInput.send(new GenericMessage("test")); + assertEquals(2, this.outputChannel.receive(100).getPayload()); + } + +}