Skip to content

Commit

Permalink
Changes in the template evaluation, consider the controller name
Browse files Browse the repository at this point in the history
- Test of acceptance of get paths
- Test if the template is been used as the default path
- CHanges ins controller mapping
- Consider controller class name as controller name
  • Loading branch information
efraimgentil committed Mar 15, 2015
1 parent f090524 commit bace902
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 14 deletions.
10 changes: 10 additions & 0 deletions cloverx-acceptance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<artifactId>selenium-firefox-driver</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ public class MainApp {
private CloverX cloverx;


public void start( CloverXConfiguration configuration){
cloverx = new CloverX( configuration );
}

public void start(){
cloverx = new CloverX( configure() );
start( configure() );
}

public CloverXConfiguration configure(){
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.withPackageToScan("org.esmerilprogramming.cloverxacceptance")
.shouldRunManagement(false);
.shouldRunManagement(false)
.withAppContext("acceptance");
return cb.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Index with Template</title>
</head>
<body>
<h1 id="pageTitle">Index with template</h1>
<br/>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.esmerilprogramming.cloverxacceptance;

import org.esmerilprogramming.cloverxacceptance.main.MainApp;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import static org.junit.Assert.*;

/**
* Created by efraimgentil<[email protected]> on 15/03/15.
*/
public class GetRoutesAcceptance {

WebDriver webDriver;
static MainApp mainApp;

@BeforeClass
public static void initClass(){
mainApp = new MainApp();
mainApp.start();
}

@AfterClass
public static void finish(){
mainApp.stop();
}

@Before
public void initTest(){
webDriver = new FirefoxDriver();
}

@After
public void endTest(){
webDriver.quit();
}

@Test
public void doesGetIndexWithoutTemplatePage() throws InterruptedException{
webDriver.get("localhost:8080/acceptance/index/index");
String pageSource = webDriver.getPageSource();
assertTrue( pageSource.contains("GET - index/index"));
}

@Test
public void doesGetINdexWithTemplatePage() throws InterruptedException{
webDriver.get("localhost:8080/acceptance/index/indexWithTemplate");
WebElement title = webDriver.findElement(By.id("pageTitle"));
assertTrue("Index with template".equalsIgnoreCase(title.getText()));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {

String template() default "";
String template() default Path.NO_TEMPLATE;

String[] value() default Path.NO_PATH;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public static final String NO_PATH = "$$NO_PATH$$";
public static final String NO_TEMPLATE = "$$NO_TEMPLATE$$";

String template() default "";
String template() default Path.NO_TEMPLATE;

String[] value() default NO_PATH;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Post {

String template() default "";
String template() default Path.NO_TEMPLATE;

String[] value() default Path.NO_PATH;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ControllerScanner {
public ControllerMapping scanControllerForMapping(Class<?> controllerClass){

Controller annotation = controllerClass.getAnnotation(Controller.class);
ControllerMapping mapping = new ControllerMapping( getPath( annotation , controllerClass ) );
ControllerMapping mapping = new ControllerMapping( getControllerSimpleName(controllerClass) , getPath( annotation , controllerClass ) );
mapping.setControllerClass(controllerClass);
mapping.addPathMethods( ReflectionUtils.getAllMethods(controllerClass, ReflectionUtils.withAnnotation(Page.class) ) );
mapping.addPathMethods( ReflectionUtils.getAllMethods(controllerClass, ReflectionUtils.withAnnotation(Get.class)) );
Expand All @@ -34,17 +34,27 @@ public String getPath( Controller annotation , Class<?> controllerClass ){
String path = annotation.path();
StringBuilder pathBuilder = new StringBuilder(path);
if(NO_PATH.equalsIgnoreCase( pathBuilder.toString() )){
pathBuilder = new StringBuilder( controllerClass.getSimpleName() );
path = getControllerSimpleName( controllerClass );
/*pathBuilder = new StringBuilder( controllerClass.getSimpleName() );
path = controllerClass.getSimpleName();
if( path.matches(".{1,}Controller") ){
pathBuilder.reverse().replace( 0 , 10 , "" ).reverse().toString();
}
path = pathBuilder.replace(0 , 1 , ((Character) pathBuilder.charAt(0) ).toString().toLowerCase() ).toString();
path = pathBuilder.replace(0 , 1 , ((Character) pathBuilder.charAt(0) ).toString().toLowerCase() ).toString();*/
}
if(!path.startsWith("/")){
path = "/" + path;
}
return path;
}

public String getControllerSimpleName( Class<?> controllerClass ){
StringBuilder sb = new StringBuilder( controllerClass.getSimpleName() );
String simpleName = controllerClass.getSimpleName();
if( simpleName.matches(".{1,}Controller") ){
sb.reverse().replace( 0 , 10 , "" ).reverse().toString();
}
return sb.replace(0 , 1 , ((Character) sb.charAt(0) ).toString().toLowerCase() ).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
public class ControllerMapping {

private Class<?> controllerClass;
private String name;
private String path;
private Set<PathMapping> pathMappings;
private Set<Method> beforeTranslationMethods;

public ControllerMapping(String path) {
public ControllerMapping(String name , String path) {
this.name = name;
this.path = path;
pathMappings = new LinkedHashSet<>();
beforeTranslationMethods = new LinkedHashSet<>();
Expand All @@ -36,6 +38,11 @@ public void addPathMethods(Set<Method> methods) {
for (VerbAndPaths vap : vaps) {
for (String path : vap.paths) {
path = Path.NO_PATH.equals(path) ? m.getName() : path.trim();
if(!Path.NO_TEMPLATE.equalsIgnoreCase( vap.template)) {
if (!vap.template.startsWith("/")) { //Mount template with the controllerName/templateName
vap.template = name + "/" + vap.template;
}
}
pathMappings.add(new PathMapping(path, vap.httpVerb, m, vap.template, vap.jsonResponse));
}
}
Expand Down Expand Up @@ -149,6 +156,10 @@ public String getPath() {
public Set<PathMapping> getPathMappings() {
return pathMappings;
}


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ protected HttpHandler mount405( Class methodNotAllowed ){

protected HttpHandler createErrorController(String error , Class<?> clazz , Class defaultClass){
clazz = clazz == null ? defaultClass : clazz;
ControllerMapping controllerMapping = new ControllerMapping(error);
ControllerMapping controllerMapping = new ControllerMapping( error , error);
controllerMapping.setControllerClass( clazz );
try {
Method handlerError = clazz.getMethod("handleError", CloverXRequest.class);
PathMapping methodMapping = new PathMapping("404" , null , handlerError , getTemplate(handlerError) , false );
PathMapping methodMapping = new PathMapping( error , null , handlerError , getTemplate(handlerError) , false );
Paranamer paranamer = new CachingParanamer(new BytecodeReadingParanamer());
return new MainHttpHandler( controllerMapping , methodMapping , paranamer.lookupParameterNames( handlerError ) );
} catch (NoSuchMethodException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ControllerMappingTest {

@Before
public void setUp(){
controllerMapping = new ControllerMapping("does_not_matter_the_path");
controllerMapping = new ControllerMapping("does_not_matter_the_path" ,"does_not_matter_the_path");
}

@Test
Expand Down

0 comments on commit bace902

Please sign in to comment.