001: /**
002: *
003: * Copyright 2005-2006 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.mortbay.jetty.xbean;
017:
018: import java.net.URL;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
026: import org.mortbay.component.LifeCycle;
027: import org.mortbay.jetty.Handler;
028: import org.mortbay.jetty.Server;
029: import org.mortbay.jetty.deployer.ContextDeployer;
030: import org.mortbay.jetty.handler.ContextHandlerCollection;
031: import org.mortbay.jetty.handler.HandlerCollection;
032: import org.mortbay.jetty.webapp.WebAppContext;
033: import org.springframework.context.support.AbstractApplicationContext;
034: import org.springframework.core.io.UrlResource;
035:
036: public class XBeanTest extends TestCase {
037:
038: protected AbstractApplicationContext context;
039:
040: protected URL url;
041: protected Server server;
042:
043: public void setUp() throws Exception {
044: url = getClass().getClassLoader().getResource(
045: "org/mortbay/jetty/xbean/xbean.xml");
046: assertNotNull("Could not find xbean.xml on the classpath!", url);
047:
048: context = new ResourceXmlApplicationContext(
049: new UrlResource(url));
050: }
051:
052: public void testUsingXBeanXmlConfig() throws Exception {
053: System.setProperty("DEBUG", "false");
054:
055: String[] names = context.getBeanNamesForType(Server.class);
056: assertEquals("Should have the name of a Jetty server", 1,
057: names.length);
058: server = (Server) context.getBean(names[0]);
059: assertNotNull("Should have a Jetty Server", server);
060:
061: HandlerCollection hcollection = (HandlerCollection) server
062: .getChildHandlerByClass(HandlerCollection.class);
063: assertNotNull("Should have a HandlerCollection", hcollection);
064: assertNotNull("HandlerCollection should contain handlers",
065: hcollection.getHandlers());
066: Handler[] handlers = hcollection.getHandlers();
067: assertEquals("Should be 3 handlers", 3, handlers.length);
068: assertTrue(
069: "First handler should be a ContextHandlerCollection",
070: handlers[0] instanceof ContextHandlerCollection);
071: Handler[] webapps = ((ContextHandlerCollection) handlers[0])
072: .getChildHandlers();
073: assertNotNull("Should be at least one webapp", webapps);
074: assertTrue("Should be an instance of WebAppContext",
075: webapps[0] instanceof WebAppContext);
076: }
077:
078: public void testHotDeployer() throws Exception {
079: System.setProperty("DEBUG", "false");
080:
081: String[] names = context.getBeanNamesForType(Server.class);
082: assertEquals("Should have the name of a Jetty server", 1,
083: names.length);
084: server = (Server) context.getBean(names[0]);
085: assertNotNull("Should have a Jetty Server", server);
086:
087: Collection deployers = ((JettyFactoryBean) server)
088: .getDeployers();
089: assertTrue("Should be a deployer", !deployers.isEmpty());
090: ContextDeployer deployer = null;
091:
092: for (Iterator iter = deployers.iterator(); iter.hasNext();) {
093: deployer = (ContextDeployer) iter.next();
094: }
095:
096: assertNotNull("Should be a ContextDeployer", deployer);
097:
098: // Cannot get the following to work properly because the MacOS X java.io.tmpdir
099: // resolves to /private/tmp instead of /tmp
100: // assertEquals("", deployer.getConfigurationDir(), System.getProperty("java.io.tmpdir") + "/deploy");
101: }
102:
103: protected void tearDown() throws Exception {
104: if (context != null) {
105: context.destroy();
106: }
107:
108: server.stop();
109: }
110:
111: }
|