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

File ObservedBeanFactoryBeanTest.java

 

Code metrics

0
50
3
1
163
94
3
0.06
16.67
3
1

Classes

Class Line # Actions
ObservedBeanFactoryBeanTest 64 50 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 3 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.context;
35   
36    import java.io.IOException;
37    import javax.jcr.Node;
38    import javax.jcr.RepositoryException;
39    import javax.jcr.observation.Event;
40   
41    import org.junit.Test;
42    import org.springframework.aop.support.AopUtils;
43    import org.springframework.context.support.ClassPathXmlApplicationContext;
44    import static org.junit.Assert.assertEquals;
45    import static org.junit.Assert.assertFalse;
46    import static org.junit.Assert.assertSame;
47    import static org.junit.Assert.assertTrue;
48   
49    import info.magnolia.module.blossom.content2bean.TestService;
50    import info.magnolia.module.blossom.content2bean.TestServiceImpl;
51    import info.magnolia.repository.RepositoryConstants;
52    import info.magnolia.test.MgnlTestCase;
53    import info.magnolia.test.mock.MockUtil;
54    import info.magnolia.test.mock.jcr.MockEvent;
55    import info.magnolia.test.mock.jcr.MockObservationManager;
56    import info.magnolia.test.mock.jcr.MockSession;
57    import info.magnolia.test.mock.jcr.SessionTestUtil;
58   
59    /**
60    * TestCase for ObservedBeanFactoryBean.
61    *
62    * @since 1.2
63    */
 
64    public class ObservedBeanFactoryBeanTest extends MgnlTestCase {
65   
 
66  1 toggle @Test
67    public void testInstantiationInitializationReloadingAndDestruction() throws RepositoryException, IOException, InterruptedException {
68   
69  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.CONFIG,
70    "/modules/testcase/beans/testService.class=info.magnolia.module.blossom.content2bean.TestServiceImpl\n" +
71    "/modules/testcase/beans/testService.shoeSize=long:41\n" +
72    "/modules/testcase/beans/testService.servicePort=string:9001\n" +
73    "/modules/testcase/beans/testService/innerBean.class=string:info.magnolia.module.blossom.content2bean.SimpleInnerBean\n" +
74    "/modules/testcase/beans/testService/innerBean.innerProperty=string:qwerty");
75   
76  1 MockUtil.setSystemContextSessionAndHierarchyManager(session);
77   
78  1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:info/magnolia/module/blossom/context/ObservedBeanFactoryBean-test.xml"});
79   
80  1 TestServiceImpl testService = (TestServiceImpl) context.getBean("testService");
81   
82  1 assertEquals("testService", testService.getBeanName());
83  1 assertEquals(41, testService.getShoeSize());
84  1 assertEquals(9001, testService.getServicePort());
85  1 assertTrue(testService.isInitialized());
86  1 assertTrue(testService.isPostConstructed());
87  1 assertSame(context, testService.getApplicationContext());
88   
89  1 assertTrue(AopUtils.isAopProxy(testService));
90  1 assertTrue(AopUtils.isCglibProxy(testService));
91  1 assertEquals(TestServiceImpl.class, AopUtils.getTargetClass(testService));
92   
93  1 assertFalse("testService".equals(testService.getInnerBean().getBeanName()));
94  1 assertEquals(testService.getInnerBean().getInnerProperty(), "qwerty");
95  1 assertTrue(testService.getInnerBean().isPostConstructed());
96   
97  1 Node content = session.getNode("/modules/testcase/beans/testService");
98  1 content.getProperty("shoeSize").setValue(39);
99   
100  1 MockEvent event = new MockEvent();
101  1 event.setType(Event.PROPERTY_CHANGED);
102  1 event.setPath("/modules/testcase/beans/testService@shoeSize");
103  1 event.setUserID("superuser");
104  1 ((MockObservationManager)session.getWorkspace().getObservationManager()).fireEvent(event);
105   
106  1 Thread.sleep(1200);
107   
108  1 assertEquals(39, testService.getShoeSize());
109   
110  1 testService = testService.getInstance()[0];
111   
112  1 assertFalse(testService.isDisposed());
113  1 assertFalse(testService.isPreDestroyed());
114  1 context.close();
115  1 assertTrue(testService.isDisposed());
116  1 assertTrue(testService.isPreDestroyed());
117    }
118   
 
119  1 toggle @Test
120    public void testDefaultClass() throws RepositoryException, IOException, InterruptedException {
121   
122  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.CONFIG,
123    "/modules/testcase/beans/testService.shoeSize=long:41\n" +
124    "/modules/testcase/beans/testService.servicePort=string:9001");
125   
126  1 MockUtil.setSystemContextSessionAndHierarchyManager(session);
127   
128  1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:info/magnolia/module/blossom/context/ObservedBeanFactoryBean-defaultClass-test.xml"});
129   
130  1 TestServiceImpl testService = (TestServiceImpl) context.getBean("testService");
131   
132  1 assertEquals("testService", testService.getBeanName());
133  1 assertEquals(41, testService.getShoeSize());
134  1 assertEquals(9001, testService.getServicePort());
135  1 assertTrue(testService.isInitialized());
136  1 assertTrue(testService.isPostConstructed());
137  1 assertSame(context, testService.getApplicationContext());
138  1 assertTrue(AopUtils.isAopProxy(testService));
139  1 assertEquals(TestServiceImpl.class, AopUtils.getTargetClass(testService));
140    }
141   
 
142  1 toggle @Test
143    public void testJdkProxy() throws RepositoryException, IOException, InterruptedException {
144   
145  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.CONFIG,
146    "/modules/testcase/beans/testService.class=info.magnolia.module.blossom.content2bean.TestServiceImpl\n" +
147    "/modules/testcase/beans/testService.shoeSize=long:41\n" +
148    "/modules/testcase/beans/testService.servicePort=string:9001\n" +
149    "/modules/testcase/beans/testService/innerBean.class=string:info.magnolia.module.blossom.content2bean.SimpleInnerBean\n" +
150    "/modules/testcase/beans/testService/innerBean.innerProperty=string:qwerty");
151   
152  1 MockUtil.setSystemContextSessionAndHierarchyManager(session);
153   
154  1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:info/magnolia/module/blossom/context/ObservedBeanFactoryBean-jdkProxies-test.xml"});
155   
156  1 TestService testService = (TestService) context.getBean("testService");
157   
158  1 assertTrue(AopUtils.isAopProxy(testService));
159  1 assertFalse(AopUtils.isCglibProxy(testService));
160  1 assertEquals(TestServiceImpl.class, AopUtils.getTargetClass(testService));
161   
162    }
163    }