1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.web.portlet.context;
18 
19 import javax.portlet.PortletConfig;
20 import javax.portlet.PortletContext;
21 
22 import junit.framework.TestCase;
23 
24 import org.springframework.mock.web.portlet.MockPortletConfig;
25 import org.springframework.mock.web.portlet.MockPortletContext;
26 
27 /**
28  * @author Mark Fisher
29  */
30 public class PortletContextAwareProcessorTests extends TestCase {
31 
testPortletContextAwareWithPortletContext()32 	public void testPortletContextAwareWithPortletContext() {
33 		PortletContext portletContext = new MockPortletContext();
34 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext);
35 		PortletContextAwareBean bean = new PortletContextAwareBean();
36 		assertNull(bean.getPortletContext());
37 		processor.postProcessBeforeInitialization(bean, "testBean");
38 		assertNotNull("PortletContext should have been set", bean.getPortletContext());
39 		assertEquals(portletContext, bean.getPortletContext());
40 	}
41 
testPortletContextAwareWithPortletConfig()42 	public void testPortletContextAwareWithPortletConfig() {
43 		PortletContext portletContext = new MockPortletContext();
44 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
45 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletConfig);
46 		PortletContextAwareBean bean = new PortletContextAwareBean();
47 		assertNull(bean.getPortletContext());
48 		processor.postProcessBeforeInitialization(bean, "testBean");
49 		assertNotNull("PortletContext should have been set", bean.getPortletContext());
50 		assertEquals(portletContext, bean.getPortletContext());
51 	}
52 
testPortletContextAwareWithPortletContextAndPortletConfig()53 	public void testPortletContextAwareWithPortletContextAndPortletConfig() {
54 		PortletContext portletContext = new MockPortletContext();
55 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
56 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext, portletConfig);
57 		PortletContextAwareBean bean = new PortletContextAwareBean();
58 		assertNull(bean.getPortletContext());
59 		processor.postProcessBeforeInitialization(bean, "testBean");
60 		assertNotNull("PortletContext should have been set", bean.getPortletContext());
61 		assertEquals(portletContext, bean.getPortletContext());
62 	}
63 
testPortletContextAwareWithNullPortletContextAndNonNullPortletConfig()64 	public void testPortletContextAwareWithNullPortletContextAndNonNullPortletConfig() {
65 		PortletContext portletContext = new MockPortletContext();
66 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
67 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(null, portletConfig);
68 		PortletContextAwareBean bean = new PortletContextAwareBean();
69 		assertNull(bean.getPortletContext());
70 		processor.postProcessBeforeInitialization(bean, "testBean");
71 		assertNotNull("PortletContext should have been set", bean.getPortletContext());
72 		assertEquals(portletContext, bean.getPortletContext());
73 	}
74 
testPortletContextAwareWithNonNullPortletContextAndNullPortletConfig()75 	public void testPortletContextAwareWithNonNullPortletContextAndNullPortletConfig() {
76 		PortletContext portletContext = new MockPortletContext();
77 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext, null);
78 		PortletContextAwareBean bean = new PortletContextAwareBean();
79 		assertNull(bean.getPortletContext());
80 		processor.postProcessBeforeInitialization(bean, "testBean");
81 		assertNotNull("PortletContext should have been set", bean.getPortletContext());
82 		assertEquals(portletContext, bean.getPortletContext());
83 	}
84 
testPortletContextAwareWithNullPortletContext()85 	public void testPortletContextAwareWithNullPortletContext() {
86 		PortletContext portletContext = null;
87 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext);
88 		PortletContextAwareBean bean = new PortletContextAwareBean();
89 		assertNull(bean.getPortletContext());
90 		processor.postProcessBeforeInitialization(bean, "testBean");
91 		assertNull(bean.getPortletContext());
92 	}
93 
testPortletConfigAwareWithPortletContextOnly()94 	public void testPortletConfigAwareWithPortletContextOnly() {
95 		PortletContext portletContext = new MockPortletContext();
96 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext);
97 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
98 		assertNull(bean.getPortletConfig());
99 		processor.postProcessBeforeInitialization(bean, "testBean");
100 		assertNull(bean.getPortletConfig());
101 	}
102 
testPortletConfigAwareWithPortletConfig()103 	public void testPortletConfigAwareWithPortletConfig() {
104 		PortletContext portletContext = new MockPortletContext();
105 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
106 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletConfig);
107 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
108 		assertNull(bean.getPortletConfig());
109 		processor.postProcessBeforeInitialization(bean, "testBean");
110 		assertNotNull("PortletConfig should have been set", bean.getPortletConfig());
111 		assertEquals(portletConfig, bean.getPortletConfig());
112 	}
113 
testPortletConfigAwareWithPortletContextAndPortletConfig()114 	public void testPortletConfigAwareWithPortletContextAndPortletConfig() {
115 		PortletContext portletContext = new MockPortletContext();
116 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
117 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext, portletConfig);
118 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
119 		assertNull(bean.getPortletConfig());
120 		processor.postProcessBeforeInitialization(bean, "testBean");
121 		assertNotNull("PortletConfig should have been set", bean.getPortletConfig());
122 		assertEquals(portletConfig, bean.getPortletConfig());
123 	}
124 
testPortletConfigAwareWithNullPortletContextAndNonNullPortletConfig()125 	public void testPortletConfigAwareWithNullPortletContextAndNonNullPortletConfig() {
126 		PortletContext portletContext = new MockPortletContext();
127 		PortletConfig portletConfig = new MockPortletConfig(portletContext);
128 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(null, portletConfig);
129 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
130 		assertNull(bean.getPortletConfig());
131 		processor.postProcessBeforeInitialization(bean, "testBean");
132 		assertNotNull("PortletConfig should have been set", bean.getPortletConfig());
133 		assertEquals(portletConfig, bean.getPortletConfig());
134 	}
135 
testPortletConfigAwareWithNonNullPortletContextAndNullPortletConfig()136 	public void testPortletConfigAwareWithNonNullPortletContextAndNullPortletConfig() {
137 		PortletContext portletContext = new MockPortletContext();
138 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext, null);
139 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
140 		assertNull(bean.getPortletConfig());
141 		processor.postProcessBeforeInitialization(bean, "testBean");
142 		assertNull(bean.getPortletConfig());
143 	}
144 
testPortletConfigAwareWithNullPortletContext()145 	public void testPortletConfigAwareWithNullPortletContext() {
146 		PortletContext portletContext = null;
147 		PortletContextAwareProcessor processor = new PortletContextAwareProcessor(portletContext);
148 		PortletConfigAwareBean bean = new PortletConfigAwareBean();
149 		assertNull(bean.getPortletConfig());
150 		processor.postProcessBeforeInitialization(bean, "testBean");
151 		assertNull(bean.getPortletConfig());
152 	}
153 
154 }
155