Migrate SMB extension project to respective module
* Removed deprecated replaceFile and useTempFile session configs * Refactored to use AssertJ instead of JUnit asserts as per PR feedback * Code clean up for SMB module
This commit is contained in:
committed by
Artem Bilan
parent
392455eb34
commit
7ad71d38d9
@@ -0,0 +1,276 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Assorted test utils for the library.
|
||||
*
|
||||
* @author Markus Spann
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
public abstract class AbstractBaseTests {
|
||||
|
||||
/** Instance logger. */
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
protected final Log getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
@Rule
|
||||
public final TestName testMethodName = new TestName();
|
||||
|
||||
private String getTestMethodName() {
|
||||
return getClass().getSimpleName() + '.' + testMethodName.getMethodName() + "()";
|
||||
}
|
||||
|
||||
@Before
|
||||
public final void logTestBegin() {
|
||||
getLogger().info("BGN - Test " + getTestMethodName());
|
||||
}
|
||||
|
||||
@After
|
||||
public final void logTestEnd() {
|
||||
getLogger().info("END - Test " + getTestMethodName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the Spring application context XML file name from simple class name and suffix '-context.xml'.
|
||||
* @param _suffix optional suffix
|
||||
* @return application context XML file name
|
||||
*/
|
||||
protected final String getApplicationContextXmlFile(String _suffix) {
|
||||
String fn = getClass().getSimpleName();
|
||||
if (StringUtils.hasText(_suffix)) {
|
||||
fn += _suffix;
|
||||
}
|
||||
fn += "-context.xml";
|
||||
getLogger().debug("Returning application context xml file [" + fn + "] for class [" + getClass().getName() + "].");
|
||||
return fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the Spring application context XML file name from simple class name.
|
||||
* @return application context XML file name
|
||||
*/
|
||||
protected final String getApplicationContextXmlFile() {
|
||||
return getApplicationContextXmlFile(null);
|
||||
}
|
||||
|
||||
protected final ClassPathXmlApplicationContext getApplicationContext() {
|
||||
return new ClassPathXmlApplicationContext(getApplicationContextXmlFile(), getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified input stream to file.
|
||||
* @param _inputStream input stream
|
||||
* @param _path output file path
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void writeToFile(InputStream _inputStream, String _path) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(_path);
|
||||
try {
|
||||
FileCopyUtils.copy(_inputStream, fos);
|
||||
}
|
||||
finally {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified byte array to the output stream.
|
||||
* @param _bytes byte array
|
||||
* @param _outputStream output stream
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void writeToFile(byte[] _bytes, OutputStream _outputStream) throws IOException {
|
||||
FileCopyUtils.copy(_bytes, _outputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified byte array to the an output file.
|
||||
* @param _bytes byte array
|
||||
* @param _fileName output file
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void writeToFile(byte[] _bytes, String _fileName) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(_fileName);
|
||||
writeToFile(_bytes, fos);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new file of the given name.
|
||||
* If the file exists, it will be deleted.
|
||||
* The file will be deleted on exit of the JVM.
|
||||
* @param _fileName file name
|
||||
* @return file object
|
||||
*/
|
||||
protected File createNewFile(String _fileName) {
|
||||
File file = new File(_fileName);
|
||||
file.deleteOnExit();
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
getLogger().debug("File object [" + _fileName + "] created: " + file.getAbsolutePath());
|
||||
|
||||
assertFileNotExists(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes one or more files or directories.
|
||||
* @param _files file or directories
|
||||
*/
|
||||
protected void delete(String... _files) {
|
||||
for (String fileName : _files) {
|
||||
if (fileName == null) {
|
||||
continue;
|
||||
}
|
||||
File file = new File(fileName);
|
||||
if (file.exists()) {
|
||||
getLogger().debug("Deleting file [" + fileName + "].");
|
||||
if (!file.delete()) {
|
||||
file.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a directory exists, if not creates it and adds it to the DeleteOnExit hook.
|
||||
* @param _dir directory
|
||||
*/
|
||||
protected void ensureExists(String _dir) {
|
||||
File dir = new File(_dir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
dir.deleteOnExit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves class name and method name at the specified stacktrace index.
|
||||
* @param _index stacktrace index
|
||||
* @return fully qualified method name
|
||||
*/
|
||||
private static String getStackTraceString(int _index) {
|
||||
StackTraceElement[] arrStackTraceElems = new Throwable().fillInStackTrace().getStackTrace();
|
||||
final int lIndex = Math.min(arrStackTraceElems.length - 1, Math.max(0, _index));
|
||||
return arrStackTraceElems[lIndex].getClassName() + "." + arrStackTraceElems[lIndex].getMethodName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current method name.
|
||||
* @return method name
|
||||
*/
|
||||
public static String getMethodName() {
|
||||
return getStackTraceString(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the calling method name.
|
||||
* @return method name
|
||||
*/
|
||||
public static String getCallingMethodName() {
|
||||
return getStackTraceString(3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the specified file exists.
|
||||
* @param _file file object
|
||||
* @return the file object
|
||||
*/
|
||||
public static final File assertFileExists(File _file) {
|
||||
return assertFileExists(_file, true);
|
||||
}
|
||||
|
||||
public static final File assertFileNotExists(File _file) {
|
||||
return assertFileExists(_file, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the specified file exists or does not exists.
|
||||
* @param _file file object
|
||||
* @param _exists true if should exist, false otherwise
|
||||
* @return the file object
|
||||
*/
|
||||
private static File assertFileExists(File _file, boolean _exists) {
|
||||
assertThat(_file).as("File object is null.").isNotNull();
|
||||
if (_exists) {
|
||||
assertThat(_file.exists()).as("File [" + _file.getAbsolutePath() + "] does not exist.").isTrue();
|
||||
}
|
||||
else {
|
||||
assertThat(!_file.exists()).as("File [" + _file.getAbsolutePath() + "] exists.").isTrue();
|
||||
}
|
||||
return _file;
|
||||
}
|
||||
|
||||
public static final File assertFileExists(String _file) {
|
||||
return assertFileExists(new File(_file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes one or more test methods on the specified test class.
|
||||
* Catches exceptions during test setup (using reflection) and test invocation.
|
||||
* @param _testClass test class object
|
||||
* @param _methodNames String method names to invoke in order, no parameters expected
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
protected static void runTests(Class<? extends AbstractBaseTests> _testClass, String... _methodNames)
|
||||
throws Exception {
|
||||
AbstractBaseTests test;
|
||||
Method[] methods = new Method[_methodNames.length];
|
||||
String methodName = null;
|
||||
|
||||
test = _testClass.newInstance();
|
||||
for (int i = 0; i < _methodNames.length; i++) {
|
||||
methodName = _methodNames[i];
|
||||
methods[i] = _testClass.getMethod(methodName, (Class<?>[]) null);
|
||||
}
|
||||
|
||||
Method method = null;
|
||||
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
method = methods[i];
|
||||
method.invoke(test, (Object[]) null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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
|
||||
*
|
||||
* https://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.smb;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.smb.outbound.SmbMessageHandler;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import jcifs.DialectVersion;
|
||||
|
||||
/**
|
||||
* Starts the Spring Context and will initialize the Spring Integration routes.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
* @author Gregory Bragg
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(Main.class);
|
||||
|
||||
private Main() { }
|
||||
|
||||
/**
|
||||
* Load the Spring Integration Application Context
|
||||
*
|
||||
* @param args - command line arguments
|
||||
*/
|
||||
public static void main(final String... args) {
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
if (LOGGER.isInfoEnabled()) {
|
||||
LOGGER.info("\n================================================================="
|
||||
+ "\n "
|
||||
+ "\n Welcome to the Spring Integration SMB Test Client "
|
||||
+ "\n "
|
||||
+ "\n For more information please visit: "
|
||||
+ "\n https://github.com/SpringSource/spring-integration-extensions "
|
||||
+ "\n "
|
||||
+ "\n=================================================================");
|
||||
}
|
||||
|
||||
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
|
||||
|
||||
LOGGER.info("Please enter the: ");
|
||||
LOGGER.info("\t- SMB Host");
|
||||
LOGGER.info("\t- SMB Share and Directory");
|
||||
LOGGER.info("\t- SMB Username");
|
||||
LOGGER.info("\t- SMB Password");
|
||||
|
||||
LOGGER.info("Host: ");
|
||||
final String host = scanner.nextLine();
|
||||
|
||||
LOGGER.info("Share and Directory (e.g. myFile/path/to/): ");
|
||||
final String shareAndDir = scanner.nextLine();
|
||||
|
||||
LOGGER.info("Username (e.g. guest): ");
|
||||
final String username = scanner.nextLine();
|
||||
|
||||
LOGGER.info("Password (can be empty): ");
|
||||
final String password = scanner.nextLine();
|
||||
|
||||
context.getEnvironment().getSystemProperties().put("host", host);
|
||||
context.getEnvironment().getSystemProperties().put("shareAndDir", shareAndDir);
|
||||
context.getEnvironment().getSystemProperties().put("username", username);
|
||||
context.getEnvironment().getSystemProperties().put("password", password);
|
||||
|
||||
context.load("classpath:META-INF/spring/integration/*-context.xml");
|
||||
context.registerShutdownHook();
|
||||
context.refresh();
|
||||
|
||||
if (LOGGER.isInfoEnabled()) {
|
||||
LOGGER.info("\n========================================================="
|
||||
+ "\n "
|
||||
+ "\n Please press 'q + Enter' to quit the application. "
|
||||
+ "\n "
|
||||
+ "\n=========================================================");
|
||||
}
|
||||
|
||||
SmbSessionFactory smbSessionFactory = context.getBean("smbSession", SmbSessionFactory.class);
|
||||
smbSessionFactory.setSmbMinVersion(DialectVersion.SMB210);
|
||||
smbSessionFactory.setSmbMaxVersion(DialectVersion.SMB311);
|
||||
|
||||
LOGGER.info("Polling from Share: " + smbSessionFactory.getUrl());
|
||||
|
||||
// Create a test text file on the SMB file share
|
||||
SmbMessageHandler handlerTxt = new SmbMessageHandler(smbSessionFactory);
|
||||
handlerTxt.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handlerTxt.setFileNameGenerator(message -> "handlerContent.txt");
|
||||
handlerTxt.setAutoCreateDirectory(true);
|
||||
handlerTxt.setUseTemporaryFileName(false);
|
||||
handlerTxt.setBeanFactory(context.getBeanFactory());
|
||||
handlerTxt.afterPropertiesSet();
|
||||
handlerTxt.handleMessage(new GenericMessage<String>("hello, my text"));
|
||||
|
||||
// Create a test binary file on the SMB file share using a temporary filename
|
||||
SmbMessageHandler handlerBin = new SmbMessageHandler(smbSessionFactory);
|
||||
handlerBin.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handlerBin.setFileNameGenerator(message -> "handlerContent.bin");
|
||||
handlerBin.setAutoCreateDirectory(true);
|
||||
handlerBin.setUseTemporaryFileName(true);
|
||||
handlerBin.setBeanFactory(context.getBeanFactory());
|
||||
handlerBin.afterPropertiesSet();
|
||||
handlerBin.handleMessage(new GenericMessage<byte[]>("hello, my bytes".getBytes()));
|
||||
|
||||
while (true) {
|
||||
final String input = scanner.nextLine();
|
||||
|
||||
if ("q".equals(input.trim())) {
|
||||
scanner.close();
|
||||
context.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (LOGGER.isInfoEnabled()) {
|
||||
LOGGER.info("Exiting application...bye.");
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<int:message-history/>
|
||||
|
||||
<bean id="smbSessionFactory" class="org.springframework.integration.smb.session.SmbSessionFactory">
|
||||
<property name="host" value="localhost"/>
|
||||
<property name="port" value="0"/>
|
||||
<property name="domain" value=""/>
|
||||
<property name="username" value="sambagu@est"/>
|
||||
<property name="password" value="sambag%uest"/>
|
||||
<property name="shareAndDir" value="smb-share/"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="smbInboundChannelAdapter"
|
||||
session-factory="smbSessionFactory"
|
||||
channel="smbInboundChannel"
|
||||
auto-create-local-directory="true"
|
||||
local-directory="file:test-temp/local-5"
|
||||
remote-directory="test-temp/remote-9"
|
||||
delete-remote-files="false">
|
||||
<int:poller fixed-rate="1000"/>
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="smbInboundChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Prafull Kumar Soni
|
||||
* @author Artem Bilan
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
public class SmbMessageHistoryTests extends AbstractBaseTests {
|
||||
|
||||
@Test
|
||||
public void testMessageHistory() throws URISyntaxException {
|
||||
ClassPathXmlApplicationContext applicationContext = getApplicationContext();
|
||||
SourcePollingChannelAdapter adapter = applicationContext
|
||||
.getBean("smbInboundChannelAdapter", SourcePollingChannelAdapter.class);
|
||||
assertThat("smbInboundChannelAdapter").isEqualTo(adapter.getComponentName());
|
||||
assertThat("smb:inbound-channel-adapter").isEqualTo(adapter.getComponentType());
|
||||
|
||||
SmbSessionFactory smbSessionFactory = applicationContext.getBean(SmbSessionFactory.class);
|
||||
|
||||
String url = smbSessionFactory.getUrl();
|
||||
URI uri = new URI(url);
|
||||
assertThat("sambagu%40est:sambag%25uest").isEqualTo(uri.getRawUserInfo());
|
||||
assertThat("sambagu@est:sambag%uest").isEqualTo(uri.getUserInfo());
|
||||
|
||||
applicationContext.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<bean id="smbSessionFactory" class="org.springframework.integration.smb.session.SmbSessionFactory">
|
||||
<property name="host" value="localhost"/>
|
||||
<property name="domain" value=""/>
|
||||
<property name="username" value="sambaguest"/>
|
||||
<property name="password" value="sambaguest"/>
|
||||
<property name="shareAndDir" value="smb-share/"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="adapterSmb"
|
||||
session-factory="smbSessionFactory"
|
||||
channel="smbIn"
|
||||
filename-pattern="foo"
|
||||
local-directory="test-temp/local-10"
|
||||
remote-directory="test-temp/remote-10"
|
||||
auto-create-local-directory="true"
|
||||
delete-remote-files="false">
|
||||
<int:poller ref="smbPoller" />
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="adapterSmb2"
|
||||
session-factory="smbSessionFactory"
|
||||
channel="smbIn"
|
||||
filter="filter"
|
||||
local-directory="test-temp"
|
||||
remote-directory="test-temp/remote-11"
|
||||
auto-create-local-directory="true"
|
||||
delete-remote-files="false">
|
||||
<int:poller ref="smbPoller" />
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter"/>
|
||||
</bean>
|
||||
|
||||
<int:poller fixed-rate="3000" id="smbPoller" />
|
||||
|
||||
<int:channel id="smbIn">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<bean id="smbSessionFactory" class="org.springframework.integration.smb.session.SmbSessionFactory">
|
||||
<property name="host" value="localhost"/>
|
||||
<property name="domain" value=""/>
|
||||
<property name="username" value="sambaguest"/>
|
||||
<property name="password" value="sambaguest"/>
|
||||
<property name="shareAndDir" value="smb-share/"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="adapterSmbDontAutoCreate"
|
||||
channel="smbIn"
|
||||
session-factory="smbSessionFactory"
|
||||
filter="filter"
|
||||
local-directory="file:test-temp/local-6"
|
||||
remote-directory="test-temp/remote-12"
|
||||
auto-create-local-directory="false"
|
||||
delete-remote-files="false">
|
||||
<int:poller fixed-rate="1000"/>
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<bean id="filter" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.file.entries.EntryListFilter"/>
|
||||
</bean>
|
||||
|
||||
<int:channel id="smbIn">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* https://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.smb;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Prafull Kumar Soni
|
||||
*
|
||||
*/
|
||||
public class SmbParserInboundTests extends AbstractBaseTests {
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
ensureExists("test-temp/remote-10");
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalFilesAutoCreationTrue() {
|
||||
assertFileNotExists(new File("test-temp/local-10"));
|
||||
new ClassPathXmlApplicationContext(getApplicationContextXmlFile(), this.getClass());
|
||||
assertFileExists(new File("test-temp/local-10"));
|
||||
assertFileNotExists(new File("test-temp/local-6"));
|
||||
}
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testLocalFilesAutoCreationFalse() {
|
||||
assertFileNotExists(new File("test-temp/local-6"));
|
||||
new ClassPathXmlApplicationContext(getApplicationContextXmlFile("-fail"), this.getClass())
|
||||
.close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
delete("test-temp/local-10", "test-temp/local-6");
|
||||
}
|
||||
|
||||
public static void main(String[] _args) throws Exception {
|
||||
new SmbParserInboundTests().cleanUp();
|
||||
runTests(SmbParserInboundTests.class, "testLocalFilesAutoCreationTrue", "testLocalFilesAutoCreationFalse");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb
|
||||
https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<bean id="smbSessionFactory"
|
||||
class="org.springframework.integration.smb.config.SmbInboundChannelAdapterParserTests.TestSessionFactoryBean"/>
|
||||
|
||||
<bean id="acceptAllFilter" class="org.springframework.integration.file.filters.AcceptAllFileListFilter"/>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="smbInbound"
|
||||
auto-startup="false"
|
||||
channel="smbChannel"
|
||||
session-factory="smbSessionFactory"
|
||||
charset="UTF-8"
|
||||
auto-create-local-directory="true"
|
||||
delete-remote-files="true"
|
||||
filename-pattern="*.txt"
|
||||
local-filter="acceptAllFilter"
|
||||
local-directory="file:test-temp/local-1"
|
||||
remote-file-separator=""
|
||||
comparator="comparator"
|
||||
temporary-file-suffix=".working.tmp"
|
||||
remote-directory="test-temp/remote-1">
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<bean id="comparator" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="java.util.Comparator"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:inbound-channel-adapter
|
||||
channel="smbChannel"
|
||||
auto-startup="false"
|
||||
session-factory="smbSessionFactory"
|
||||
charset="UTF-8"
|
||||
auto-create-local-directory="true"
|
||||
delete-remote-files="true"
|
||||
filter="entryListFilter"
|
||||
local-directory="file:test-temp/local-2"
|
||||
remote-directory="test-temp/remote-2">
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="simpleAdapter"
|
||||
channel="smbChannel"
|
||||
auto-startup="false"
|
||||
session-factory="smbSessionFactory"
|
||||
local-directory="file:test-temp/local-3"
|
||||
remote-directory="test-temp/remote-3">
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="smbChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
<bean id="entryListFilter" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.file.filters.FileListFilter"/>
|
||||
</bean>
|
||||
|
||||
<int:poller fixed-rate="10000" default="true"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.file.filters.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.smb.filters.SmbPersistentAcceptOnceFileListFilter;
|
||||
import org.springframework.integration.smb.filters.SmbSimplePatternFileListFilter;
|
||||
import org.springframework.integration.smb.inbound.SmbInboundFileSynchronizer;
|
||||
import org.springframework.integration.smb.inbound.SmbInboundFileSynchronizingMessageSource;
|
||||
import org.springframework.integration.smb.session.SmbSession;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
* @author Prafull Kumar Soni
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class SmbInboundChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Test(timeout = 100000)
|
||||
public void testSmbInboundChannelAdapterComplete() {
|
||||
|
||||
final SourcePollingChannelAdapter adapter = this.applicationContext.getBean("smbInbound", SourcePollingChannelAdapter.class);
|
||||
final PriorityBlockingQueue<?> queue = TestUtils.getPropertyValue(adapter, "source.fileSource.toBeReceived", PriorityBlockingQueue.class);
|
||||
assertThat(queue.comparator()).isNotNull();
|
||||
assertThat("smbInbound").isEqualTo(adapter.getComponentName());
|
||||
assertThat("smb:inbound-channel-adapter").isEqualTo(adapter.getComponentType());
|
||||
assertThat(applicationContext.getBean("smbChannel")).isEqualTo(TestUtils.getPropertyValue(adapter, "outputChannel"));
|
||||
SmbInboundFileSynchronizingMessageSource inbound =
|
||||
(SmbInboundFileSynchronizingMessageSource) TestUtils.getPropertyValue(adapter, "source");
|
||||
|
||||
SmbInboundFileSynchronizer fisync =
|
||||
(SmbInboundFileSynchronizer) TestUtils.getPropertyValue(inbound, "synchronizer");
|
||||
assertThat(".working.tmp").isEqualTo(TestUtils.getPropertyValue(fisync, "temporaryFileSuffix", String.class));
|
||||
String remoteFileSeparator = (String) TestUtils.getPropertyValue(fisync, "remoteFileSeparator");
|
||||
assertThat(remoteFileSeparator).isNotNull();
|
||||
assertThat("").isEqualTo(remoteFileSeparator);
|
||||
FileListFilter<?> filter = TestUtils.getPropertyValue(fisync, "filter", FileListFilter.class);
|
||||
assertThat(filter).isNotNull();
|
||||
assertThat(filter).isInstanceOf(CompositeFileListFilter.class);
|
||||
Set<?> fileFilters = TestUtils.getPropertyValue(filter, "fileFilters", Set.class);
|
||||
|
||||
Iterator<?> filtersIterator = fileFilters.iterator();
|
||||
assertThat(filtersIterator.next()).isInstanceOf(SmbSimplePatternFileListFilter.class);
|
||||
assertThat(filtersIterator.next()).isInstanceOf(SmbPersistentAcceptOnceFileListFilter.class);
|
||||
Object sessionFactory = TestUtils.getPropertyValue(fisync, "remoteFileTemplate.sessionFactory");
|
||||
assertThat(SmbSessionFactory.class.isAssignableFrom(sessionFactory.getClass())).isTrue();
|
||||
|
||||
FileListFilter<?> acceptAllFilter = this.applicationContext.getBean("acceptAllFilter", FileListFilter.class);
|
||||
assertThat(TestUtils.getPropertyValue(inbound, "fileSource.scanner.filter.fileFilters", Collection.class)
|
||||
.contains(acceptAllFilter)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCachingSessionFactoryByDefault() {
|
||||
SourcePollingChannelAdapter adapter = applicationContext.getBean("simpleAdapter", SourcePollingChannelAdapter.class);
|
||||
Object sessionFactory = TestUtils.getPropertyValue(adapter, "source.synchronizer.remoteFileTemplate.sessionFactory");
|
||||
assertThat(sessionFactory).isInstanceOf(SmbSessionFactory.class);
|
||||
SmbInboundFileSynchronizer fisync =
|
||||
TestUtils.getPropertyValue(adapter, "source.synchronizer", SmbInboundFileSynchronizer.class);
|
||||
String remoteFileSeparator = (String) TestUtils.getPropertyValue(fisync, "remoteFileSeparator");
|
||||
assertThat(remoteFileSeparator).isNotNull();
|
||||
assertThat("/").isEqualTo(remoteFileSeparator);
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void testSmbInboundChannelAdapterCompleteNoId() {
|
||||
|
||||
Map<String, SourcePollingChannelAdapter> spcas = applicationContext.getBeansOfType(SourcePollingChannelAdapter.class);
|
||||
SourcePollingChannelAdapter adapter = null;
|
||||
for (String key : spcas.keySet()) {
|
||||
if (!key.equals("smbInbound") && !key.equals("simpleAdapter")) {
|
||||
adapter = spcas.get(key);
|
||||
}
|
||||
}
|
||||
assertThat(adapter).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
public static class TestSessionFactoryBean implements FactoryBean<SmbSessionFactory> {
|
||||
|
||||
public SmbSessionFactory getObject() {
|
||||
SmbSessionFactory smbFactory = mock(SmbSessionFactory.class);
|
||||
SmbSession session = mock(SmbSession.class);
|
||||
when(smbFactory.getSession()).thenReturn(session);
|
||||
return smbFactory;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
return SmbSessionFactory.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<bean id="smbSessionFactory"
|
||||
class="org.springframework.integration.smb.session.SmbSessionFactory"
|
||||
p:host="localhost"
|
||||
p:port="0"
|
||||
p:domain="sambaguest"
|
||||
p:username="sambaguest"
|
||||
p:password="sambaguest"
|
||||
p:shareAndDir="smb-share/"/>
|
||||
|
||||
<int-smb:inbound-channel-adapter id="smbInboundChannelAdapter"
|
||||
channel="smbInboundChannel"
|
||||
session-factory="smbSessionFactory"
|
||||
charset="UTF-8"
|
||||
remote-directory="test-temp/remote-4"
|
||||
remote-file-separator="/"
|
||||
filename-regex=".*\.txt$"
|
||||
delete-remote-files="true"
|
||||
temporary-file-suffix=".working.tmp"
|
||||
auto-create-local-directory="true"
|
||||
local-directory="file:test-temp/local-4">
|
||||
<int:poller fixed-rate="5000" error-channel="nullChannel"/>
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="smbInboundChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler;
|
||||
import org.springframework.integration.smb.AbstractBaseTests;
|
||||
import org.springframework.integration.smb.inbound.SmbInboundFileSynchronizingMessageSource;
|
||||
import org.springframework.integration.smb.session.SmbSession;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* System tests that perform SMB access without any mocking.
|
||||
* These tests are annotated with '@Ignore', as they requires real SMB share configured
|
||||
* in the application context/smbClientFactory in order to succeed.
|
||||
* The test cases create directories and files autonomously and perform clean-up
|
||||
* on a best effort basis.
|
||||
*
|
||||
* @author Markus Spann
|
||||
* @author Gunnar Hillert
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
public class SmbInboundOutboundSample extends AbstractBaseTests {
|
||||
|
||||
private static final String INBOUND_APPLICATION_CONTEXT_XML = "SmbInboundChannelAdapterSample-context.xml";
|
||||
private static final String OUTBOUND_APPLICATION_CONTEXT_XML = "SmbOutboundChannelAdapterSample-context.xml";
|
||||
|
||||
@Ignore("Actual SMB share must be configured in file [" + INBOUND_APPLICATION_CONTEXT_XML + "].")
|
||||
@Test
|
||||
public void testSmbInboundChannelAdapter() throws Exception {
|
||||
String testLocalDir = "test-temp/local-4/";
|
||||
String testRemoteDir = "test-temp/remote-4/";
|
||||
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext(INBOUND_APPLICATION_CONTEXT_XML, this.getClass());
|
||||
|
||||
Object consumer = ac.getBean("smbInboundChannelAdapter");
|
||||
assertThat(consumer).isInstanceOf(SourcePollingChannelAdapter.class);
|
||||
Object messageSource = TestUtils.getPropertyValue(consumer, "source");
|
||||
assertThat(messageSource).isInstanceOf(SmbInboundFileSynchronizingMessageSource.class);
|
||||
|
||||
// retrieve the session factory bean to place a couple of test files remotely using a new session
|
||||
SmbSessionFactory smbSessionFactory = ac.getBean("smbSessionFactory", SmbSessionFactory.class);
|
||||
SmbSession smbSession = smbSessionFactory.getSession();
|
||||
|
||||
// place text files onto the share
|
||||
smbSession.mkdir(testRemoteDir);
|
||||
|
||||
String[] fileNames = createTestFileNames(5);
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
smbSession.write(("File [" + fileNames[i] + "] written by test case [" + getMethodName() + "].").getBytes(),
|
||||
testRemoteDir + fileNames[i]);
|
||||
}
|
||||
|
||||
// allow time for the files to arrive locally
|
||||
Thread.sleep(5000);
|
||||
|
||||
// confirm the local presence of all test files
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
assertFileExists(testLocalDir + fileNames[i]).deleteOnExit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Ignore("Actual SMB share must be configured in file [" + OUTBOUND_APPLICATION_CONTEXT_XML + "].")
|
||||
@Test
|
||||
public void testSmbOutboundChannelAdapter() throws Exception {
|
||||
String testRemoteDir = "test-temp/remote-8/";
|
||||
String testLocalDir = "test-temp/local-8/";
|
||||
new File(testLocalDir).mkdirs();
|
||||
|
||||
String[] fileNames = createTestFileNames(5);
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
writeToFile(("File [" + fileNames[i] + "] written by test case [" + getMethodName() + "].").getBytes(),
|
||||
testLocalDir + fileNames[i]);
|
||||
}
|
||||
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext(OUTBOUND_APPLICATION_CONTEXT_XML, this.getClass());
|
||||
|
||||
Object consumer = ac.getBean("smbOutboundChannelAdapter");
|
||||
assertThat(consumer).isInstanceOf(EventDrivenConsumer.class);
|
||||
Object messageSource = TestUtils.getPropertyValue(consumer, "handler");
|
||||
assertThat(messageSource).isInstanceOf(FileTransferringMessageHandler.class);
|
||||
|
||||
MessageChannel smbChannel = ac.getBean("smbOutboundChannel", MessageChannel.class);
|
||||
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
smbChannel.send(new GenericMessage<File>(new File(testLocalDir + fileNames[i])));
|
||||
}
|
||||
|
||||
Thread.sleep(3000);
|
||||
|
||||
// retrieve the session factory bean to check the test files are present in the remote location
|
||||
SmbSessionFactory smbSessionFactory = ac.getBean("smbSessionFactory", SmbSessionFactory.class);
|
||||
SmbSession smbSession = smbSessionFactory.getSession();
|
||||
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
String remoteFile = testRemoteDir + fileNames[i];
|
||||
assertThat(smbSession.exists(remoteFile)).as("Remote file [" + remoteFile + "] does not exist.").isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String[] createTestFileNames(int _nbTestFiles) {
|
||||
String[] fileNames = new String[_nbTestFiles];
|
||||
for (int i = 0; i < fileNames.length; i++) {
|
||||
fileNames[i] = "test-file-" + i + ".txt";
|
||||
}
|
||||
return fileNames;
|
||||
}
|
||||
|
||||
public static void main(String[] _args) throws Exception {
|
||||
runTests(SmbInboundOutboundSample.class, "testSmbOutboundChannelAdapter", "testSmbInboundChannelAdapter");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<bean id="smbSessionFactory" class="org.springframework.integration.smb.session.SmbSessionFactory">
|
||||
<property name="host" value="localhost"/>
|
||||
<property name="port" value="0"/>
|
||||
<property name="domain" value=""/>
|
||||
<property name="username" value="sambaguest"/>
|
||||
<property name="password" value="sambaguest"/>
|
||||
<property name="shareAndDir" value="smb-share/"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:outbound-channel-adapter id="smbOutboundChannelAdapter"
|
||||
channel="smbPubSubChannel"
|
||||
session-factory="smbSessionFactory"
|
||||
remote-directory="test-temp/remote-5"
|
||||
charset="UTF-8"
|
||||
remote-file-separator="."
|
||||
temporary-file-suffix=".working.tmp"
|
||||
remote-filename-generator="fileNameGenerator"
|
||||
order="23"/>
|
||||
|
||||
<int-smb:outbound-channel-adapter id="smbOutboundChannelAdapter2"
|
||||
channel="smbPubSubChannel"
|
||||
session-factory="smbSessionFactory"
|
||||
remote-directory="test-temp/remote-6"
|
||||
charset="UTF-8"
|
||||
remote-file-separator="."
|
||||
temporary-file-suffix=".working.tmp"
|
||||
remote-filename-generator="fileNameGenerator"
|
||||
order="12"/>
|
||||
|
||||
<int-smb:outbound-channel-adapter id="simpleAdapter"
|
||||
channel="smbPubSubChannel"
|
||||
session-factory="smbSessionFactory"
|
||||
remote-directory="test-temp/remote-7"/>
|
||||
|
||||
<int:publish-subscribe-channel id="smbPubSubChannel"/>
|
||||
|
||||
<bean id="fileNameGenerator" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.file.FileNameGenerator"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.smb.AbstractBaseTests;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
* @author Prafull Kumar Soni
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
public class SmbOutboundChannelAdapterParserTests extends AbstractBaseTests {
|
||||
|
||||
@Test
|
||||
public void testSmbOutboundChannelAdapterComplete() {
|
||||
ApplicationContext ac = getApplicationContext();
|
||||
|
||||
Object consumer = ac.getBean("smbOutboundChannelAdapter");
|
||||
assertThat(consumer).isInstanceOf(EventDrivenConsumer.class);
|
||||
|
||||
PublishSubscribeChannel channel = ac.getBean("smbPubSubChannel", PublishSubscribeChannel.class);
|
||||
assertThat(channel).isEqualTo(TestUtils.getPropertyValue(consumer, "inputChannel"));
|
||||
assertThat("smbOutboundChannelAdapter").isEqualTo(((EventDrivenConsumer) consumer).getComponentName());
|
||||
|
||||
Object messageHandler = TestUtils.getPropertyValue(consumer, "handler");
|
||||
String remoteFileSeparator = (String) TestUtils.getPropertyValue(messageHandler, "remoteFileTemplate.remoteFileSeparator");
|
||||
assertThat(remoteFileSeparator).isNotNull();
|
||||
assertThat(".working.tmp").isEqualTo(TestUtils.getPropertyValue(messageHandler, "remoteFileTemplate.temporaryFileSuffix", String.class));
|
||||
assertThat(".").isEqualTo(remoteFileSeparator);
|
||||
assertThat(ac.getBean("fileNameGenerator")).isEqualTo(TestUtils.getPropertyValue(messageHandler, "remoteFileTemplate.fileNameGenerator"));
|
||||
assertThat("UTF-8").isEqualTo(TestUtils.getPropertyValue(messageHandler, "remoteFileTemplate.charset", Charset.class).name());
|
||||
|
||||
Object sessionFactoryProp = TestUtils.getPropertyValue(messageHandler, "remoteFileTemplate.sessionFactory");
|
||||
assertThat(SmbSessionFactory.class).isEqualTo(sessionFactoryProp.getClass());
|
||||
|
||||
SmbSessionFactory smbSessionFactory = (SmbSessionFactory) sessionFactoryProp;
|
||||
assertThat("localhost").isEqualTo(TestUtils.getPropertyValue(smbSessionFactory, "host"));
|
||||
assertThat(0).isEqualTo(TestUtils.getPropertyValue(smbSessionFactory, "port"));
|
||||
assertThat(23).isEqualTo(TestUtils.getPropertyValue(messageHandler, "order"));
|
||||
|
||||
// verify subscription order
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<MessageHandler> handlers = (Set<MessageHandler>) TestUtils.getPropertyValue(
|
||||
TestUtils.getPropertyValue(channel, "dispatcher"), "handlers");
|
||||
Iterator<MessageHandler> iterator = handlers.iterator();
|
||||
assertThat(TestUtils.getPropertyValue(ac.getBean("smbOutboundChannelAdapter2"), "handler")).isSameAs(iterator.next());
|
||||
assertThat(messageHandler).isSameAs(iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noCachingByDefault() {
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext(getApplicationContextXmlFile(), this.getClass());
|
||||
Object adapter = ac.getBean("simpleAdapter");
|
||||
Object sfProperty = TestUtils.getPropertyValue(adapter, "handler.remoteFileTemplate.sessionFactory");
|
||||
assertThat(SmbSessionFactory.class).isEqualTo(sfProperty.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd">
|
||||
|
||||
<bean id="smbSessionFactory"
|
||||
class="org.springframework.integration.smb.session.SmbSessionFactory"
|
||||
p:host="localhost"
|
||||
p:port="0"
|
||||
p:domain="sambaguest"
|
||||
p:username="sambaguest"
|
||||
p:password="sambaguest"
|
||||
p:shareAndDir="smb-share/"/>
|
||||
|
||||
<int:channel id="smbOutboundChannel" />
|
||||
|
||||
<int-smb:outbound-channel-adapter id="smbOutboundChannelAdapter"
|
||||
session-factory="smbSessionFactory"
|
||||
remote-directory="test-temp/remote-8"
|
||||
channel="smbOutboundChannel"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* https://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.smb.inbound;
|
||||
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.integration.smb.AbstractBaseTests;
|
||||
import org.springframework.integration.smb.session.SmbSession;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
|
||||
import jcifs.smb.SmbFile;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SmbInboundRemoteFileSystemSynchronizerTests extends AbstractBaseTests {
|
||||
|
||||
private SmbSession smbSession;
|
||||
|
||||
private SmbSessionFactory smbSessionFactory;
|
||||
|
||||
private String testLocalDir = "test-temp/local-9/";
|
||||
|
||||
private String testRemoteDir = "test-temp/remote-9/";
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
delete(testLocalDir);
|
||||
ensureExists(testRemoteDir);
|
||||
|
||||
smbSession = mock(SmbSession.class);
|
||||
smbSessionFactory = new TestSmbSessionFactory();
|
||||
smbSessionFactory.setHost("localhost");
|
||||
smbSessionFactory.setPort(0);
|
||||
smbSessionFactory.setDomain("");
|
||||
smbSessionFactory.setUsername("sambaguest");
|
||||
smbSessionFactory.setPassword("sambaguest");
|
||||
smbSessionFactory.setShareAndDir("smb-share/");
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testCopyFileToLocalDir() throws Exception {
|
||||
// File localDirectoy = new File(testLocalDir);
|
||||
// assertFileNotExists(localDirectoy);
|
||||
//
|
||||
// SmbInboundFileSynchronizer synchronizer = spy(new SmbInboundFileSynchronizer(smbSessionFactory));
|
||||
// synchronizer.setDeleteRemoteFiles(true);
|
||||
// synchronizer.setRemoteDirectory(testRemoteDir);
|
||||
// synchronizer.setFilter(new SmbRegexPatternFileListFilter(".*\\.test$"));
|
||||
//
|
||||
// SmbInboundFileSynchronizingMessageSource messageSource = new SmbInboundFileSynchronizingMessageSource(synchronizer);
|
||||
// messageSource.setAutoCreateLocalDirectory(true);
|
||||
//
|
||||
// messageSource.setLocalDirectory(localDirectoy);
|
||||
// messageSource.afterPropertiesSet();
|
||||
//
|
||||
// String[] testFiles = new String[] {"a.test", "b.test"};
|
||||
//
|
||||
// for (String testFile : testFiles) {
|
||||
// Message<File> message = messageSource.receive();
|
||||
// assertNotNull(message);
|
||||
// assertEquals(testFile, message.getPayload().getName());
|
||||
// assertFileExists(new File(testLocalDir + "/" + testFile));
|
||||
// }
|
||||
//
|
||||
// Message<File> nothing = messageSource.receive();
|
||||
// assertNull(nothing);
|
||||
//
|
||||
// // two times because on the third receive (above) the internal queue will be empty
|
||||
// verify(synchronizer, times(2)).synchronizeToLocalDirectory(localDirectoy);
|
||||
// }
|
||||
|
||||
class TestSmbSessionFactory extends SmbSessionFactory {
|
||||
|
||||
@Override
|
||||
protected SmbSession createSession() {
|
||||
try {
|
||||
List<SmbFile> smbFiles = new ArrayList<SmbFile>();
|
||||
for (String fileName : new File(testRemoteDir).list()) {
|
||||
SmbFile file = smbSession.createSmbFileObject(fileName);
|
||||
smbFiles.add(file);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
|
||||
public Object answer(InvocationOnMock _invocation) throws Throwable {
|
||||
String path = (String) _invocation.getArguments()[0];
|
||||
OutputStream os = (OutputStream) _invocation.getArguments()[1];
|
||||
writeToFile((this.getClass().getSimpleName() + " : TEST : " + path).getBytes(), os);
|
||||
return null;
|
||||
}
|
||||
}).when(smbSession).read(Mockito.eq(testRemoteDir + "/" + fileName), Mockito.any(OutputStream.class));
|
||||
}
|
||||
|
||||
when(smbSession.list(testRemoteDir)).thenReturn(smbFiles.toArray(new SmbFile[] { }));
|
||||
when(smbSession.remove(Mockito.anyString())).thenReturn(true);
|
||||
return smbSession;
|
||||
|
||||
}
|
||||
catch (Exception _ex) {
|
||||
throw new RuntimeException("Failed to create mock session.", _ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.outbound;
|
||||
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.junit.After;
|
||||
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.beans.factory.BeanFactory;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.smb.AbstractBaseTests;
|
||||
import org.springframework.integration.smb.session.SmbSession;
|
||||
import org.springframework.integration.smb.session.SmbSessionFactory;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
import jcifs.smb.SmbFile;
|
||||
|
||||
/**
|
||||
* @author Markus Spann
|
||||
* @author Artem Bilan
|
||||
* @author Prafull Kumar Soni
|
||||
* @author Gregory Bragg
|
||||
*/
|
||||
public class SmbSendingMessageHandlerTests extends AbstractBaseTests {
|
||||
|
||||
private SmbSession smbSession;
|
||||
|
||||
private SmbSessionFactory smbSessionFactory;
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
smbSession = mock(SmbSession.class);
|
||||
smbSessionFactory = new TestSmbSessionFactory();
|
||||
smbSessionFactory.setHost("localhost");
|
||||
smbSessionFactory.setPort(0);
|
||||
smbSessionFactory.setDomain("");
|
||||
smbSessionFactory.setUsername("sambaguest");
|
||||
smbSessionFactory.setPassword("sambaguest");
|
||||
smbSessionFactory.setShareAndDir("smb-share/");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
FileSystemUtils.deleteRecursively(new File("remote-target-dir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleFileContentMessage() {
|
||||
File file = createNewFile("remote-target-dir/handlerContent.test");
|
||||
SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handler.setFileNameGenerator(message -> "handlerContent.test");
|
||||
handler.setAutoCreateDirectory(true);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage<>("hello"));
|
||||
assertFileExists(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleFileAsByte() {
|
||||
File file = createNewFile("remote-target-dir/handlerContent.test");
|
||||
SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handler.setFileNameGenerator(message -> "handlerContent.test");
|
||||
handler.setAutoCreateDirectory(true);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage<>("hello".getBytes()));
|
||||
assertFileExists(file);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testHandleFileMessage() throws Exception {
|
||||
// File file = createNewFile("remote-target-dir/template.mf.test");
|
||||
// SmbMessageHandler handler = new SmbMessageHandler(smbSessionFactory);
|
||||
// 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")));
|
||||
// assertFileExists(file);
|
||||
// }
|
||||
|
||||
class TestSmbSessionFactory extends SmbSessionFactory {
|
||||
|
||||
@Override
|
||||
protected SmbSession createSession() {
|
||||
try {
|
||||
when(smbSession.remove(Mockito.anyString())).thenReturn(true);
|
||||
when(smbSession.list(Mockito.anyString())).thenReturn(new SmbFile[0]);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
|
||||
@Override
|
||||
public Object answer(InvocationOnMock _invocation) throws Throwable {
|
||||
String path = (String) _invocation.getArguments()[0];
|
||||
OutputStream os = (OutputStream) _invocation.getArguments()[1];
|
||||
writeToFile((this.getClass().getSimpleName() + " : TEST : " + path).getBytes(), os);
|
||||
return null;
|
||||
}
|
||||
}).when(smbSession).read(Mockito.anyString(), Mockito.any(OutputStream.class));
|
||||
|
||||
doAnswer(_invocation -> {
|
||||
InputStream inputStream = (InputStream) _invocation.getArguments()[0];
|
||||
String path = (String) _invocation.getArguments()[1];
|
||||
writeToFile(inputStream, path);
|
||||
return null;
|
||||
}).when(smbSession)
|
||||
.write(Mockito.any(InputStream.class), Mockito.anyString());
|
||||
|
||||
// when(smbSession.write(Mockito.any(byte[].class), Mockito.anyString())).thenReturn(null);
|
||||
// when(smbSession.write(Mockito.any(File.class), Mockito.anyString())).thenReturn(null);
|
||||
|
||||
doAnswer((Answer<Object>) _invocation -> {
|
||||
String path = (String) _invocation.getArguments()[0];
|
||||
return new File(path).mkdirs();
|
||||
}).when(smbSession).mkdir(Mockito.anyString());
|
||||
|
||||
doAnswer(_invocation -> {
|
||||
String pathFrom = (String) _invocation.getArguments()[0];
|
||||
String pathTo = (String) _invocation.getArguments()[1];
|
||||
new File(pathFrom).renameTo(new File(pathTo));
|
||||
return null;
|
||||
}).when(smbSession)
|
||||
.rename(Mockito.anyString(), Mockito.anyString());
|
||||
|
||||
doNothing().when(smbSession).close();
|
||||
when(smbSession.isOpen()).thenReturn(true);
|
||||
return smbSession;
|
||||
|
||||
}
|
||||
catch (Exception _ex) {
|
||||
throw new RuntimeException("Failed to create mock session.", _ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.session;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.junit.After;
|
||||
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.beans.factory.BeanFactory;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler;
|
||||
import org.springframework.integration.smb.AbstractBaseTests;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
import jcifs.CIFSContext;
|
||||
import jcifs.context.SingletonContext;
|
||||
import jcifs.smb.SmbFile;
|
||||
|
||||
/**
|
||||
* @author Gregory Bragg
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class SmbSessionFactoryWithCIFSContextTests extends AbstractBaseTests {
|
||||
|
||||
private SmbSession smbSession;
|
||||
|
||||
private SmbSessionFactory smbSessionFactory;
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
smbSession = mock(SmbSession.class);
|
||||
|
||||
smbSessionFactory = new TestSmbSessionFactory(SingletonContext.getInstance());
|
||||
assertThat(smbSessionFactory).as("TestSmbSessionFactory object is null.").isNotNull();
|
||||
|
||||
smbSessionFactory.setHost("localhost");
|
||||
smbSessionFactory.setPort(445);
|
||||
smbSessionFactory.setDomain("");
|
||||
smbSessionFactory.setUsername("sambaguest");
|
||||
smbSessionFactory.setPassword("sambaguest");
|
||||
smbSessionFactory.setShareAndDir("smb-share/");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
FileSystemUtils.deleteRecursively(new File("remote-target-dir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleFileContentMessage() {
|
||||
File file = createNewFile("remote-target-dir/handlerContent.test");
|
||||
FileTransferringMessageHandler<?> handler = new FileTransferringMessageHandler<>(smbSessionFactory);
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
|
||||
handler.setFileNameGenerator(message -> "handlerContent.test");
|
||||
handler.setAutoCreateDirectory(true);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage<>("hello"));
|
||||
assertFileExists(file);
|
||||
}
|
||||
|
||||
class TestSmbSessionFactory extends SmbSessionFactory {
|
||||
|
||||
private CIFSContext context;
|
||||
|
||||
protected TestSmbSessionFactory(CIFSContext _context) {
|
||||
assertThat(_context).as("CIFSContext object is null.").isNotNull();
|
||||
this.context = _context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SmbSession createSession() {
|
||||
try {
|
||||
// test for a constructor with a CIFSContext
|
||||
SmbShare smbShare = new SmbShare(this, this.context);
|
||||
assertThat(smbShare).as("SmbShare object is null.").isNotNull();
|
||||
assertThat(smbShare.toString()).isEqualTo("smb://sambaguest:sambaguest@localhost:445/smb-share/");
|
||||
|
||||
// the rest has been copied from SmbSendingMessageHandlerTests
|
||||
when(smbSession.remove(Mockito.anyString())).thenReturn(true);
|
||||
when(smbSession.list(Mockito.anyString())).thenReturn(new SmbFile[0]);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
|
||||
@Override
|
||||
public Object answer(InvocationOnMock _invocation) throws Throwable {
|
||||
String path = (String) _invocation.getArguments()[0];
|
||||
OutputStream os = (OutputStream) _invocation.getArguments()[1];
|
||||
writeToFile((this.getClass().getSimpleName() + " : TEST : " + path).getBytes(), os);
|
||||
return null;
|
||||
}
|
||||
}).when(smbSession).read(Mockito.anyString(), Mockito.any(OutputStream.class));
|
||||
|
||||
doAnswer(_invocation -> {
|
||||
InputStream inputStream = (InputStream) _invocation.getArguments()[0];
|
||||
String path = (String) _invocation.getArguments()[1];
|
||||
writeToFile(inputStream, path);
|
||||
return null;
|
||||
}).when(smbSession)
|
||||
.write(Mockito.any(InputStream.class), Mockito.anyString());
|
||||
|
||||
// when(smbSession.write(Mockito.any(byte[].class), Mockito.anyString())).thenReturn(null);
|
||||
// when(smbSession.write(Mockito.any(File.class), Mockito.anyString())).thenReturn(null);
|
||||
|
||||
doAnswer(_invocation -> {
|
||||
String path = (String) _invocation.getArguments()[0];
|
||||
return new File(path).mkdirs();
|
||||
}).when(smbSession).mkdir(Mockito.anyString());
|
||||
|
||||
doAnswer(_invocation -> {
|
||||
String pathFrom = (String) _invocation.getArguments()[0];
|
||||
String pathTo = (String) _invocation.getArguments()[1];
|
||||
new File(pathFrom).renameTo(new File(pathTo));
|
||||
return null;
|
||||
}).when(smbSession)
|
||||
.rename(Mockito.anyString(), Mockito.anyString());
|
||||
|
||||
doNothing().when(smbSession).close();
|
||||
when(smbSession.isOpen()).thenReturn(true);
|
||||
return smbSession;
|
||||
}
|
||||
catch (Exception _ex) {
|
||||
throw new RuntimeException("Failed to create mock session.", _ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.smb.session;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import jcifs.DialectVersion;
|
||||
import jcifs.smb.SmbFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Gregory Bragg
|
||||
*
|
||||
*/
|
||||
public class SmbSessionTests {
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithBackSlash1() throws IOException {
|
||||
System.setProperty("file.separator", "\\");
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\");
|
||||
assertThat("smb://myshare/blubba/").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithBackSlash2() throws IOException {
|
||||
System.setProperty("file.separator", "\\");
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared\\");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\");
|
||||
assertThat("smb://myshare/blubba/").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithBackSlash3() throws IOException {
|
||||
System.setProperty("file.separator", "\\");
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared\\");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("..\\another");
|
||||
assertThat("smb://myshare:445/another").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithBackSlash4() throws IOException {
|
||||
System.setProperty("file.separator", "/");
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\");
|
||||
assertThat("smb://myshare/blubba/").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithMissingTrailingSlash1() throws IOException {
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba");
|
||||
assertThat("smb://myshare/blubba").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithMissingTrailingSlash2() throws IOException {
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject(".");
|
||||
assertThat("smb://myshare:445/shared/").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithMissingTrailingSlash3() throws IOException {
|
||||
SmbConfig config = new SmbConfig();
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
SmbShare smbShare = new SmbShare(config);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("../anotherShare");
|
||||
assertThat("smb://myshare:445/anotherShare").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithSmb3Versions1() throws IOException {
|
||||
Properties props = new Properties();
|
||||
SmbConfig config = new SmbConfig();
|
||||
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
config.setSmbMinVersion(DialectVersion.SMB300);
|
||||
config.setSmbMaxVersion(DialectVersion.SMB311);
|
||||
|
||||
props.setProperty("jcifs.smb.client.minVersion", config.getSmbMinVersion().name());
|
||||
props.setProperty("jcifs.smb.client.maxVersion", config.getSmbMaxVersion().name());
|
||||
|
||||
SmbShare smbShare = new SmbShare(config, props);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba");
|
||||
assertThat("smb://myshare/blubba").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithSmb3Versions2() throws IOException {
|
||||
Properties props = new Properties();
|
||||
SmbConfig config = new SmbConfig();
|
||||
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
config.setSmbMinVersion(DialectVersion.SMB302);
|
||||
config.setSmbMaxVersion(DialectVersion.SMB311);
|
||||
|
||||
props.setProperty("jcifs.smb.client.minVersion", config.getSmbMinVersion().name());
|
||||
props.setProperty("jcifs.smb.client.maxVersion", config.getSmbMaxVersion().name());
|
||||
|
||||
SmbShare smbShare = new SmbShare(config, props);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba");
|
||||
assertThat("smb://myshare/blubba").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmbFileObjectWithSmb3Versions3() throws IOException {
|
||||
Properties props = new Properties();
|
||||
SmbConfig config = new SmbConfig();
|
||||
|
||||
config.setHost("myshare");
|
||||
config.setPort(445);
|
||||
config.setShareAndDir("shared/");
|
||||
config.setSmbMinVersion(DialectVersion.SMB311);
|
||||
config.setSmbMaxVersion(DialectVersion.SMB311);
|
||||
|
||||
props.setProperty("jcifs.smb.client.minVersion", config.getSmbMinVersion().name());
|
||||
props.setProperty("jcifs.smb.client.maxVersion", config.getSmbMaxVersion().name());
|
||||
|
||||
SmbShare smbShare = new SmbShare(config, props);
|
||||
SmbSession smbSession = new SmbSession(smbShare);
|
||||
|
||||
SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba");
|
||||
assertThat("smb://myshare/blubba").isEqualTo(smbFile.getPath());
|
||||
smbSession.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?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:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-smb="http://www.springframework.org/schema/integration/smb"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/smb https://www.springframework.org/schema/integration/smb/spring-integration-smb.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:property-placeholder />
|
||||
|
||||
<!--
|
||||
smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?[param=value[param2=value2[...]]]
|
||||
-->
|
||||
<bean id="smbSession" class="org.springframework.integration.smb.session.SmbSessionFactory">
|
||||
<property name="host" value="${host}"/>
|
||||
<property name="username" value="${username}"/>
|
||||
<property name="password" value="${password}"/>
|
||||
<property name="shareAndDir" value="${shareAndDir}"/>
|
||||
</bean>
|
||||
|
||||
<int-smb:inbound-channel-adapter local-directory="target/smb-transfer-work"
|
||||
session-factory="smbSession" remote-directory="."
|
||||
auto-create-local-directory="true" delete-remote-files="false"
|
||||
channel="inboundChannel">
|
||||
<int:poller fixed-rate="10000" max-messages-per-poll="1"/>
|
||||
</int-smb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="inboundChannel">
|
||||
<int:interceptors>
|
||||
<int:wire-tap channel="loggit"/>
|
||||
</int:interceptors>
|
||||
</int:channel>
|
||||
|
||||
<int:logging-channel-adapter id="loggit" level="INFO"
|
||||
logger-name="org.springframework.integration.samples.smb"
|
||||
expression="'File Name: ' + payload.name + '(' + payload.length() + ')'"/>
|
||||
<int-file:outbound-channel-adapter channel="inboundChannel" directory="target/smb-out"/>
|
||||
</beans>
|
||||
15
spring-integration-smb/src/test/resources/log4j2-test.xml
Normal file
15
spring-integration-smb/src/test/resources/log4j2-test.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d %p [%t] [%c] - %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.springframework.integration" level="warn"/>
|
||||
<Logger name="org.springframework.integration.smb" level="info"/>
|
||||
<Root level="warn">
|
||||
<AppenderRef ref="STDOUT" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user