@Test public void testCtorWithWhitespacedScriptSourceLocator() throws Exception { try { new GroovyScriptFactory("\n "); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
@Test public void testCtorWithEmptyScriptSourceLocator() throws Exception { try { new GroovyScriptFactory(""); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
@Test public void testCtorWithNullScriptSourceLocator() throws Exception { try { new GroovyScriptFactory(null); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
@Test public void testGetScriptedObjectDoesChokeOnNullScriptSourceBeingPassedIn() throws Exception { GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); try { factory.getScriptedObject(null); fail("Must have thrown a NullPointerException as per contract ('null' ScriptSource supplied"); } catch (NullPointerException expected) { } }
@Test public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception { ScriptSource script = mock(ScriptSource.class); String badScript = "class Foo { protected Foo() {} \n String toString() { 'X' }}"; given(script.getScriptAsString()).willReturn(badScript); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript); assertEquals("X", factory.getScriptedObject(script).toString()); }
@Test public void testGetScriptedObjectDoesNotChokeOnNullInterfacesBeingPassedIn() throws Exception { ScriptSource script = mock(ScriptSource.class); given(script.getScriptAsString()).willReturn("class Bar {}"); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); Object scriptedObject = factory.getScriptedObject(script); assertNotNull(scriptedObject); }
@Test public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception { ScriptSource script = mock(ScriptSource.class); String badScript = "class Foo { public Foo(String foo) {}}"; given(script.getScriptAsString()).willReturn(badScript); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript); try { factory.getScriptedObject(script); fail("Must have thrown a ScriptCompilationException (no public no-arg ctor in scripted class)."); } catch (ScriptCompilationException expected) { assertTrue(expected.contains(NoSuchMethodException.class)); } }
@Override protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception { Assert.notNull(scriptSource, "scriptSource must not be null"); VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator(); if (this.customizer != null) { customizerDecorator.setCustomizer(this.customizer); } if (!CollectionUtils.isEmpty(variables)) { customizerDecorator.setVariables(variables); } GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); if (this.beanClassLoader != null) { factory.setBeanClassLoader(this.beanClassLoader); } if (this.beanFactory != null) { factory.setBeanFactory(this.beanFactory); } Object result = factory.getScriptedObject(scriptSource); return (result instanceof GString) ? result.toString() : result; }
@Override protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception { Assert.notNull(scriptSource, "scriptSource must not be null"); VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator(); if (this.customizer != null) { customizerDecorator.setCustomizer(this.customizer); } if (!CollectionUtils.isEmpty(variables)) { customizerDecorator.setVariables(variables); } GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); if (this.beanClassLoader != null) { factory.setBeanClassLoader(this.beanClassLoader); } if (this.beanFactory != null) { factory.setBeanFactory(this.beanFactory); } Object result = factory.getScriptedObject(scriptSource); return (result instanceof GString) ? result.toString() : result; }
@Override public void registerGroovyController(String scriptLocation) throws IOException { if (scriptNotExists(scriptLocation)) { throw new IllegalArgumentException("script not exists : " + scriptLocation); } scriptLastModifiedMap.put(scriptLocation, scriptLastModified(scriptLocation)); // Create script factory bean definition. GroovyScriptFactory groovyScriptFactory = new GroovyScriptFactory(scriptLocation); groovyScriptFactory.setBeanFactory(getBeanFactory()); groovyScriptFactory.setBeanClassLoader(getBeanFactory().getBeanClassLoader()); Object controller = groovyScriptFactory .getScriptedObject(new ResourceScriptSource(getApplicationContext().getResource(scriptLocation))); String controllerBeanName = scriptLocation; // 1、如果RequestMapping存在则移除 removeRequestMappingIfNecessary(controllerBeanName); if (getBeanFactory().containsBean(controllerBeanName)) { getBeanFactory().destroySingleton(controllerBeanName); // 移除单例bean // 移除注入缓存 否则Caused by: java.lang.IllegalArgumentException: object is not an // instance of declaring class getInjectionMetadataCache().remove(controller.getClass().getName()); } // 2、注册新的GroovyController getBeanFactory().registerSingleton(controllerBeanName, controller); // 注册单例bean getBeanFactory().autowireBean(controller); // 自动注入 // 3、注册新的RequestMapping registerRequestMappingIfNecessary(controllerBeanName); }