Skip to content

Commit

Permalink
Renaming FreeMaker* and Freemaker* to Freemarker* and blocking access…
Browse files Browse the repository at this point in the history
… to all routes page when in production environment
  • Loading branch information
luiz authored and narcisobenigno committed Sep 16, 2011
1 parent a9aaf44 commit c7c2cae
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import br.com.caelum.vraptor.resource.HttpMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;

public class FreeMakerRoute {
public class FreemarkerRoute {

private final Route route ;
private final MutableRequest request;

public FreeMakerRoute(Route route, MutableRequest request) {
public FreemarkerRoute(Route route, MutableRequest request) {
this.route = route;
this.request = request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.freemarker.Freemarker;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.route.Route;
Expand All @@ -17,42 +18,49 @@

@Resource
public class RoutesController {


private static final String PRODUCTION = "production";
private static final String INDEX = "routes/index";

private final Router router;
private final Freemarker maker;
private final Freemarker marker;
private final MutableRequest request;
private final Environment environment;

public RoutesController(Router router, MutableRequest request, Freemarker maker) {
public RoutesController(Router router, MutableRequest request, Freemarker marker, Environment environment) {
this.router = router;
this.request = request;
this.maker = maker;
this.marker = marker;
this.environment = environment;
}

@Path("/dash/routes") @Get
public void allRoutes() throws IOException, TemplateException {
if (PRODUCTION.equals(environment.getName())) {
throw new UnsupportedOperationException();
}

List<Route> routes = orderRoutesByURI(router.allRoutes());
List<FreeMakerRoute> freemakerRoutes = createRoutesForFreeMaker(routes);
this.maker.use(INDEX).with("routes", freemakerRoutes).render();
List<FreemarkerRoute> freemakerRoutes = createRoutesForFreeMarker(routes);
this.marker.use(INDEX).with("routes", freemakerRoutes).render();
}

private List<FreeMakerRoute> createRoutesForFreeMaker(List<Route> routes) {
List<FreeMakerRoute> freemakerRoutes = new ArrayList<FreeMakerRoute>();
private List<FreemarkerRoute> createRoutesForFreeMarker(List<Route> routes) {
List<FreemarkerRoute> freemakerRoutes = new ArrayList<FreemarkerRoute>();
for (Route route : routes) {
freemakerRoutes.add(new FreeMakerRoute(route, this.request));
freemakerRoutes.add(new FreemarkerRoute(route, this.request));
}
return freemakerRoutes;
}

private List<Route> orderRoutesByURI(List<Route> allRoutes) {
List<Route> routes = new ArrayList<Route>(allRoutes);
Collections.sort(routes, new CompareByRouteToString());
Collections.sort(routes, new RouteUriComparator());
return routes;
}
private final class CompareByRouteToString implements Comparator<Route> {

private final class RouteUriComparator implements Comparator<Route> {

public int compare(Route r1, Route r2) {
return r1.getOriginalUri().compareTo(r2.getOriginalUri());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
package br.com.caelum.vraptor.dash.monitor;

import static org.mockito.Mockito.mock;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.lang.reflect.Method;
import java.util.EnumSet;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.route.Route;
import br.com.caelum.vraptor.resource.DefaultResourceClass;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.HttpMethod;

public class FreemakerRouteTest {
@RunWith(MockitoJUnitRunner.class)
public class FreemarkerRouteTest {

private FreeMakerRoute route;
private Route routeMock;
private MutableRequest requestMock;
private FreemarkerRoute route;
private @Mock Route routeMock;
private @Mock MutableRequest requestMock;
private DefaultResourceMethod resourceMethod;

@Before
public void setup() {
this.requestMock = mock(MutableRequest.class);
this.routeMock = mock(Route.class);
this.route = new FreeMakerRoute(routeMock, requestMock);
this.route = new FreemarkerRoute(routeMock, requestMock);
}

@Test
public void returnsControllerAndMethodName() throws SecurityException, NoSuchMethodException {
Class<?> type = RoutesController.class;
Expand All @@ -38,27 +39,27 @@ public void returnsControllerAndMethodName() throws SecurityException, NoSuchMet
String uri = "/dash/routes";
when(routeMock.getOriginalUri()).thenReturn(uri);
when(routeMock.resourceMethod(this.requestMock, uri)).thenReturn(this.resourceMethod);
Assert.assertEquals("RoutesController.allRoutes", route.getControllerAndMethodName());
assertEquals("RoutesController.allRoutes", route.getControllerAndMethodName());
}


@Test
public void returnsGETandPOSTWhenHttpMethodsGETAndPOSTAllowed() {
when(routeMock.allowedMethods()).thenReturn(EnumSet.of(HttpMethod.GET, HttpMethod.POST));
Assert.assertEquals("[GET POST]", route.getAllowedMethods());
}
assertEquals("[GET POST]", route.getAllowedMethods());
}

@Test
public void returnsPUTandDeleteWhenHttpMethodsPutAndDeleteAllowed() {
when(routeMock.allowedMethods()).thenReturn(EnumSet.of(HttpMethod.PUT, HttpMethod.DELETE));
Assert.assertEquals("[PUT DELETE]", route.getAllowedMethods());
assertEquals("[PUT DELETE]", route.getAllowedMethods());
}

@Test
public void returnsALLWhenAllHttpMethodsAllowed() {
when(routeMock.allowedMethods()).thenReturn(EnumSet.allOf(HttpMethod.class));
Assert.assertEquals("[ALL]", route.getAllowedMethods());
assertEquals("[ALL]", route.getAllowedMethods());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package br.com.caelum.vraptor.dash.monitor;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.freemarker.Freemarker;
import br.com.caelum.vraptor.freemarker.Template;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.route.Router;

@RunWith(MockitoJUnitRunner.class)
public class RoutesControllerTest {

private @Mock Router router;
private @Mock MutableRequest request;
private @Mock Freemarker marker;
private @Mock Template indexTemplate;
private @Mock Environment environment;

private RoutesController controller;

@Before
public void setUp() throws Exception {
this.controller = new RoutesController(router, request, marker, environment);

when(marker.use("routes/index")).thenReturn(indexTemplate);
when(indexTemplate.with(anyString(), any())).thenReturn(indexTemplate);
}

@Test
public void showsAllRegisteredRoutesWhenAccessedOutOfProductionEnvironment() throws Exception {
when(environment.getName()).thenReturn("notProduction");

controller.allRoutes();

verify(router).allRoutes();
}

@Test(expected=UnsupportedOperationException.class)
public void throwsExceptionWhenAccessedInProductionEnvironment() throws Exception {
when(environment.getName()).thenReturn("production");

controller.allRoutes();
}
}

0 comments on commit c7c2cae

Please sign in to comment.