1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http5;
18
19 import java.io.File;
20 import java.time.Duration;
21 import java.util.concurrent.TimeUnit;
22
23 import org.apache.commons.vfs2.AbstractProviderTestConfig;
24 import org.apache.commons.vfs2.FileNotFolderException;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileSystemManager;
28 import org.apache.commons.vfs2.FileSystemOptions;
29 import org.apache.commons.vfs2.ProviderTestSuite;
30 import org.apache.commons.vfs2.VFS;
31 import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
32 import org.apache.commons.vfs2.util.NHttpFileServer;
33 import org.junit.Assert;
34
35 import junit.framework.Test;
36
37
38
39
40
41 public class Http5ProviderTestCase extends AbstractProviderTestConfig {
42
43 private static NHttpFileServer Server;
44
45 private static int SocketPort;
46
47 private static final String TEST_URI = "test.http.uri";
48
49
50
51
52 private static String ConnectionUri;
53
54 private static String getSystemTestUriOverride() {
55 return System.getProperty(TEST_URI);
56 }
57
58
59
60
61
62
63 private static void setUpClass() throws Exception {
64 Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
65 SocketPort = Server.getPort();
66 ConnectionUri = "http5://localhost:" + SocketPort;
67 }
68
69
70
71
72
73
74
75 public static Test suite() throws Exception {
76 return new ProviderTestSuite(new Http5ProviderTestCase()) {
77
78
79
80 @Override
81 protected void addBaseTests() throws Exception {
82 super.addBaseTests();
83 addTests(Http5ProviderTestCase.class);
84 }
85
86 @Override
87 protected void setUp() throws Exception {
88 if (getSystemTestUriOverride() == null) {
89 setUpClass();
90 }
91 super.setUp();
92 }
93
94 @Override
95 protected void tearDown() throws Exception {
96 tearDownClass();
97 super.tearDown();
98 }
99 };
100 }
101
102
103
104
105
106 private static void tearDownClass() throws InterruptedException {
107 if (Server != null) {
108 Server.shutdown(5000, TimeUnit.SECONDS);
109 }
110 }
111
112 private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
113 Assert.assertNotNull(file.getChildren());
114 Assert.assertTrue(file.getChildren().length > 0);
115 }
116
117
118
119
120 @Override
121 public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
122 String uri = getSystemTestUriOverride();
123 if (uri == null) {
124 uri = ConnectionUri;
125 }
126 return manager.resolveFile(uri);
127 }
128
129
130 public void ignoreTestHttp405() throws FileSystemException {
131 final FileObject fileObject = VFS.getManager()
132 .resolveFile("http5://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
133 assert !fileObject.getContent().isEmpty();
134 }
135
136
137
138
139 @Override
140 public void prepare(final DefaultFileSystemManager manager) throws Exception {
141 if (!manager.hasProvider("http5")) {
142 manager.addProvider("http5", new Http5FileProvider());
143 }
144 }
145
146
147 @SuppressWarnings("deprecation")
148 public void testHttpTimeoutConfig() {
149 final FileSystemOptions opts = new FileSystemOptions();
150 final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
151
152
153 assertEquals(0, builder.getConnectionTimeout(opts));
154 assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
155 assertEquals(0, builder.getSoTimeout(opts));
156 assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
157 assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
158
159
160 builder.setConnectionTimeout(opts, 60000);
161 builder.setSoTimeout(opts, 60000);
162 builder.setUserAgent(opts, "foo/bar");
163
164
165 assertEquals(60000, builder.getConnectionTimeout(opts));
166 assertEquals(60000, builder.getSoTimeout(opts));
167 assertEquals("foo/bar", builder.getUserAgent(opts));
168
169
170 builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
171 builder.setSoTimeout(opts, Duration.ofMinutes(1));
172 builder.setUserAgent(opts, "foo/bar");
173
174
175 assertEquals(60000, builder.getConnectionTimeoutDuration(opts).toMillis());
176 assertEquals(60000, builder.getSoTimeoutDuration(opts).toMillis());
177 assertEquals("foo/bar", builder.getUserAgent(opts));
178 }
179
180 private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
181 VFS.getManager().getFilesCache().close();
182 final FileSystemOptions opts = new FileSystemOptions();
183 Http5FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
184 final FileObject file = VFS.getManager().resolveFile(uri, opts);
185 try {
186 checkReadTestsFolder(file);
187 } catch (final FileNotFolderException e) {
188
189 }
190 }
191
192 public void testResloveFolderSlashNoRedirectOff() throws FileSystemException {
193 testResloveFolderSlash(ConnectionUri + "/read-tests", false);
194 }
195
196 public void testResloveFolderSlashNoRedirectOn() throws FileSystemException {
197 testResloveFolderSlash(ConnectionUri + "/read-tests", true);
198 }
199
200 public void testResloveFolderSlashYesRedirectOff() throws FileSystemException {
201 testResloveFolderSlash(ConnectionUri + "/read-tests/", false);
202 }
203
204 public void testResloveFolderSlashYesRedirectOn() throws FileSystemException {
205 testResloveFolderSlash(ConnectionUri + "/read-tests/", true);
206 }
207 }