TilesAccess { public static void setCurrentContainer(Request request, String key) { ApplicationContext applicationContext = request.getApplicationContext(); TilesContainer container = getContainer(applicationContext, key); if (container != null) { request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container); } else { throw new NoSuchContainerException("The container with the key '" + key + "' cannot be found"); } } 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 testSetCurrentContainer() { 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>(); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); expect(request.getContext("request")).andReturn(requestScope); expect(request.getApplicationContext()).andReturn(context); replay(request, context, container); TilesAccess.setCurrentContainer(request, "myKey"); assertEquals(container, requestScope.get(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME)); verify(request, context, container); }
@Test(expected = NoSuchContainerException.class) public void testSetCurrentContainerException() { Request request = createMock(Request.class); ApplicationContext context = createMock(ApplicationContext.class); Map<String, Object> attribs = new HashMap<String, Object>(); expect(request.getApplicationContext()).andReturn(context); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); replay(request, context); try { TilesAccess.setCurrentContainer(request, "myKey"); } finally { verify(request, context); } }
@Test public void testSetCurrentContainerWithContainer() { 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>(); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); expect(request.getContext("request")).andReturn(requestScope); replay(request, context, container); TilesAccess.setCurrentContainer(request, container); assertEquals(container, requestScope.get(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME)); verify(request, context, container); }
@Test(expected = NullPointerException.class) public void testSetCurrentContainerWithContainerException() { Request request = createMock(Request.class); ApplicationContext context = createMock(ApplicationContext.class); Map<String, Object> attribs = new HashMap<String, Object>(); expect(context.getApplicationScope()).andReturn(attribs).anyTimes(); replay(request, context); try { TilesAccess.setCurrentContainer(request, (TilesContainer) null); } finally { verify(request, context); } }