View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.apache;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.nio.charset.StandardCharsets;
24  
25  import org.apache.http.pool.ConnPoolControl;
26  import org.apache.http.pool.PoolStats;
27  import org.eclipse.aether.ConfigurationProperties;
28  import org.eclipse.aether.DefaultRepositoryCache;
29  import org.eclipse.aether.internal.test.util.TestFileUtils;
30  import org.eclipse.aether.internal.test.util.http.HttpTransporterTest;
31  import org.eclipse.aether.internal.test.util.http.RecordingTransportListener;
32  import org.eclipse.aether.spi.connector.transport.GetTask;
33  import org.eclipse.aether.spi.connector.transport.PutTask;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.*;
37  
38  /**
39   * Apache Transporter UT.
40   * It does support WebDAV.
41   */
42  class ApacheTransporterTest extends HttpTransporterTest {
43  
44      public ApacheTransporterTest() {
45          super(ApacheTransporterFactory::new);
46      }
47  
48      @Test
49      void testGet_WebDav() throws Exception {
50          httpServer.setWebDav(true);
51          RecordingTransportListener listener = new RecordingTransportListener();
52          GetTask task = new GetTask(URI.create("repo/dir/file.txt")).setListener(listener);
53          ((ApacheTransporter) transporter).getState().setWebDav(true);
54          transporter.get(task);
55          assertEquals("test", task.getDataString());
56          assertEquals(0L, listener.getDataOffset());
57          assertEquals(4L, listener.getDataLength());
58          assertEquals(1, listener.getStartedCount());
59          assertTrue(listener.getProgressedCount() > 0, "Count: " + listener.getProgressedCount());
60          assertEquals(task.getDataString(), new String(listener.getBaos().toByteArray(), StandardCharsets.UTF_8));
61          assertEquals(
62                  1, httpServer.getLogEntries().size(), httpServer.getLogEntries().toString());
63      }
64  
65      @Test
66      void testPut_WebDav() throws Exception {
67          httpServer.setWebDav(true);
68          session.setConfigProperty(ConfigurationProperties.HTTP_SUPPORT_WEBDAV, true);
69          newTransporter(httpServer.getHttpUrl());
70  
71          RecordingTransportListener listener = new RecordingTransportListener();
72          PutTask task = new PutTask(URI.create("repo/dir1/dir2/file.txt"))
73                  .setListener(listener)
74                  .setDataString("upload");
75          transporter.put(task);
76          assertEquals(0L, listener.getDataOffset());
77          assertEquals(6L, listener.getDataLength());
78          assertEquals(1, listener.getStartedCount());
79          assertTrue(listener.getProgressedCount() > 0, "Count: " + listener.getProgressedCount());
80          assertEquals("upload", TestFileUtils.readString(new File(repoDir, "dir1/dir2/file.txt")));
81  
82          assertEquals(5, httpServer.getLogEntries().size());
83          assertEquals("OPTIONS", httpServer.getLogEntries().get(0).getMethod());
84          assertEquals("MKCOL", httpServer.getLogEntries().get(1).getMethod());
85          assertEquals("/repo/dir1/dir2/", httpServer.getLogEntries().get(1).getPath());
86          assertEquals("MKCOL", httpServer.getLogEntries().get(2).getMethod());
87          assertEquals("/repo/dir1/", httpServer.getLogEntries().get(2).getPath());
88          assertEquals("MKCOL", httpServer.getLogEntries().get(3).getMethod());
89          assertEquals("/repo/dir1/dir2/", httpServer.getLogEntries().get(3).getPath());
90          assertEquals("PUT", httpServer.getLogEntries().get(4).getMethod());
91      }
92  
93      @Test
94      void testConnectionReuse() throws Exception {
95          httpServer.addSslConnector();
96          session.setCache(new DefaultRepositoryCache());
97          for (int i = 0; i < 3; i++) {
98              newTransporter(httpServer.getHttpsUrl());
99              GetTask task = new GetTask(URI.create("repo/file.txt"));
100             transporter.get(task);
101             assertEquals("test", task.getDataString());
102         }
103         PoolStats stats = ((ConnPoolControl<?>)
104                         ((ApacheTransporter) transporter).getState().getConnectionManager())
105                 .getTotalStats();
106         assertEquals(1, stats.getAvailable(), stats.toString());
107     }
108 
109     @Test
110     void testConnectionNoReuse() throws Exception {
111         httpServer.addSslConnector();
112         session.setCache(new DefaultRepositoryCache());
113         session.setConfigProperty(ConfigurationProperties.HTTP_REUSE_CONNECTIONS, false);
114         for (int i = 0; i < 3; i++) {
115             newTransporter(httpServer.getHttpsUrl());
116             GetTask task = new GetTask(URI.create("repo/file.txt"));
117             transporter.get(task);
118             assertEquals("test", task.getDataString());
119         }
120         PoolStats stats = ((ConnPoolControl<?>)
121                         ((ApacheTransporter) transporter).getState().getConnectionManager())
122                 .getTotalStats();
123         assertEquals(0, stats.getAvailable(), stats.toString());
124     }
125 }