Skip to content

Commit

Permalink
Fix for some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Nov 30, 2010
1 parent 0797a86 commit 5b7e85a
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 79 deletions.
13 changes: 13 additions & 0 deletions .springBeans
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.5.0.201010221000-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
*/
package org.codehaus.groovy.grails.commons.metaclass;

import groovy.lang.MetaMethod;
import groovy.lang.Closure;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.codehaus.groovy.reflection.CachedClass;
import org.codehaus.groovy.reflection.CachedMethod;
import org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod;
import org.springframework.util.ReflectionUtils;

/**
*
Expand All @@ -35,19 +38,32 @@
*
*/
public abstract class BaseApiProvider {

protected List<MetaMethod> instanceMethods = new ArrayList<MetaMethod>();
private static List<String> EXCLUDED_METHODS = new ArrayList<String>() {{
add("setMetaClass");
add("getMetaClass");
}};
private static Map<String, Integer> METHOD_CLOSURES = new HashMap<String, Integer>() {{
put("setProperty", 2);
put("getProperty", 1);
put("invokeMethod", 2);
put("methodMissing", 2);
put("propertyMissing", 1);

}};
@SuppressWarnings("rawtypes")
protected List instanceMethods = new ArrayList();
protected List<Method> staticMethods = new ArrayList<Method>();

@SuppressWarnings("unchecked")
public void addApi(final Object apiInstance) {
if(apiInstance != null) {
Class<?> currentClass = apiInstance.getClass();
while(currentClass != Object.class) {
final Method[] declaredMethods = currentClass.getDeclaredMethods();

for (Method method : declaredMethods) {
for (final Method method : declaredMethods) {
final int modifiers = method.getModifiers();
if(Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers)) {
if(isNotExcluded(method, modifiers)) {
if(Modifier.isStatic(modifiers)) {
staticMethods.add(method);
}
Expand All @@ -67,14 +83,27 @@ public CachedClass[] getParameterTypes() {
else
return paramTypes;
}
});
});

}
}
}
currentClass = currentClass.getSuperclass();
}
}
}

private boolean isNotExcluded(Method method, final int modifiers) {
final String name = method.getName();

if(EXCLUDED_METHODS.contains(name)) return false;

Integer parameterCount = METHOD_CLOSURES.get(name);
return Modifier.isPublic(modifiers) &&
!Modifier.isAbstract(modifiers) &&
!name.contains("$") &&
!(parameterCount != null && (parameterCount == method.getParameterTypes().length));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package org.codehaus.groovy.grails.commons.metaclass

import java.lang.reflect.Method;

import groovy.lang.ExpandoMetaClass;
import org.codehaus.groovy.runtime.MethodClosure
import org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod;

import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;

Expand All @@ -32,8 +34,9 @@ import groovy.lang.MetaMethod;
class MetaClassEnhancer extends BaseApiProvider {

public void enhance(MetaClass metaClass) {
for (MetaMethod method : instanceMethods) {
metaClass.registerInstanceMethod(method)
for (method in instanceMethods) {
if(method instanceof ReflectionMetaMethod)
metaClass.registerInstanceMethod(method)
}

for (Method method : staticMethods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession

import org.codehaus.groovy.grails.commons.metaclass.MetaClassEnhancer;
import org.codehaus.groovy.grails.plugins.web.api.AttributeAccessor;
import org.codehaus.groovy.grails.plugins.web.api.ServletRequestApi;
import org.springframework.web.util.WebUtils

Expand All @@ -41,18 +40,62 @@ class ServletsGrailsPlugin {
def dependsOn = [core:version]

private MetaClassEnhancer requestEnhancer
private MetaClassEnhancer attrEnhancer


public ServletsGrailsPlugin() {
this.requestEnhancer = new MetaClassEnhancer()
requestEnhancer.addApi new ServletRequestApi()
this.attrEnhancer = new MetaClassEnhancer()
attrEnhancer.addApi new AttributeAccessor()

}
def doWithDynamicMethods = { ctx ->
def getAttributeClosure = { String name ->
def mp = delegate.class.metaClass.getMetaProperty(name)
return mp ? mp.getProperty(delegate) : delegate.getAttribute(name)
}

def setAttributeClosure = { String name, value ->
def mp = delegate.class.metaClass.getMetaProperty(name)
if (mp) {
mp.setProperty(delegate, value)
}
else {
delegate.setAttribute(name, value)
}
}

def getAttributeSubscript = { String key ->
delegate.getAttribute(key)
}
def setAttributeSubScript = { String key, Object val ->
delegate.setAttribute(key, val)
}
// enables acces to servlet context with servletContext.foo syntax
ServletContext.metaClass.getProperty = getAttributeClosure
ServletContext.metaClass.setProperty = setAttributeClosure
ServletContext.metaClass.getAt = getAttributeSubscript
ServletContext.metaClass.putAt = setAttributeSubScript

// enables access to session attributes using session.foo syntax
HttpSession.metaClass.getProperty = getAttributeClosure
HttpSession.metaClass.setProperty = setAttributeClosure
// enables access to session attributes with session["foo"] syntax
HttpSession.metaClass.getAt = getAttributeSubscript
// enables setting of session attributes with session["foo"] = "bar" syntax
HttpSession.metaClass.putAt = setAttributeSubScript
// enables access to request attributes with request["foo"] syntax
HttpServletRequest.metaClass.getAt = { String key ->
delegate.getAttribute(key)
}
// enables setting of request attributes with request["foo"] = "bar" syntax
HttpServletRequest.metaClass.putAt = { String key, Object val ->
delegate.setAttribute(key, val)
}
// enables access to request attributes using property syntax
HttpServletRequest.metaClass.getProperty = getAttributeClosure
HttpServletRequest.metaClass.setProperty = setAttributeClosure

requestEnhancer.enhance HttpServletRequest.metaClass
attrEnhancer.enhanceAll( [ServletContext.metaClass, HttpSession.metaClass] )


// allows the syntax response << "foo"
HttpServletResponse.metaClass.leftShift = { Object o ->
delegate.writer << o
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletRequest;
*
*/

class ServletRequestApi extends AttributeAccessor{
class ServletRequestApi {

/**
* @return retrieve the forwardURI for the request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.codehaus.groovy.grails.plugins.web

import groovy.lang.GroovySystem;

import org.codehaus.groovy.grails.commons.test.*
import org.codehaus.groovy.grails.commons.metaclass.*
import org.codehaus.groovy.grails.commons.spring.*
import org.springframework.mock.web.*

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpSession;

import org.springframework.web.util.*

class ServletsGrailsPluginTests extends AbstractGrailsPluginTests {
Expand Down Expand Up @@ -37,7 +43,9 @@ class ServletsGrailsPluginTests extends AbstractGrailsPluginTests {
void testServletContextObject() {
def context = new MockServletContext()

context["foo"] = "bar"
println context.metaClass.getMetaMethod("getProperty")
println ServletContext.metaClass.getMetaMethod("getProperty")
context["foo"] = "bar"
assertEquals "bar", context["foo"]

context.foo = "fred"
Expand All @@ -49,7 +57,9 @@ class ServletsGrailsPluginTests extends AbstractGrailsPluginTests {

void testHttpSessionObject() {
def session = new MockHttpSession()
assert session.creationTime
def httpSessionMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(HttpSession)
def metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(session.getClass())
assert session.getProperty("creationTime")

session["foo"] = "bar"
assertEquals "bar", session["foo"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ class IvyDependencyManagerTests extends GroovyTestCase {
def grailsVersion = getCurrentGrailsVersion()
manager.parseDependencies(IvyDependencyManager.getDefaultDependencies(grailsVersion))

assertEquals 54, manager.listDependencies('runtime').size()
assertEquals 57, manager.listDependencies('test').size()
assertEquals 20, manager.listDependencies('build').size()
assertEquals 56, manager.listDependencies('runtime').size()
assertEquals 59, manager.listDependencies('test').size()
assertEquals 19, manager.listDependencies('build').size()
assertEquals 2, manager.listDependencies('provided').size()
assertEquals 23, manager.listDependencies('docs').size()
assertEquals 22, manager.listDependencies('docs').size()

// This should be a functional test since it relies on the Grails
// JAR files being built. It also runs Ivy, which isn't ideal
Expand All @@ -501,9 +501,9 @@ class IvyDependencyManagerTests extends GroovyTestCase {

assertEquals 0, manager.listDependencies('runtime').size()
assertEquals 3, manager.listDependencies('test').size()
assertEquals 20, manager.listDependencies('build').size()
assertEquals 56, manager.listDependencies('provided').size()
assertEquals 23, manager.listDependencies('docs').size()
assertEquals 19, manager.listDependencies('build').size()
assertEquals 58, manager.listDependencies('provided').size()
assertEquals 22, manager.listDependencies('docs').size()

manager = new IvyDependencyManager("project", "0.1",settings)
defaultDependencyClosure = IvyDependencyManager.getDefaultDependencies(grailsVersion)
Expand All @@ -513,11 +513,11 @@ class IvyDependencyManagerTests extends GroovyTestCase {
defaultDependencyClosure()
}

assertEquals 54, manager.listDependencies('runtime').size()
assertEquals 57, manager.listDependencies('test').size()
assertEquals 20, manager.listDependencies('build').size()
assertEquals 56, manager.listDependencies('runtime').size()
assertEquals 59, manager.listDependencies('test').size()
assertEquals 19, manager.listDependencies('build').size()
assertEquals 2, manager.listDependencies('provided').size()
assertEquals 23, manager.listDependencies('docs').size()
assertEquals 22, manager.listDependencies('docs').size()
}

def getCurrentGrailsVersion() {
Expand Down

0 comments on commit 5b7e85a

Please sign in to comment.