1 15 package org.apache.tapestry.portlet; 16 17 import java.net.URL ; 18 19 import javax.portlet.PortletConfig; 20 21 import org.apache.hivemind.Resource; 22 import org.apache.hivemind.test.HiveMindTestCase; 23 import org.apache.tapestry.parse.ISpecificationParser; 24 import org.apache.tapestry.services.ApplicationGlobals; 25 import org.apache.tapestry.services.impl.ApplicationGlobalsImpl; 26 import org.apache.tapestry.spec.IApplicationSpecification; 27 import org.apache.tapestry.web.WebContext; 28 import org.apache.tapestry.web.WebContextResource; 29 import org.easymock.MockControl; 30 31 37 public class TestPortletApplicationSpecificationInitializer extends HiveMindTestCase 38 { 39 private PortletConfig newConfig(String name) 40 { 41 MockControl control = newControl(PortletConfig.class); 42 PortletConfig config = (PortletConfig) control.getMock(); 43 44 config.getPortletName(); 45 control.setReturnValue(name); 46 47 return config; 48 } 49 50 private IApplicationSpecification newSpecification() 51 { 52 return (IApplicationSpecification) newMock(IApplicationSpecification.class); 53 } 54 55 private ISpecificationParser newParser(Resource input, IApplicationSpecification specification) 56 { 57 MockControl control = newControl(ISpecificationParser.class); 58 ISpecificationParser parser = (ISpecificationParser) control.getMock(); 59 60 parser.parseApplicationSpecification(input); 61 control.setReturnValue(specification); 62 63 return parser; 64 } 65 66 private ApplicationGlobals newGlobals() 67 { 68 return (ApplicationGlobals) newMock(ApplicationGlobals.class); 69 } 70 71 public void testFoundInSubdir() throws Exception 72 { 73 PortletConfig config = newConfig("myportlet"); 74 75 MockControl contextc = newControl(WebContext.class); 76 WebContext context = (WebContext) contextc.getMock(); 77 78 IApplicationSpecification specification = newSpecification(); 79 80 82 URL fakeURL = getClass().getResource("hivemodule.xml"); 83 84 context.getResource("/WEB-INF/myportlet/myportlet.application"); 85 contextc.setReturnValue(fakeURL); 86 87 Resource expectedResource = new WebContextResource(context, 88 "/WEB-INF/myportlet/myportlet.application"); 89 90 ISpecificationParser parser = newParser(expectedResource, specification); 91 92 ApplicationGlobals globals = newGlobals(); 93 94 globals.storeSpecification(specification); 95 96 replayControls(); 97 98 PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer(); 99 init.setContext(context); 100 init.setGlobals(globals); 101 init.setParser(parser); 102 103 init.initialize(config); 104 105 verifyControls(); 106 } 107 108 public void testFoundInRootDir() throws Exception 109 { 110 PortletConfig config = newConfig("myportlet"); 111 112 MockControl contextc = newControl(WebContext.class); 113 WebContext context = (WebContext) contextc.getMock(); 114 115 IApplicationSpecification specification = newSpecification(); 116 117 119 URL fakeURL = getClass().getResource("hivemodule.xml"); 120 121 context.getResource("/WEB-INF/myportlet/myportlet.application"); 122 contextc.setReturnValue(null); 123 124 context.getResource("/WEB-INF/myportlet.application"); 125 contextc.setReturnValue(fakeURL); 126 127 Resource expectedResource = new WebContextResource(context, 128 "/WEB-INF/myportlet.application"); 129 130 ISpecificationParser parser = newParser(expectedResource, specification); 131 132 ApplicationGlobals globals = newGlobals(); 133 134 globals.storeSpecification(specification); 135 136 replayControls(); 137 138 PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer(); 139 init.setContext(context); 140 init.setGlobals(globals); 141 init.setParser(parser); 142 143 init.initialize(config); 144 145 verifyControls(); 146 } 147 148 public void testNotFound() throws Exception 149 { 150 PortletConfig config = newConfig("myportlet"); 151 152 MockControl contextc = newControl(WebContext.class); 153 WebContext context = (WebContext) contextc.getMock(); 154 155 context.getResource("/WEB-INF/myportlet/myportlet.application"); 156 contextc.setReturnValue(null); 157 158 context.getResource("/WEB-INF/myportlet.application"); 159 contextc.setReturnValue(null); 160 161 replayControls(); 162 163 ApplicationGlobals globals = new ApplicationGlobalsImpl(); 164 165 PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer(); 166 init.setContext(context); 167 init.setGlobals(globals); 168 169 init.initialize(config); 170 171 verifyControls(); 172 173 IApplicationSpecification spec = globals.getSpecification(); 174 175 assertEquals("myportlet", spec.getName()); 176 assertEquals(new WebContextResource(context, "/WEB-INF/myportlet.application"), spec 177 .getSpecificationLocation()); 178 } 179 } | Popular Tags |