You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitServletInitTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package org.eclipse.jgit.http.test;
  11. import static org.junit.Assert.assertTrue;
  12. import static org.junit.Assert.fail;
  13. import java.util.List;
  14. import javax.servlet.ServletException;
  15. import org.eclipse.jetty.servlet.ServletContextHandler;
  16. import org.eclipse.jetty.servlet.ServletHolder;
  17. import org.eclipse.jetty.util.MultiException;
  18. import org.eclipse.jgit.http.server.GitServlet;
  19. import org.eclipse.jgit.junit.http.AppServer;
  20. import org.eclipse.jgit.junit.http.MockServletConfig;
  21. import org.eclipse.jgit.junit.http.RecordingLogger;
  22. import org.junit.After;
  23. import org.junit.Test;
  24. public class GitServletInitTest {
  25. private AppServer server;
  26. @After
  27. public void tearDown() throws Exception {
  28. if (server != null) {
  29. server.tearDown();
  30. server = null;
  31. }
  32. }
  33. @Test
  34. public void testDefaultConstructor_NoBasePath() throws Exception {
  35. GitServlet s = new GitServlet();
  36. try {
  37. s.init(new MockServletConfig());
  38. fail("Init did not crash due to missing parameter");
  39. } catch (ServletException e) {
  40. assertTrue(e.getMessage().contains("base-path"));
  41. }
  42. }
  43. @Test
  44. public void testDefaultConstructor_WithBasePath() throws Exception {
  45. MockServletConfig c = new MockServletConfig();
  46. c.setInitParameter("base-path", ".");
  47. c.setInitParameter("export-all", "false");
  48. GitServlet s = new GitServlet();
  49. s.init(c);
  50. s.destroy();
  51. }
  52. @Test
  53. public void testInitUnderContainer_NoBasePath() throws Exception {
  54. server = new AppServer();
  55. ServletContextHandler app = server.addContext("/");
  56. ServletHolder s = app.addServlet(GitServlet.class, "/git");
  57. s.setInitOrder(1);
  58. s.getServletHandler().setStartWithUnavailable(false);
  59. // The tmp directory is symlinked on OS X
  60. s.setInitParameter("aliases", "true");
  61. try {
  62. server.setUp();
  63. } catch (Exception e) {
  64. Throwable why = null;
  65. if (e instanceof MultiException) {
  66. MultiException multi = (MultiException) e;
  67. List<Throwable> reasons = multi.getThrowables();
  68. why = reasons.get(0);
  69. assertTrue("Expected ServletException",
  70. why instanceof ServletException);
  71. } else if (e instanceof ServletException)
  72. why = e;
  73. if (why != null) {
  74. assertTrue("Wanted base-path",
  75. why.getMessage().contains("base-path"));
  76. return;
  77. }
  78. }
  79. fail("Expected ServletException complaining about unset base-path");
  80. }
  81. @Test
  82. public void testInitUnderContainer_WithBasePath() throws Exception {
  83. server = new AppServer();
  84. ServletContextHandler app = server.addContext("/");
  85. ServletHolder s = app.addServlet(GitServlet.class, "/git");
  86. s.setInitOrder(1);
  87. s.setInitParameter("base-path", ".");
  88. s.setInitParameter("export-all", "true");
  89. // The tmp directory is symlinked on OS X
  90. s.setInitParameter("aliases", "true");
  91. server.setUp();
  92. assertTrue("no warnings", RecordingLogger.getWarnings().isEmpty());
  93. }
  94. }