This project has retired. For details please refer to its Attic page.
ChainedDelegateAttributeRendererTest xref
View Javadoc

1   /*
2    * $Id: ChainedDelegateAttributeRendererTest.java 821299 2009-10-03 12:15:05Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.renderer.impl;
22  
23  import static org.easymock.EasyMock.*;
24  
25  import java.io.IOException;
26  import java.io.StringWriter;
27  
28  import org.apache.tiles.Attribute;
29  import org.apache.tiles.Expression;
30  import org.apache.tiles.TilesApplicationContext;
31  import org.apache.tiles.context.TilesRequestContext;
32  import org.apache.tiles.context.TilesRequestContextFactory;
33  import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory;
34  import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator;
35  import org.apache.tiles.renderer.TypeDetectingAttributeRenderer;
36  import org.easymock.EasyMock;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  /***
41   * Tests {@link ChainedDelegateAttributeRenderer}.
42   *
43   * @version $Rev: 821299 $ $Date: 2009-10-03 14:15:05 +0200 (sab, 03 ott 2009) $
44   */
45  public class ChainedDelegateAttributeRendererTest {
46  
47      /***
48       * The renderer.
49       */
50      private ChainedDelegateAttributeRenderer renderer;
51  
52      /***
53       * A mock string attribute renderer.
54       */
55      private TypeDetectingAttributeRenderer stringRenderer;
56  
57      /***
58       * A mock template attribute renderer.
59       */
60      private TypeDetectingAttributeRenderer templateRenderer;
61  
62      /***
63       * A mock definition attribute renderer.
64       */
65      private TypeDetectingAttributeRenderer definitionRenderer;
66  
67      /***
68       * Sets up the test.
69       */
70      @Before
71      public void setUp() {
72          stringRenderer = createMock(TypeDetectingAttributeRenderer.class);
73          templateRenderer = createMock(TypeDetectingAttributeRenderer.class);
74          definitionRenderer = createMock(TypeDetectingAttributeRenderer.class);
75          renderer = new ChainedDelegateAttributeRenderer();
76          renderer.setAttributeEvaluatorFactory(new BasicAttributeEvaluatorFactory(
77                  new DirectAttributeEvaluator()));
78          renderer.addAttributeRenderer(definitionRenderer);
79          renderer.addAttributeRenderer(templateRenderer);
80          renderer.addAttributeRenderer(stringRenderer);
81      }
82  
83      /***
84       * Tests
85       * {@link ChainedDelegateAttributeRenderer#render(Attribute, TilesRequestContext)}
86       * writing a definition.
87       *
88       * @throws IOException If something goes wrong during rendition.
89       */
90      @Test
91      public void testWriteDefinition() throws IOException {
92          StringWriter writer = new StringWriter();
93          Attribute attribute = new Attribute("my.definition", (Expression) null,
94                  null, "definition");
95          TilesApplicationContext applicationContext = EasyMock
96                  .createMock(TilesApplicationContext.class);
97          TilesRequestContextFactory contextFactory = EasyMock
98                  .createMock(TilesRequestContextFactory.class);
99          TilesRequestContext requestContext = EasyMock
100                 .createMock(TilesRequestContext.class);
101 
102         expect(
103                 definitionRenderer.isRenderable("my.definition", attribute,
104                         requestContext)).andReturn(Boolean.TRUE);
105         definitionRenderer.render(attribute, requestContext);
106 
107         replay(applicationContext, contextFactory, requestContext,
108                 stringRenderer, templateRenderer, definitionRenderer);
109         renderer.setApplicationContext(applicationContext);
110         renderer.setRequestContextFactory(contextFactory);
111         renderer.render(attribute, requestContext);
112         writer.close();
113         verify(applicationContext, contextFactory, requestContext,
114                 stringRenderer, templateRenderer, definitionRenderer);
115     }
116 
117     /***
118      * Tests
119      * {@link ChainedDelegateAttributeRenderer#render(Attribute, TilesRequestContext)}
120      * writing a string.
121      *
122      * @throws IOException If something goes wrong during rendition.
123      */
124     @Test
125     public void testWriteString() throws IOException {
126         Attribute attribute = new Attribute("Result", (Expression) null, null,
127                 "string");
128         TilesApplicationContext applicationContext = EasyMock
129                 .createMock(TilesApplicationContext.class);
130         TilesRequestContextFactory contextFactory = EasyMock
131                 .createMock(TilesRequestContextFactory.class);
132         TilesRequestContext requestContext = EasyMock
133                 .createMock(TilesRequestContext.class);
134         expect(
135                 definitionRenderer.isRenderable("Result", attribute,
136                         requestContext)).andReturn(Boolean.FALSE);
137         expect(
138                 templateRenderer.isRenderable("Result", attribute,
139                         requestContext)).andReturn(Boolean.FALSE);
140         expect(
141                 stringRenderer.isRenderable("Result", attribute,
142                         requestContext)).andReturn(Boolean.TRUE);
143         stringRenderer.render(attribute, requestContext);
144 
145         replay(applicationContext, contextFactory, requestContext,
146                 stringRenderer, templateRenderer, definitionRenderer);
147         renderer.setApplicationContext(applicationContext);
148         renderer.setRequestContextFactory(contextFactory);
149         renderer.render(attribute, requestContext);
150         verify(applicationContext, contextFactory, requestContext,
151                 stringRenderer, templateRenderer, definitionRenderer);
152     }
153 
154     /***
155      * Tests
156      * {@link ChainedDelegateAttributeRenderer#render(Attribute, TilesRequestContext)}
157      * writing a template.
158      *
159      * @throws IOException If something goes wrong during rendition.
160      */
161     @Test
162     public void testWriteTemplate() throws IOException {
163         StringWriter writer = new StringWriter();
164         Attribute attribute = new Attribute("/myTemplate.jsp",
165                 (Expression) null, null, "template");
166         TilesApplicationContext applicationContext = EasyMock
167                 .createMock(TilesApplicationContext.class);
168         TilesRequestContextFactory contextFactory = EasyMock
169                 .createMock(TilesRequestContextFactory.class);
170         TilesRequestContext requestContext = EasyMock
171                 .createMock(TilesRequestContext.class);
172         templateRenderer.render(attribute, requestContext);
173         expect(
174                 definitionRenderer.isRenderable("/myTemplate.jsp", attribute,
175                         requestContext)).andReturn(Boolean.FALSE);
176         expect(
177                 templateRenderer.isRenderable("/myTemplate.jsp", attribute,
178                         requestContext)).andReturn(Boolean.TRUE);
179 
180         replay(applicationContext, contextFactory, requestContext,
181                 stringRenderer, templateRenderer, definitionRenderer);
182         renderer.setApplicationContext(applicationContext);
183         renderer.setRequestContextFactory(contextFactory);
184         renderer.render(attribute, requestContext);
185         writer.close();
186         verify(applicationContext, contextFactory, requestContext,
187                 stringRenderer, templateRenderer, definitionRenderer);
188     }
189 }