📄 jrubyscriptfactorytests.java
字号:
/*
* Copyright 2002-2006 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.scripting.jruby;
import junit.framework.TestCase;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.JdkVersion;
import org.springframework.core.NestedRuntimeException;
import org.springframework.scripting.Calculator;
import org.springframework.scripting.Messenger;
import org.springframework.scripting.ScriptCompilationException;
/**
* Unit tests for the JRubyScriptFactory class.
*
* @author Rob Harrop
* @author Rick Evans
*/
public class JRubyScriptFactoryTests extends TestCase {
private static final String RUBY_SCRIPT_SOURCE_LOCATOR =
"inline:require 'java'\n" +
"class RubyBar\n" +
"end\n" +
"RubyBar.new";
public void testStatic() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
ApplicationContext ctx =
new ClassPathXmlApplicationContext("org/springframework/scripting/jruby/jrubyContext.xml");
Calculator calc = (Calculator) ctx.getBean("calculator");
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
public void testNonStatic() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
ApplicationContext ctx =
new ClassPathXmlApplicationContext("org/springframework/scripting/jruby/jrubyRefreshableContext.xml");
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect.", desiredMessage, messenger.getMessage());
Refreshable refreshable = (Refreshable) messenger;
refreshable.refresh();
assertEquals("Message is incorrect after refresh.", desiredMessage, messenger.getMessage());
assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
public void testScriptCompilationException() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new ClassPathXmlApplicationContext("org/springframework/scripting/jruby/jrubyBrokenContext.xml");
fail("Should throw exception for broken script file");
}
catch (NestedRuntimeException e) {
assertTrue(e.contains(ScriptCompilationException.class));
}
}
public void testCtorWithNullScriptSourceLocator() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new JRubyScriptFactory(null, new Class[] {Messenger.class});
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testCtorWithEmptyScriptSourceLocator() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new JRubyScriptFactory("", new Class[] {Messenger.class});
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testCtorWithWhitespacedScriptSourceLocator() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new JRubyScriptFactory("\n ", new Class[] {Messenger.class});
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testCtorWithNullScriptInterfacesArray() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new JRubyScriptFactory(RUBY_SCRIPT_SOURCE_LOCATOR, null);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testCtorWithEmptyScriptInterfacesArray() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
try {
new JRubyScriptFactory(RUBY_SCRIPT_SOURCE_LOCATOR, new Class[] {});
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testResourceScriptFromTag() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertEquals("Hello World!", messenger.getMessage());
assertFalse(messenger instanceof Refreshable);
}
public void testInlineScriptFromTag() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Calculator calculator = (Calculator) ctx.getBean("calculator");
assertNotNull(calculator);
assertFalse(calculator instanceof Refreshable);
}
public void testRefreshableFromTag() throws Exception {
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
assertEquals("Hello World!", messenger.getMessage());
assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -