1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.urimapping

File BlossomVirtualURIMapperTest.java

 

Code metrics

4
38
8
2
146
90
10
0.26
4.75
4
1.25

Classes

Class Line # Actions
BlossomVirtualURIMapperTest 62 31 0% 6 0
1.0100%
BlossomVirtualURIMapperTest.IncorrectURIMapping 65 7 0% 4 4
0.692307769.2%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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.urimapping;
35   
36    import org.junit.After;
37    import org.junit.Before;
38    import org.junit.Test;
39    import org.springframework.aop.support.AopUtils;
40    import org.springframework.beans.BeansException;
41    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
42    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
43    import org.springframework.context.support.ClassPathXmlApplicationContext;
44    import org.springframework.mock.web.MockHttpServletRequest;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertNull;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.Mockito.mock;
50   
51    import info.magnolia.cms.beans.config.VirtualURIMapping;
52    import info.magnolia.context.MgnlContext;
53    import info.magnolia.module.blossom.annotation.VirtualURIMapper;
54    import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
55    import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAwareBeanPostProcessor;
56    import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent;
57    import info.magnolia.test.mock.MockWebContext;
58   
59    /**
60    * @since 1.1.1
61    */
 
62    public class BlossomVirtualURIMapperTest {
63   
64    @VirtualURIMapper
 
65    public static class IncorrectURIMapping implements VirtualURIMapping {
66   
 
67  8 toggle @Override
68    public MappingResult mapURI(String uri) {
69  8 if (uri.equals("/original-6")) {
70  1 MappingResult mappingResult = new MappingResult();
71  1 mappingResult.setToURI("/virtual-6");
72  1 mappingResult.setLevel(14);
73  1 return mappingResult;
74    } else {
75  7 return null;
76    }
77    }
78   
 
79  0 toggle public String mapper7(String from) {
80  0 return from.equals("/original-7") ? "/virtual-7" : null;
81    }
82    }
83   
84    private BlossomVirtualURIMapping mapping = new BlossomVirtualURIMapping();
85   
 
86  2 toggle @Before
87    public void setUp() {
88  2 MockHttpServletRequest request = new MockHttpServletRequest();
89  2 MockWebContext webContext = new MockWebContext();
90  2 webContext.setRequest(request);
91  2 MgnlContext.setInstance(webContext);
92    }
93   
 
94  2 toggle @After
95    public void tearDown() {
96  2 MgnlContext.setInstance(null);
97  2 mapping.clear();
98    }
99   
 
100  1 toggle @Test
101    public void testBeanDetection() {
102   
103  1 assertNull(mapping.mapURI("/original-1"));
104  1 assertNull(mapping.mapURI("/original-2"));
105   
106  1 ClassPathXmlApplicationContext context = createContextWithDispatcher("classpath:info/magnolia/module/blossom/urimapping/BlossomVirtualURIMapper-test.xml");
107   
108  1 assertNull(mapping.mapURI("/does-not-match-a-virtual-uri-mapping"));
109  1 assertEquals("/virtual-1", mapping.mapURI("/original-1").getToURI());
110  1 assertEquals("/virtual-2", mapping.mapURI("/original-2").getToURI());
111  1 assertEquals("/virtual-3", mapping.mapURI("/original-3").getToURI());
112  1 assertNull(mapping.mapURI("/original-4"));
113  1 assertNull(mapping.mapURI("/original-5"));
114  1 assertEquals("/virtual-6", mapping.mapURI("/original-6").getToURI());
115  1 assertNull(mapping.mapURI("/original-7"));
116    }
117   
 
118  1 toggle @Test
119    public void testBeanDetectionWithProxies() {
120   
121  1 assertNull(mapping.mapURI("/original-2"));
122  1 assertNull(mapping.mapURI("/original-3"));
123   
124  1 ClassPathXmlApplicationContext context = createContextWithDispatcher("classpath:info/magnolia/module/blossom/urimapping/BlossomVirtualURIMapper-proxy-test.xml");
125   
126  1 assertTrue(AopUtils.isCglibProxy(context.getBean("proxiedMapping")));
127   
128  1 assertNull(mapping.mapURI("/does-not-match-a-virtual-uri-mapping"));
129  1 assertEquals("/virtual-2", mapping.mapURI("/original-2").getToURI());
130  1 assertEquals("/virtual-3", mapping.mapURI("/original-3").getToURI());
131    }
132   
 
133  2 toggle private ClassPathXmlApplicationContext createContextWithDispatcher(String configLocation) {
134  2 final BlossomDispatcher dispatcher = mock(BlossomDispatcher.class);
135  2 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {configLocation}, false);
136  2 context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
 
137  2 toggle @Override
138    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
139  2 beanFactory.addBeanPostProcessor(new BlossomDispatcherAwareBeanPostProcessor(dispatcher));
140    }
141    });
142  2 context.refresh();
143  2 context.publishEvent(new BlossomDispatcherInitializedEvent(dispatcher));
144  2 return context;
145    }
146    }