1. Project Clover database Fri Mar 6 2015 14:07:48 CET
  2. Package info.magnolia.module.blossom.web

File BlossomWebArgumentResolverTest.java

 

Code metrics

0
63
16
1
219
131
16
0.25
3.94
16
1

Classes

Class Line # Actions
BlossomWebArgumentResolverTest 71 63 0% 16 7
0.911392491.1%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.blossom.web;
35   
36    import java.lang.reflect.Method;
37   
38    import javax.jcr.Node;
39   
40    import com.google.inject.util.Providers;
41   
42    import org.junit.Test;
43    import org.springframework.core.MethodParameter;
44    import static org.easymock.classextension.EasyMock.createMock;
45    import static org.junit.Assert.assertEquals;
46    import static org.junit.Assert.assertSame;
47    import static org.mockito.Mockito.mock;
48    import static org.mockito.Mockito.when;
49   
50    import info.magnolia.cms.core.AggregationState;
51    import info.magnolia.cms.core.Content;
52    import info.magnolia.cms.security.MgnlUser;
53    import info.magnolia.cms.security.User;
54    import info.magnolia.context.Context;
55    import info.magnolia.context.MgnlContext;
56    import info.magnolia.context.WebContext;
57    import info.magnolia.module.blossom.template.BlossomTemplateDefinition;
58    import info.magnolia.rendering.context.AggregationStateBasedRenderingContext;
59    import info.magnolia.rendering.context.RenderingContext;
60    import info.magnolia.rendering.template.TemplateDefinition;
61    import info.magnolia.test.ComponentsTestUtil;
62    import info.magnolia.test.MgnlTestCase;
63    import info.magnolia.test.mock.MockContent;
64    import info.magnolia.test.mock.MockWebContext;
65   
66    /**
67    * Tests for BlossomWebArgumentResolver.
68    *
69    * @since 1.2
70    */
 
71    public class BlossomWebArgumentResolverTest extends MgnlTestCase {
72   
 
73  7 toggle @Override
74    protected void initContext() {
75  7 MgnlContext.setInstance(new MockWebContext());
76    }
77   
 
78  1 toggle @Test
79    public void testAggregationState() throws Exception {
80   
81  1 AggregationState state = new AggregationState();
82  1 MockWebContext context = (MockWebContext) MgnlContext.getInstance();
83  1 context.setAggregationState(state);
84   
85  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
86  1 Method method = getClass().getMethod("mockMethod", AggregationState.class, WebContext.class, Context.class);
87   
88  1 assertSame(state, resolver.resolveArgument(new MethodParameter(method, 0), null));
89  1 assertSame(context, resolver.resolveArgument(new MethodParameter(method, 1), null));
90  1 assertSame(context, resolver.resolveArgument(new MethodParameter(method, 2), null));
91    }
92   
 
93  0 toggle public void mockMethod(AggregationState aggregationState, WebContext webContext, Context context) {
94    }
95   
 
96  1 toggle @Test
97    public void testContentAsArgument() throws Exception {
98   
99  1 AggregationState state = new AggregationState();
100  1 MockWebContext context = (MockWebContext) MgnlContext.getInstance();
101  1 context.setAggregationState(state);
102   
103  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
104  1 Method method = getClass().getMethod("mockMethod", Content.class);
105   
106  1 MockContent templateContent = new MockContent("template");
107  1 state.setMainContent(templateContent);
108  1 assertSameContent(templateContent, (Content)resolver.resolveArgument(new MethodParameter(method, 0), null));
109   
110  1 MockContent paragraphContent = new MockContent("paragraph");
111  1 state.setCurrentContent(paragraphContent);
112  1 assertSameContent(paragraphContent, (Content)resolver.resolveArgument(new MethodParameter(method, 0), null));
113    }
114   
 
115  0 toggle public void mockMethod(Content content) {
116    }
117   
 
118  1 toggle @Test
119    public void testTwoArgumentsOfTypeContent() throws Exception {
120   
121  1 AggregationState state = new AggregationState();
122  1 MockWebContext context = (MockWebContext) MgnlContext.getInstance();
123  1 context.setAggregationState(state);
124   
125  1 MockContent templateContent = new MockContent("template");
126  1 state.setMainContent(templateContent);
127  1 MockContent paragraphContent = new MockContent("paragraph");
128  1 state.setCurrentContent(paragraphContent);
129   
130  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
131  1 Method method = getClass().getMethod("methodWithTwoContentArguments", Content.class, Content.class);
132   
133  1 assertSameContent(templateContent, (Content)resolver.resolveArgument(new MethodParameter(method, 0), null));
134  1 assertSameContent(paragraphContent, (Content)resolver.resolveArgument(new MethodParameter(method, 1), null));
135    }
136   
 
137  0 toggle public void methodWithTwoContentArguments(Content page, Content paragraph) {
138    }
139   
 
140  1 toggle @Test
141    public void testTwoArgumentsOfTypeNode() throws Exception {
142   
143  1 AggregationState state = new AggregationState();
144   
145  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
146  1 Method method = getClass().getMethod("methodWithTwoNodeArguments", Node.class, Node.class);
147   
148  1 MockContent pageContent = new MockContent("template");
149  1 state.setMainContent(pageContent);
150  1 MockContent paragraphContent = new MockContent("paragraph");
151  1 state.setCurrentContent(paragraphContent);
152   
153  1 ComponentsTestUtil.setInstance(RenderingContext.class, new AggregationStateBasedRenderingContext(Providers.of(state), null));
154   
155  1 assertSame(pageContent.getJCRNode(), resolver.resolveArgument(new MethodParameter(method, 0), null));
156  1 assertSame(paragraphContent.getJCRNode(), resolver.resolveArgument(new MethodParameter(method, 1), null));
157    }
158   
 
159  0 toggle public void methodWithTwoNodeArguments(Node page, Node paragraph) {
160    }
161   
 
162  1 toggle @Test
163    public void testUserAsArgument() throws Exception {
164   
165  1 MgnlUser mgnlUser = createMock(MgnlUser.class);
166   
167  1 MockWebContext context = (MockWebContext) MgnlContext.getInstance();
168  1 context.setUser(mgnlUser);
169   
170  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
171  1 Method method = getClass().getMethod("mockMethod", User.class, MgnlUser.class);
172   
173  1 assertSame(mgnlUser, resolver.resolveArgument(new MethodParameter(method, 0), null));
174  1 assertSame(mgnlUser, resolver.resolveArgument(new MethodParameter(method, 1), null));
175    }
176   
 
177  0 toggle public void mockMethod(User user, MgnlUser mgnlUser) {
178    }
179   
 
180  1 toggle @Test
181    public void testTemplateDefinitionAsArgument() throws Exception {
182   
183  1 RenderingContext renderingContext = mock(RenderingContext.class);
184  1 ComponentsTestUtil.setInstance(RenderingContext.class, renderingContext);
185   
186  1 TemplateDefinition templateDefinition = createMock(TemplateDefinition.class);
187  1 when(renderingContext.getRenderableDefinition()).thenReturn(templateDefinition);
188   
189  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
190  1 Method method = getClass().getMethod("methodWithTemplateDefinitionArgument", TemplateDefinition.class);
191   
192  1 assertSame(templateDefinition, resolver.resolveArgument(new MethodParameter(method, 0), null));
193    }
194   
 
195  0 toggle public void methodWithTemplateDefinitionArgument(TemplateDefinition templateDefinition) {
196    }
197   
 
198  1 toggle @Test
199    public void testBlossomTemplateDefinitionAsArgument() throws Exception {
200   
201  1 RenderingContext renderingContext = mock(RenderingContext.class);
202  1 ComponentsTestUtil.setInstance(RenderingContext.class, renderingContext);
203   
204  1 BlossomTemplateDefinition templateDefinition = createMock(BlossomTemplateDefinition.class);
205  1 when(renderingContext.getRenderableDefinition()).thenReturn(templateDefinition);
206   
207  1 BlossomWebArgumentResolver resolver = new BlossomWebArgumentResolver();
208  1 Method method = getClass().getMethod("methodWithBlossomTemplateDefinitionArgument", BlossomTemplateDefinition.class);
209   
210  1 assertSame(templateDefinition, resolver.resolveArgument(new MethodParameter(method, 0), null));
211    }
212   
 
213  0 toggle public void methodWithBlossomTemplateDefinitionArgument(BlossomTemplateDefinition templateDefinition) {
214    }
215   
 
216  4 toggle private void assertSameContent(Content lhs, Content rhs) {
217  4 assertEquals(lhs.getUUID(), rhs.getUUID());
218    }
219    }