INT-1614 fixed FtpSendingMessageHandlerTest, removed unused test xml files, created SessionFactoryTests with Ignored test untill the issue with validating client modes in ASF is fixed

This commit is contained in:
Oleg Zhurakousky
2010-11-21 09:27:15 -05:00
parent 06f4bfda06
commit 1313572878
4 changed files with 171 additions and 119 deletions

View File

@@ -1,34 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ftp="http://www.springframework.org/schema/integration/ftp"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <context:property-placeholder-->
<!-- location="file://${user.home}/Desktop/ftp.properties"-->
<!-- ignore-unresolvable="true"/>-->
<ftp:inbound-channel-adapter remote-directory="${ftp.remotedir}" channel="ftpIn" auto-create-directories="true"
host="${ftp.host}" auto-delete-remote-files-on-sync="false"
username="${ftp.username}" password="${ftp.password}" port="21"
file-type="binary-file-type"
filename-pattern=".*?jpg"
>
<int:poller>
<int:interval-trigger interval="1000" time-unit="MILLISECONDS"/>
</int:poller>
</ftp:inbound-channel-adapter>
<int:channel id="ftpIn"/>
<bean id="inboundFTPFileServiceActivator"
class="org.springframework.integration.ftp.InboundFtpFileServiceActivator"/>
<int:service-activator input-channel="ftpIn" ref="inboundFTPFileServiceActivator"/>
</beans>

View File

@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ftps="http://www.springframework.org/schema/integration/ftps"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ftps http://www.springframework.org/schema/integration/ftp/spring-integration-ftps.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder
location="file://${user.home}/Desktop/ftp.properties"
ignore-unresolvable="true"/>
<ftps:inbound-channel-adapter remote-directory="${ftp.remotedir}"
channel="ftpIn"
auto-create-directories="true"
host="${ftp.host}"
auto-delete-remote-files-on-sync="false"
username="${ftp.username}"
password="${ftp.password}"
port="21"
file-type="binary-file-type"
filename-pattern=".*?java"
client-mode="passive-local-data-connection-mode"
>
<int:poller>
<int:interval-trigger interval="1000" time-unit="MILLISECONDS"/>
</int:poller>
</ftps:inbound-channel-adapter>
<int:channel id="ftpIn"/>
<bean id="inboundFtpsFileServiceActivator"
class="org.springframework.integration.ftp.InboundFtpsFileServiceActivator"/>
<int:service-activator input-channel="ftpIn" ref="inboundFtpsFileServiceActivator"/>
</beans>

View File

@@ -16,57 +16,130 @@
package org.springframework.integration.ftp.outbound;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.Message;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.ftp.session.AbstractFtpSessionFactory;
import org.springframework.integration.message.GenericMessage;
import org.springframework.util.FileCopyUtils;
/**
* @author Oleg Zhurakousky
*/
public class FtpSendingMessageHandlerTest {
private static FTPClient ftpClient;
private TestFtpSessionFactory sessionFactory;
@Before
public void prepare(){
ftpClient = mock(FTPClient.class);
sessionFactory = new TestFtpSessionFactory();
sessionFactory.setUsername("kermit");
sessionFactory.setPassword("frog");
sessionFactory.setHost("foo.com");
sessionFactory.setRemoteWorkingDirectory("remote-test-dir");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFileNameMessage() throws Exception {
// FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
// //FtpClientPool clientPoll = mock(FtpClientPool.class);
// FtpSession session = mock(FtpSession.class);
// when(session.put(Mockito.any(InputStream.class), Mockito.anyString())).th
// //when(session.put(Mockito.any(InputStream.class), Mockito.anyString())).thenReturn(true);
// when(clientPoll.getClient()).thenReturn(client);
//
// handler.setFtpClientPool(clientPoll);
// handler.handleMessage(new GenericMessage("hello"));
// verify(clientPoll, times(1)).getClient();
// verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class));
public void testHandleFileContentMessage() throws Exception {
File file = new File("remote-target-dir/handlerContent.test");
if (file.exists()){
file.delete();
}
assertFalse(file.exists());
FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
handler.setSessionFactory(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
handler.setFileNameGenerator(new FileNameGenerator() {
public String generateFileName(Message<?> message) {
return "handlerContent.test";
}
});
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage("hello"));
assertTrue(file.exists());
}
@Test
public void testHandleFileAsByte() throws Exception {
File file = new File("remote-target-dir/handlerContent.test");
if (file.exists()){
file.delete();
}
assertFalse(file.exists());
FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
handler.setSessionFactory(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
handler.setFileNameGenerator(new FileNameGenerator() {
public String generateFileName(Message<?> message) {
return "handlerContent.test";
}
});
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage<byte[]>("hello".getBytes()));
assertTrue(file.exists());
}
@Test
public void testHandleFileMessage() throws Exception {
File file = new File("remote-target-dir/template.mf.test");
if (file.exists()){
file.delete();
}
assertFalse(file.exists());
FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
handler.setSessionFactory(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
handler.setFileNameGenerator(new FileNameGenerator() {
public String generateFileName(Message<?> message) {
return ((File)message.getPayload()).getName() + ".test";
}
});
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage<File>(new File("template.mf")));
assertTrue(file.exists());
}
public static class TestFtpSessionFactory extends AbstractFtpSessionFactory<FTPClient> {
@Override
protected FTPClient createSingleInstanceOfClient() {
try {
when(ftpClient.getReplyCode()).thenReturn(250);
when(ftpClient.login("kermit", "frog")).thenReturn(true);
when(ftpClient.changeWorkingDirectory(Mockito.anyString())).thenReturn(true);
when(ftpClient.printWorkingDirectory()).thenReturn("remote-target-dir");
when(ftpClient.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenAnswer(new Answer<Boolean>() {
public Boolean answer(InvocationOnMock invocation)
throws Throwable {
String fileName = (String) invocation.getArguments()[0];
InputStream fis = (InputStream) invocation.getArguments()[1];
String workingDirectory = ftpClient.printWorkingDirectory();
FileCopyUtils.copy(fis, new FileOutputStream(workingDirectory + File.separator + fileName));
return true;
}
});
return ftpClient;
} catch (Exception e) {
throw new RuntimeException("Failed to create mock client", e);
}
}
}
// @SuppressWarnings({ "unchecked", "rawtypes" })
// @Test
// public void testHandleFileAsByte() throws Exception {
// FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
// FtpClientPool clientPoll = mock(FtpClientPool.class);
// FTPClient client = mock(FTPClient.class);
// when(client.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(true);
// when(clientPoll.getClient()).thenReturn(client);
//
// handler.setFtpClientPool(clientPoll);
// handler.handleMessage(new GenericMessage("hello".getBytes()));
// verify(clientPoll, times(1)).getClient();
// verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class));
// }
//
// @SuppressWarnings({ "unchecked", "rawtypes" })
// @Test
// public void testHandleFileMessage() throws Exception {
// FtpSendingMessageHandler handler = new FtpSendingMessageHandler();
// FtpClientPool clientPoll = mock(FtpClientPool.class);
// FTPClient client = mock(FTPClient.class);
// when(client.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(true);
// when(clientPoll.getClient()).thenReturn(client);
//
// handler.setFtpClientPool(clientPoll);
//
// File file = File.createTempFile("foo", ".txt");
// handler.handleMessage(new GenericMessage(file));
// verify(clientPoll, times(1)).getClient();
// verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class));
// }
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.ftp.session;
import static junit.framework.Assert.fail;
import java.lang.reflect.Field;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Ignore;
import org.junit.Test;
/**
* @author Oleg Zhurakousky
*
*/
public class SessionFactoryTests {
@Test
@Ignore // until we fix the issue with clientMode validation in ASF
public void testClientModes() throws Exception{
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory();
Field[] fields = FTPClient.class.getDeclaredFields();
for (Field field : fields) {
if (field.getName().endsWith("MODE")){
try {
int clientMode = field.getInt(null);
sessionFactory.setClientMode(clientMode);
if (!(clientMode == FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE ||
clientMode == FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE)){
fail();
}
} catch (IllegalArgumentException e) {
// success
} catch (Throwable e) {
fail();
}
}
}
}
}