Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Nov 20, 2017
1 parent 73b9aaa commit 40dacd3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.scripting.bsh;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -47,7 +48,7 @@
public class BshScriptFactoryTests {

@Test
public void staticScript() throws Exception {
public void staticScript() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());

assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
Expand Down Expand Up @@ -75,7 +76,7 @@ public void staticScript() throws Exception {
}

@Test
public void staticScriptWithNullReturnValue() throws Exception {
public void staticScriptWithNullReturnValue() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfig"));

Expand All @@ -86,7 +87,7 @@ public void staticScriptWithNullReturnValue() throws Exception {
}

@Test
public void staticScriptWithTwoInterfacesSpecified() throws Exception {
public void staticScriptWithTwoInterfacesSpecified() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfigExtra"));

Expand All @@ -100,7 +101,7 @@ public void staticScriptWithTwoInterfacesSpecified() throws Exception {
}

@Test
public void staticWithScriptReturningInstance() throws Exception {
public void staticWithScriptReturningInstance() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));

Expand All @@ -114,7 +115,7 @@ public void staticWithScriptReturningInstance() throws Exception {
}

@Test
public void staticScriptImplementingInterface() throws Exception {
public void staticScriptImplementingInterface() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerImpl"));

Expand All @@ -128,7 +129,7 @@ public void staticScriptImplementingInterface() throws Exception {
}

@Test
public void staticPrototypeScript() throws Exception {
public void staticPrototypeScript() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
Expand All @@ -147,7 +148,7 @@ public void staticPrototypeScript() throws Exception {
}

@Test
public void nonStaticScript() throws Exception {
public void nonStaticScript() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("messenger");

Expand All @@ -165,7 +166,7 @@ public void nonStaticScript() throws Exception {
}

@Test
public void nonStaticPrototypeScript() throws Exception {
public void nonStaticPrototypeScript() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass());
ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
Expand All @@ -189,7 +190,7 @@ public void nonStaticPrototypeScript() throws Exception {
}

@Test
public void scriptCompilationException() throws Exception {
public void scriptCompilationException() {
try {
new ClassPathXmlApplicationContext("org/springframework/scripting/bsh/bshBrokenContext.xml");
fail("Must throw exception for broken script file");
Expand All @@ -200,7 +201,7 @@ public void scriptCompilationException() throws Exception {
}

@Test
public void scriptThatCompilesButIsJustPlainBad() throws Exception {
public void scriptThatCompilesButIsJustPlainBad() throws IOException {
ScriptSource script = mock(ScriptSource.class);
final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
given(script.getScriptAsString()).willReturn(badScript);
Expand All @@ -217,7 +218,7 @@ public void scriptThatCompilesButIsJustPlainBad() throws Exception {
}

@Test
public void ctorWithNullScriptSourceLocator() throws Exception {
public void ctorWithNullScriptSourceLocator() {
try {
new BshScriptFactory(null, Messenger.class);
fail("Must have thrown exception by this point.");
Expand All @@ -227,27 +228,27 @@ public void ctorWithNullScriptSourceLocator() throws Exception {
}

@Test
public void ctorWithEmptyScriptSourceLocator() throws Exception {
public void ctorWithEmptyScriptSourceLocator() {
try {
new BshScriptFactory("", new Class<?>[] {Messenger.class});
new BshScriptFactory("", Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}

@Test
public void ctorWithWhitespacedScriptSourceLocator() throws Exception {
public void ctorWithWhitespacedScriptSourceLocator() {
try {
new BshScriptFactory("\n ", new Class<?>[] {Messenger.class});
new BshScriptFactory("\n ", Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}

@Test
public void resourceScriptFromTag() throws Exception {
public void resourceScriptFromTag() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
TestBean testBean = (TestBean) ctx.getBean("testBean");

Expand Down Expand Up @@ -286,7 +287,7 @@ public void resourceScriptFromTag() throws Exception {
}

@Test
public void prototypeScriptFromTag() throws Exception {
public void prototypeScriptFromTag() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
Expand All @@ -302,23 +303,23 @@ public void prototypeScriptFromTag() throws Exception {
}

@Test
public void inlineScriptFromTag() throws Exception {
public void inlineScriptFromTag() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
Calculator calculator = (Calculator) ctx.getBean("calculator");
assertNotNull(calculator);
assertFalse(calculator instanceof Refreshable);
}

@Test
public void refreshableFromTag() throws Exception {
public void refreshableFromTag() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
assertEquals("Hello World!", messenger.getMessage());
assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}

@Test
public void applicationEventListener() throws Exception {
public void applicationEventListener() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
Messenger eventListener = (Messenger) ctx.getBean("eventListener");
ctx.publishEvent(new MyEvent(ctx));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">

<lang:bsh id="messenger" script-source="classpath:org/springframework/scripting/bsh/Messenger.bsh"
Expand Down Expand Up @@ -52,12 +51,12 @@
<lang:property name="message" value="Hello World!"/>
</lang:bsh>

<lang:bsh id="eventListener" script-interfaces="org.springframework.context.ApplicationListener,org.springframework.scripting.Messenger" >
<lang:inline-script><![CDATA[
int count;
void onApplicationEvent (org.springframework.context.ApplicationEvent event) { count++; System.out.println(event); }
String getMessage() { return "count=" + count; }
]]></lang:inline-script>
</lang:bsh>
<lang:bsh id="eventListener" script-interfaces="org.springframework.context.ApplicationListener,org.springframework.scripting.Messenger" >
<lang:inline-script><![CDATA[
int count;
void onApplicationEvent (org.springframework.context.ApplicationEvent event) { count++; System.out.println(event); }
String getMessage() { return "count=" + count; }
]]></lang:inline-script>
</lang:bsh>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public FormHttpMessageConverter() {
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
this.supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA);

this.partConverters.add(new ByteArrayHttpMessageConverter());
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false);
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316

this.partConverters.add(new ByteArrayHttpMessageConverter());
this.partConverters.add(stringHttpMessageConverter);
this.partConverters.add(new ResourceHttpMessageConverter());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter {

private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
ClassUtils.isPresent("javax.xml.bind.Binder",
AllEncompassingFormHttpMessageConverter.class.getClassLoader());

private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,11 @@ protected void extendMessageConverters(List<HttpMessageConverter<?>> converters)
* @param messageConverters the list to add the default message converters to
*/
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setWriteAcceptCharset(false);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316

messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringConverter);
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new ResourceRegionHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<>());
Expand Down

0 comments on commit 40dacd3

Please sign in to comment.