View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests.util;
20  
21  import java.net.URL;
22  import net.sourceforge.jwebunit.tests.JWebUnitAPITestCase;
23  import org.eclipse.jetty.http.MimeTypes;
24  import org.eclipse.jetty.security.HashLoginService;
25  import org.eclipse.jetty.server.Connector;
26  import org.eclipse.jetty.server.Handler;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.server.ServerConnector;
29  import org.eclipse.jetty.server.handler.DefaultHandler;
30  import org.eclipse.jetty.server.handler.HandlerCollection;
31  import org.eclipse.jetty.webapp.WebAppContext;
32  import org.junit.BeforeClass;
33  
34  import static org.junit.Assert.fail;
35  
36  /**
37   * Sets up and tears down the Jetty servlet engine before and after the tests in
38   * the <code>TestSuite</code> have run.
39   * 
40   * @author Eelco Hillenius
41   */
42  public class JettySetup {
43  	/**
44  	 * The Jetty server we are going to use as test server.
45  	 */
46  	private static Server jettyServer = null;
47  	
48  	private static boolean started = false;
49  
50  
51  	/**
52  	 * Starts the Jetty server using the <tt>jetty-test-config.xml</tt> file
53  	 * which is loaded using the classloader used to load Jetty.
54  	 * 
55  	 * @see junit.extensions.TestSetup#setUp()
56  	 */
57  	@BeforeClass
58  	public static void startup() throws Exception {
59  	    if (!started) {
60      		try {
61      			jettyServer = new Server();
62      			ServerConnector connector = new ServerConnector(jettyServer);
63      			connector.setPort(JWebUnitAPITestCase.JETTY_PORT);
64      			jettyServer.setConnectors(new Connector[] { connector });
65      
66      			WebAppContext wah = new WebAppContext();
67      			
68      			// Handle files encoded in UTF-8
69      			MimeTypes mimeTypes = new MimeTypes();
70      			mimeTypes.addMimeMapping("html_utf-8", "text/html; charset=UTF-8");
71                  mimeTypes.addMimeMapping("txt", "text/plain");
72                  mimeTypes.addMimeMapping("bin", "application/octet-stream");
73      			wah.setMimeTypes(mimeTypes);
74      			
75      			
76      			HandlerCollection handlers= new HandlerCollection();
77      			handlers.setHandlers(new Handler[]{wah, new DefaultHandler()});
78      
79      			jettyServer.setHandler(wah);
80      			HashLoginService myrealm = new HashLoginService("MyRealm");
81      			URL config = JettySetup.class.getResource("/jetty-users.properties");
82      			myrealm.setConfig(config.toString());
83      			jettyServer.addBean(myrealm);
84      			
85      			wah.setContextPath(JWebUnitAPITestCase.JETTY_URL);
86      
87      			URL url = JettySetup.class.getResource("/testcases/");
88      			wah.setWar(url.toString());
89      			
90      			jettyServer.start();
91      			
92      			started = true;
93      
94      		} catch (Exception e) {
95      			e.printStackTrace();
96      			fail("Could not start the Jetty server: " + e);
97      		}
98  	    }
99  	}
100 	
101 	protected static void shutdown() throws Exception {
102         try {
103             jettyServer.stop();
104             started = false;
105         } catch (InterruptedException e) {
106             e.printStackTrace();
107             fail("Jetty server was interrupted: " + e);
108         } catch (Exception e) {
109             e.printStackTrace();
110             fail("Could not stop the Jetty server: " + e);
111         }
112     }
113 }