View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.url;
18  
19  import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.concurrent.TimeUnit;
24  
25  import junit.framework.Test;
26  
27  import org.apache.commons.vfs2.AbstractProviderTestConfig;
28  import org.apache.commons.vfs2.FileObject;
29  import org.apache.commons.vfs2.FileSystemManager;
30  import org.apache.commons.vfs2.ProviderTestSuite;
31  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
32  import org.apache.commons.vfs2.util.NHttpFileServer;
33  
34  /**
35   * Test cases for HTTP with the default provider.
36   */
37  public class UrlProviderHttpTestCase extends AbstractProviderTestConfig {
38  
39      private static NHttpFileServer server;
40  
41      private static final String TEST_URI = "test.http.uri";
42  
43      /**
44       * Use %40 for @ in URLs
45       */
46      private static String connectionUri;
47  
48      private static String getSystemTestUriOverride() {
49          return System.getProperty(TEST_URI);
50      }
51  
52      /**
53       * Creates and starts an embedded Apache HTTP Server ().
54       *
55       * @throws Exception
56       */
57      private static void setUpClass() throws Exception {
58          server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
59          connectionUri = getLocalHostUriString("http", server.getPort());
60      }
61  
62      public static Test suite() throws Exception {
63          return new ProviderTestSuite(new UrlProviderHttpTestCase()) {
64              @Override
65              protected void setUp() throws Exception {
66                  if (getSystemTestUriOverride() == null) {
67                      setUpClass();
68                  }
69                  super.setUp();
70              }
71  
72              @Override
73              protected void tearDown() throws Exception {
74                  tearDownClass();
75                  super.tearDown();
76              }
77          };
78      }
79  
80      /**
81       * Stops the embedded Apache HTTP Server ().
82       * @throws InterruptedException
83       */
84      public static void tearDownClass() throws InterruptedException {
85          if (server != null) {
86              server.shutdown(5000, TimeUnit.SECONDS);
87          }
88      }
89  
90      public UrlProviderHttpTestCase() throws IOException {
91          // empty
92      }
93  
94      /**
95       * Returns the base folder for tests.
96       */
97      @Override
98      public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
99          String uri = getSystemTestUriOverride();
100         if (uri == null) {
101             uri = connectionUri;
102         }
103         return manager.resolveFile(uri);
104     }
105 
106     /**
107      * Prepares the file system manager.
108      */
109     @Override
110     public void prepare(final DefaultFileSystemManager manager) throws Exception {
111         manager.addProvider("http", new UrlFileProvider());
112     }
113 
114 }