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 java.io.File;
20  import java.io.IOException;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.commons.vfs2.AbstractProviderTestConfig;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemManager;
26  import org.apache.commons.vfs2.ProviderTestSuite;
27  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
28  import org.apache.commons.vfs2.util.NHttpFileServer;
29  
30  import junit.framework.Test;
31  
32  /**
33   * Test cases for HTTP with the default provider.
34   *
35   */
36  public class UrlProviderHttpTestCase extends AbstractProviderTestConfig {
37      private static NHttpFileServer Server;
38  
39      private static int SocketPort;
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          SocketPort = Server.getPort();
60          ConnectionUri = "http://localhost:" + SocketPort;
61      }
62  
63      public static Test suite() throws Exception {
64          return new ProviderTestSuite(new UrlProviderHttpTestCase()) {
65              @Override
66              protected void setUp() throws Exception {
67                  if (getSystemTestUriOverride() == null) {
68                      setUpClass();
69                  }
70                  super.setUp();
71              }
72  
73              @Override
74              protected void tearDown() throws Exception {
75                  tearDownClass();
76                  super.tearDown();
77              }
78          };
79      }
80  
81      /**
82       * Stops the embedded Apache HTTP Server ().
83       * @throws InterruptedException
84       */
85      public static void tearDownClass() throws InterruptedException {
86          if (Server != null) {
87              Server.shutdown(5000, TimeUnit.SECONDS);
88          }
89      }
90  
91      public UrlProviderHttpTestCase() throws IOException {
92          // empty
93      }
94  
95      /**
96       * Returns the base folder for tests.
97       */
98      @Override
99      public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
100         String uri = getSystemTestUriOverride();
101         if (uri == null) {
102             uri = ConnectionUri;
103         }
104         return manager.resolveFile(uri);
105     }
106 
107     /**
108      * Prepares the file system manager.
109      */
110     @Override
111     public void prepare(final DefaultFileSystemManager manager) throws Exception {
112         manager.addProvider("http", new UrlFileProvider());
113     }
114 }