【发布时间】:2014-05-13 10:06:16
【问题描述】:
我正在尝试为 Mule 流编写一个测试,该测试将涉及将文件放在某个位置,等待它由我的流处理并比较输出以查看它是否已正确转换。我的流程如下所示:
<flow name="mainFlow" processingStrategy="synchronous">
<file:inbound-endpoint name="fileIn" path="${inboundPath}">
<file:filename-regex-filter pattern="myFile.csv" caseSensitive="true"/>
</file:inbound-endpoint>
...
<file:outbound-endpoint path="${outboundPath}" outputPattern="out.csv"/>
</flow>
有没有一种方法可以访问我的测试类中的 inboundPath 和 outboundPath Mule 属性,以便我可以删除文件并在正确的位置等待输出?
我正在使用的测试类是:
public class MappingTest extends BaseFileToFileFunctionalTest {
@Override
protected String getConfigResources() {
return "mappingtest.xml";
}
@Test
public void testMapping() throws Exception {
dropInputFileIntoPlace("myFile.csv");
waitForOutputFile("out.csv", 100);
assertEquals(getExpectedOutputFile("expected-out.csv"), getActualOutputFile("out.csv"));
}
}
扩展了这个类:
public abstract class BaseFileToFileFunctionalTest extends FunctionalTestCase {
private static final File INPUT_DIR = new File("/tmp/muletest/input");
private static final File OUTPUT_DIR = new File("/tmp/muletest/output");
private static final Charset CHARSET = Charsets.UTF_8;
@Before
public void setup() {
new File("/tmp/muletest/input").mkdirs();
new File("/tmp/muletest/output").mkdirs();
empty(INPUT_DIR);
empty(OUTPUT_DIR);
}
private void empty(File inputDir) {
for (File file : inputDir.listFiles()) {
file.delete();
}
}
protected File waitForOutputFile(String expectedFileName, int retryAttempts) throws InterruptedException {
boolean polling = true;
int attemptsRemaining = retryAttempts;
File outputFile = new File(OUTPUT_DIR, expectedFileName);
while (polling) {
Thread.sleep(100L);
if (outputFile.exists()) {
polling = false;
}
if (attemptsRemaining == 0) {
VisibleAssertions.fail("Output file did not appear within expected time");
}
attemptsRemaining--;
}
outputFile.deleteOnExit();
return outputFile;
}
protected void dropInputFileIntoPlace(String inputFileResourceName) throws IOException {
File inputFile = new File(INPUT_DIR, inputFileResourceName);
Files.copy(Resources.newInputStreamSupplier(Resources.getResource(inputFileResourceName)), inputFile);
inputFile.deleteOnExit();
}
protected String getActualOutputFile(String outputFileName) throws IOException {
File outputFile = new File(OUTPUT_DIR, outputFileName);
return Files.toString(outputFile, CHARSET);
}
protected String getExpectedOutputFile(String resourceName) throws IOException {
return Resources.toString(Resources.getResource(resourceName), CHARSET);
}
}
如您所见,我目前正在创建临时输入/输出目录。如果可能,我想让这部分从 Mule 属性中读取?提前致谢。
【问题讨论】:
-
加载配置文件时,您也应该加载这些属性文件。因此它们应该在您的流入站和出站端点中可用。
-
所以我的问题是如何从 在我的 Java 类中访问这些?当我指定删除测试文件的位置以及轮询输出的位置时
-
添加测试用例类代码示例以了解您使用什么方法来加载流配置。这将有助于提供正确的解决方案
-
@user1760178,请查看更新后的问题
标签: java testing properties mule filepath