TilesAccess { public static TilesContainer getCurrentContainer(Request request) { ApplicationContext context = request.getApplicationContext(); Map<String, Object> requestScope = request.getContext("request"); TilesContainer container = (TilesContainer) requestScope.get(CURRENT_CONTAINER_ATTRIBUTE_NAME); if (container == null) { container = getContainer(context); requestScope.put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container); } return container; } private  TilesAccess(); static void setContainer(ApplicationContext context,
            TilesContainer container); static void setContainer(ApplicationContext context,
            TilesContainer container, String key); static TilesContainer getContainer(ApplicationContext context); static TilesContainer getContainer(ApplicationContext context,
            String key); static void setCurrentContainer(Request request,
            String key); static void setCurrentContainer(Request request,
            TilesContainer container); static TilesContainer getCurrentContainer(Request request); static final String CURRENT_CONTAINER_ATTRIBUTE_NAME; static final String CONTAINER_ATTRIBUTE; }
@Test public void testGetCurrentContainer() { Request request = createMock(Request.class); ApplicationContext context = createMock(ApplicationContext.class); TilesContainer container = createMock(TilesContainer.class); Map<String, Object> attribs = new HashMap<String, Object>(); attribs.put("myKey", container); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(context); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); expect(request.getContext("request")).andReturn(requestScope); replay(request, context, container); assertEquals(container, TilesAccess.getCurrentContainer(request)); verify(request, context, container); }
@Test public void testGetCurrentContainerDefault() { Request request = createMock(Request.class); ApplicationContext context = createMock(ApplicationContext.class); TilesContainer container = createMock(TilesContainer.class); Map<String, Object> attribs = new HashMap<String, Object>(); attribs.put(TilesAccess.CONTAINER_ATTRIBUTE, container); Map<String, Object> requestScope = new HashMap<String, Object>(); expect(request.getApplicationContext()).andReturn(context); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); expect(request.getContext("request")).andReturn(requestScope); replay(request, context, container); assertEquals(container, TilesAccess.getCurrentContainer(request)); verify(request, context, container); }