Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
Fix ControllerEnhancer for Redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebort committed Apr 1, 2011
1 parent 0d03c96 commit 8820ae8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
53 changes: 27 additions & 26 deletions framework/src/play/classloading/enhancers/ControllersEnhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,40 @@ public void edit(FieldAccess fieldAccess) throws CannotCompileException {
}
}

// Auto redirect -->
if(!isScalaObject(ctClass) && Modifier.isPublic(ctMethod.getModifiers()) && ((ctClass.getName().endsWith("$") && !ctMethod.getName().contains("$default$"))) && !isHandler) {

try {
ctMethod.insertBefore(
"if(play.mvc.Controller._currentReverse.get() != null) {"
+ "play.mvc.Controller.redirect(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\", $args);"
+ generateValidReturnStatement(ctMethod.getReturnType())
+ "}");
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (autoredirect).", applicationClass.name, ctMethod.getName());
throw new UnexpectedException(e);
if (isScalaObject(ctClass)) {

// Auto reverse -->
if (Modifier.isPublic(ctMethod.getModifiers()) && ((ctClass.getName().endsWith("$") && !ctMethod.getName().contains("$default$"))) && !isHandler) {
try {
ctMethod.insertBefore(
"if(play.mvc.Controller._currentReverse.get() != null) {"
+ "play.mvc.Controller.redirect(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\", $args);"
+ generateValidReturnStatement(ctMethod.getReturnType())
+ "}");
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (auto-reverse).", applicationClass.name, ctMethod.getName());
throw new UnexpectedException(e);
}
}

} else {

if (Modifier.isPublic(ctMethod.getModifiers()) && ((ctClass.getName().endsWith("$") && !ctMethod.getName().contains("$default$")) || (Modifier.isStatic(ctMethod.getModifiers()) && ctMethod.getReturnType().equals(CtClass.voidType))) && !isHandler) {
// Auto redirect -->
if (Modifier.isPublic(ctMethod.getModifiers()) && !isHandler) {
try {
ctMethod.insertBefore(
"if(!play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation.isActionCallAllowed()) {"
+ "play.mvc.Controller.redirect(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\", $args);"
+ generateValidReturnStatement(ctMethod.getReturnType()) + "}"
+ generateValidReturnStatement(ctMethod.getReturnType()) + "}"
+ "play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation.stopActionCall();");
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (autoredirect).", applicationClass.name, ctMethod.getName());
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (auto-redirect).", applicationClass.name, ctMethod.getName());
throw new UnexpectedException(e);
}
}

}



// Enhance global catch to avoid potential unwanted catching of play.mvc.results.Result
ctMethod.instrument(new ExprEditor() {

Expand Down Expand Up @@ -187,31 +188,31 @@ public static void stopActionCall() {
}

static String generateValidReturnStatement(CtClass type) {
if(type.equals(CtClass.voidType)) {
if (type.equals(CtClass.voidType)) {
return "return;";
}
if(type.equals(CtClass.booleanType)) {
if (type.equals(CtClass.booleanType)) {
return "return false;";
}
if(type.equals(CtClass.charType)) {
if (type.equals(CtClass.charType)) {
return "return '';";
}
if(type.equals(CtClass.byteType)) {
if (type.equals(CtClass.byteType)) {
return "return (byte)0;";
}
if(type.equals(CtClass.doubleType)) {
if (type.equals(CtClass.doubleType)) {
return "return (double)0;";
}
if(type.equals(CtClass.floatType)) {
if (type.equals(CtClass.floatType)) {
return "return (float)0;";
}
if(type.equals(CtClass.intType)) {
if (type.equals(CtClass.intType)) {
return "return (int)0;";
}
if(type.equals(CtClass.longType)) {
if (type.equals(CtClass.longType)) {
return "return (long)0;";
}
if(type.equals(CtClass.shortType)) {
if (type.equals(CtClass.shortType)) {
return "return (short)0;";
}
return "return null;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,3 @@ Selenium.prototype.assertTextLike = function(a, b) {
Assert.fail(a+' == '+b);
}
};

Selenium.prototype.assertPath = function(expectedPath) {
var path = window.document.location.path;
if(path == expectedPath) {
Assert.fail('Expected path '+expectedPath+' but was '+path);
}
};
14 changes: 0 additions & 14 deletions samples-and-tests/just-test-cases/test/CRUD.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@
#{selenium 'Test the CRUD module'}

open('@{CRUD.index()}')
assertPath('/admin')

// Create a company
clickAndWait('link=AdminCompany')
assertTextPresent('0 AdminCompany')
assertPath('/admin/admincompany')
clickAndWait('link=Add Company')
assertPath('/admin/admincompany/new')
assertTextPresent('version')
assertElementPresent('object_version')
type('object_name', 'Zenexity')
clickAndWait('css=input[name=_save]')
assertTextPresent('has been created')
assertTextPresent('Zenexity')
assertPath('/admin/admincompany')
clickAndWait('link=Zenexity')
assertTextPresent('Edit Company')
assertTextPresent('version')
assertElementPresent('object_version')

// Create a project
clickAndWait('link=Home')
assertPath('/admin')
clickAndWait('link=Projects')
assertPath('/admin/projects')
assertTextPresent('0 Projects')
clickAndWait('link=Add Project')
assertPath('/admin/projects/new')
type('object_name', 'Play')
select('object_company', 'Zenexity')
clickAndWait('css=input[name=_save]')
Expand All @@ -41,12 +34,9 @@

// Create a factory
clickAndWait('link=Home')
assertPath('/admin')
clickAndWait('link=Factories')
assertPath('/admin/factories')
assertTextPresent('0 Factories')
clickAndWait('link=Add Factory')
assertPath('/admin/factories/new')
type('object_name', 'Cars')
clickAndWait('css=input[name=_save]')
assertTextPresent('has been created')
Expand Down Expand Up @@ -89,20 +79,16 @@

// Create a company
open('@{CRUD.index()}')
assertPath('/admin')
clickAndWait('link=CustomAdminCompany')
assertTextPresent('1 CustomAdminCompany')
assertPath('/admin/customadmincompany')
clickAndWait('link=Add Company')
assertPath('/admin/customadmincompany/new')
assertNotVisible('object_version')
assertTextNotPresent('version')
assertElementPresent('object_version')
type('object_name', 'Zenexity 2')
clickAndWait('css=input[name=_save]')
assertTextPresent('has been created')
assertTextPresent('Zenexity 2')
assertPath('/admin/customadmincompany')
clickAndWait('link=Zenexity 2')
assertTextPresent('Edit Company')
assertNotVisible('object_version')
Expand Down

0 comments on commit 8820ae8

Please sign in to comment.