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.sftp;
18  
19  import java.io.InputStream;
20  
21  import org.apache.commons.vfs2.Capability;
22  import org.apache.commons.vfs2.FileObject;
23  import org.apache.commons.vfs2.FileSystemException;
24  import org.apache.mina.core.session.IoSession;
25  import org.apache.sshd.common.FactoryManager;
26  import org.apache.sshd.common.session.AbstractSession;
27  import org.apache.sshd.server.session.ServerSession;
28  import org.apache.sshd.server.session.SessionFactory;
29  import org.junit.Test;
30  import org.junit.jupiter.api.Assertions;
31  
32  /**
33   * Test SftpFileObject.doGetInputStream return the channel to pool when throw an exception.
34   */
35  public class SftpPutChannelTestCase extends AbstractSftpProviderTestCase {
36  
37      /**
38       * Exposes the channels size.
39       */
40      private static class CustomServerSession extends ServerSession {
41          public CustomServerSession(final FactoryManager server, final IoSession ioSession) throws Exception {
42              super(server, ioSession);
43          }
44  
45          public int getChannelsCount() {
46              return channels.size();
47          }
48      }
49  
50      private static class CustomSessionFactory extends SessionFactory {
51          @Override
52          protected AbstractSession doCreateSession(final IoSession ioSession) throws Exception {
53              return new CustomServerSession(server, ioSession);
54          }
55      }
56  
57      private static final Integer MAX_CHANNELS = 10;
58  
59      /**
60       * Creates the test suite for the sftp file system.
61       */
62      public static junit.framework.Test suite() throws Exception {
63          return new SftpProviderTestSuite(new SftpPutChannelTestCase()) {
64              @Override
65              protected void addBaseTests() throws Exception {
66                  // Just tries to read
67                  addTests(SftpPutChannelTestCase.class);
68              }
69          };
70      }
71  
72      /**
73       * Gets the capabilities required by the tests of this test case.
74       */
75      @Override
76      protected Capability[] getRequiredCapabilities() {
77          return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
78                  Capability.WRITE_CONTENT };
79      }
80  
81      @Override
82      protected boolean isExecChannelClosed() {
83          return false;
84      }
85  
86      @Override
87      protected SessionFactory sessionFactory() {
88          return new CustomSessionFactory();
89      }
90  
91      /**
92       * Tests SftpFileObject.doGetInputStream return the channel to pool, when there is an exception.
93       */
94      @Test
95      public void testDoGetInputStream() throws Exception {
96          final FileObject readFolder = getReadFolder();
97          // try MAX_CHANNELS * 2 times, then check channels count less than MAX_CHANNELS
98          // ( actually must <= 2, but less than MAX_CHANNELS is enough
99          for (int i = 0; i < MAX_CHANNELS * 2; i++) {
100             try {
101                 try (InputStream ignored = readFolder.resolveFile("not-exists.txt").getContent().getInputStream()) {
102                     Assertions.fail("file should not be exists");
103                 }
104             } catch (final FileSystemException e) {
105                 final int channelsCount = ((CustomServerSession) server.getActiveSessions().get(0)).getChannelsCount();
106                 Assertions.assertTrue(channelsCount < MAX_CHANNELS, "channels count expected less than " + MAX_CHANNELS);
107             }
108         }
109     }
110 
111 }