View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.http.test;
12  
13  import static org.junit.Assert.assertTrue;
14  import static org.junit.Assert.fail;
15  
16  import java.util.List;
17  
18  import javax.servlet.ServletException;
19  
20  import org.eclipse.jetty.servlet.ServletContextHandler;
21  import org.eclipse.jetty.servlet.ServletHolder;
22  import org.eclipse.jetty.util.MultiException;
23  import org.eclipse.jgit.http.server.GitServlet;
24  import org.eclipse.jgit.junit.http.AppServer;
25  import org.eclipse.jgit.junit.http.MockServletConfig;
26  import org.eclipse.jgit.junit.http.RecordingLogger;
27  import org.junit.After;
28  import org.junit.Test;
29  
30  public class GitServletInitTest {
31  	private AppServer server;
32  
33  	@After
34  	public void tearDown() throws Exception {
35  		if (server != null) {
36  			server.tearDown();
37  			server = null;
38  		}
39  	}
40  
41  	@Test
42  	public void testDefaultConstructor_NoBasePath() throws Exception {
43  		GitServlet s = new GitServlet();
44  		try {
45  			s.init(new MockServletConfig());
46  			fail("Init did not crash due to missing parameter");
47  		} catch (ServletException e) {
48  			assertTrue(e.getMessage().contains("base-path"));
49  		}
50  	}
51  
52  	@Test
53  	public void testDefaultConstructor_WithBasePath() throws Exception {
54  		MockServletConfig c = new MockServletConfig();
55  		c.setInitParameter("base-path", ".");
56  		c.setInitParameter("export-all", "false");
57  
58  		GitServlet s = new GitServlet();
59  		s.init(c);
60  		s.destroy();
61  	}
62  
63  	@Test
64  	public void testInitUnderContainer_NoBasePath() throws Exception {
65  		server = new AppServer();
66  
67  		ServletContextHandler app = server.addContext("/");
68  		ServletHolder s = app.addServlet(GitServlet.class, "/git");
69  		s.setInitOrder(1);
70  		s.getServletHandler().setStartWithUnavailable(false);
71  		// The tmp directory is symlinked on OS X
72  		s.setInitParameter("aliases", "true");
73  
74  		try {
75  			server.setUp();
76  		} catch (Exception e) {
77  			Throwable why = null;
78  			if (e instanceof MultiException) {
79  				MultiException multi = (MultiException) e;
80  				List<Throwable> reasons = multi.getThrowables();
81  				why = reasons.get(0);
82  				assertTrue("Expected ServletException",
83  						why instanceof ServletException);
84  			} else if (e instanceof ServletException)
85  				why = e;
86  
87  			if (why != null) {
88  				assertTrue("Wanted base-path",
89  						why.getMessage().contains("base-path"));
90  				return;
91  			}
92  		}
93  		fail("Expected ServletException complaining about unset base-path");
94  	}
95  
96  	@Test
97  	public void testInitUnderContainer_WithBasePath() throws Exception {
98  		server = new AppServer();
99  
100 		ServletContextHandler app = server.addContext("/");
101 		ServletHolder s = app.addServlet(GitServlet.class, "/git");
102 		s.setInitOrder(1);
103 		s.setInitParameter("base-path", ".");
104 		s.setInitParameter("export-all", "true");
105 		// The tmp directory is symlinked on OS X
106 		s.setInitParameter("aliases", "true");
107 
108 		server.setUp();
109 		assertTrue("no warnings", RecordingLogger.getWarnings().isEmpty());
110 	}
111 }