ImportAttributeModel { public void execute(String name, String scope, String toName, boolean ignore, Request request) { Map<String, Object> attributes = getImportedAttributes( name, toName, ignore, request); if (scope == null) { scope = request.getAvailableScopes().get(0); } request.getContext(scope).putAll(attributes); }  void execute(String name, String scope, String toName, boolean ignore, Request request);  }
@Test public void testExecuteSingle() { TilesContainer container = createMock(TilesContainer.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); Request request = createMock(Request.class); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andReturn("myEvaluatedValue"); replay(container, attributeContext, request, applicationContext); model.execute("myName", "request", null, false, request); Map<String, Object> attributes = requestScope; assertEquals(2, attributes.size()); assertEquals("myEvaluatedValue", attributes.get("myName")); verify(container, attributeContext, request, applicationContext); }
@Test public void testExecuteSingleToName() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andReturn("myEvaluatedValue"); replay(container, attributeContext, request, applicationContext); model.execute("myName", "request", "myToName", false, request); Map<String, Object> attributes = requestScope; assertEquals(2, attributes.size()); assertEquals("myEvaluatedValue", attributes.get("myToName")); verify(container, attributeContext, request, applicationContext); }
@Test public void testExecuteAll() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute1 = new Attribute("myValue1"); Attribute attribute2 = new Attribute("myValue2"); Attribute attribute3 = new Attribute("myValue3"); Set<String> cascadedNames = new HashSet<String>(); cascadedNames.add("myName1"); cascadedNames.add("myName2"); Set<String> localNames = new HashSet<String>(); localNames.add("myName1"); localNames.add("myName3"); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getCascadedAttributeNames()).andReturn(cascadedNames); expect(attributeContext.getLocalAttributeNames()).andReturn(localNames); expect(attributeContext.getAttribute("myName1")).andReturn(attribute1).times(2); expect(attributeContext.getAttribute("myName2")).andReturn(attribute2); expect(attributeContext.getAttribute("myName3")).andReturn(attribute3); expect(container.evaluate(attribute1, request)).andReturn("myEvaluatedValue1").times(2); expect(container.evaluate(attribute2, request)).andReturn("myEvaluatedValue2"); expect(container.evaluate(attribute3, request)).andReturn("myEvaluatedValue3"); replay(container, attributeContext, request, applicationContext); model.execute(null, "request", null, false, request); Map<String, Object> attributes = requestScope; assertEquals(ATTRIBUTES_SIZE, attributes.size()); assertEquals("myEvaluatedValue1", attributes.get("myName1")); assertEquals("myEvaluatedValue2", attributes.get("myName2")); assertEquals("myEvaluatedValue3", attributes.get("myName3")); verify(container, attributeContext, request, applicationContext); }
@Test(expected = NoSuchAttributeException.class) public void testExecuteSingleNullAttributeException() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(null); replay(container, attributeContext, request, applicationContext); try { model.execute("myName", "request", null, false, request); } finally { verify(container, attributeContext, request, applicationContext); } }
@Test(expected = NoSuchAttributeException.class) public void testExecuteSingleNullAttributeValueException() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andReturn(null); replay(container, attributeContext, request, applicationContext); try { model.execute("myName", "request", null, false, request); } finally { verify(container, attributeContext, request, applicationContext); } }
@Test(expected = RuntimeException.class) public void testExecuteSingleRuntimeException() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andThrow(new RuntimeException()); replay(container, attributeContext, request, applicationContext); try { model.execute("myName", "request", null, false, request); } finally { verify(container, attributeContext, request, applicationContext); } }
@Test public void testExecuteSingleNullAttributeIgnore() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(null); replay(container, attributeContext, request, applicationContext); model.execute("myName", "request", null, true, request); verify(container, attributeContext, request, applicationContext); }
@Test public void testExecuteSingleNullAttributeValueIgnore() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andReturn(null); replay(container, attributeContext, request, applicationContext); model.execute("myName", "request", null, true, request); verify(container, attributeContext, request, applicationContext); }
@Test public void testExecuteSingleRuntimeIgnore() { TilesContainer container = createMock(TilesContainer.class); Request request = createMock(Request.class); AttributeContext attributeContext = createMock(AttributeContext.class); Attribute attribute = new Attribute(); ApplicationContext applicationContext = createMock(ApplicationContext.class); Map<String, Object> requestScope = new HashMap<String, Object>(); requestScope.put(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); expect(request.getApplicationContext()).andReturn(applicationContext); expect(request.getContext("request")).andReturn(requestScope).anyTimes(); expect(container.getAttributeContext(request)).andReturn(attributeContext); expect(attributeContext.getAttribute("myName")).andReturn(attribute); expect(container.evaluate(attribute, request)).andThrow(new RuntimeException()); replay(container, attributeContext, request, applicationContext); model.execute("myName", "request", null, true, request); verify(container, attributeContext, request, applicationContext); }