-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FileInboundChannelAdapterParserTests.java
149 lines (126 loc) · 5.35 KB
/
FileInboundChannelAdapterParserTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2002-2020 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.file.config;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.PriorityBlockingQueue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.file.DefaultDirectoryScanner;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.IgnoreHiddenFileListFilter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Iwein Fuld
* @author Mark Fisher
* @author Gary Russell
* @author Gunnar Hillert
* @author Artem Bilan
*/
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
@Autowired
@Qualifier("inputDirPoller.adapter.source")
private FileReadingMessageSource inputDirPollerSource;
@Autowired
@Qualifier("inboundWithJustFilter.adapter.source")
private FileReadingMessageSource inboundWithJustFilterSource;
@Autowired
private FileListFilter<File> filter;
private DirectFieldAccessor accessor;
@BeforeEach
public void init() {
this.accessor = new DirectFieldAccessor(inputDirPollerSource);
}
@Test
public void channelName() {
AbstractMessageChannel channel = context.getBean("inputDirPoller", AbstractMessageChannel.class);
assertThat(channel.getComponentName()).as("Channel should be available under specified id")
.isEqualTo("inputDirPoller");
}
@Test
public void justFilter() {
Iterator<?> filterIterator = TestUtils
.getPropertyValue(this.inboundWithJustFilterSource, "scanner.filter.fileFilters", Set.class).iterator();
assertThat(filterIterator.next()).isInstanceOf(IgnoreHiddenFileListFilter.class);
assertThat(filterIterator.next()).isSameAs(this.filter);
}
@Test
public void inputDirectory() {
File expected = new File(System.getProperty("java.io.tmpdir"));
File actual = (File) this.accessor.getPropertyValue("directory");
assertThat(actual).as("'directory' should be set").isEqualTo(expected);
assertThat(this.accessor.getPropertyValue("scanEachPoll")).isEqualTo(Boolean.TRUE);
assertThat(this.inputDirPollerSource.getComponentName()).isEqualTo("inputDirPoller.adapter.source");
}
@Test
public void filter() {
DefaultDirectoryScanner scanner = (DefaultDirectoryScanner) accessor.getPropertyValue("scanner");
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(scanner);
Object filter = scannerAccessor.getPropertyValue("filter");
assertThat(filter instanceof AcceptOnceFileListFilter)
.as("'filter' should be set and be of instance AcceptOnceFileListFilter but got "
+ filter.getClass().getSimpleName()).isTrue();
assertThat(scanner.getClass().getName()).contains("FileReadingMessageSource$WatchServiceDirectoryScanner");
FileReadingMessageSource.WatchEventType[] watchEvents =
(FileReadingMessageSource.WatchEventType[]) this.accessor.getPropertyValue("watchEvents");
assertThat(watchEvents.length).isEqualTo(2);
for (FileReadingMessageSource.WatchEventType watchEvent : watchEvents) {
assertThat(watchEvent).isNotEqualTo(FileReadingMessageSource.WatchEventType.CREATE);
assertThat(watchEvent)
.isIn(FileReadingMessageSource.WatchEventType.MODIFY,
FileReadingMessageSource.WatchEventType.DELETE);
}
}
@Test
public void comparator() {
Object priorityQueue = accessor.getPropertyValue("toBeReceived");
assertThat(priorityQueue).isInstanceOf(PriorityBlockingQueue.class);
Object expected = context.getBean("testComparator");
DirectFieldAccessor queueAccessor = new DirectFieldAccessor(priorityQueue);
Object innerQueue = queueAccessor.getPropertyValue("q");
Object actual;
if (innerQueue != null) {
actual = new DirectFieldAccessor(innerQueue).getPropertyValue("comparator");
}
else {
// probably running under JDK 7
actual = queueAccessor.getPropertyValue("comparator");
}
assertThat(actual).as("comparator reference not set, ").isSameAs(expected);
}
static class TestComparator implements Comparator<File> {
@Override
public int compare(File f1, File f2) {
return 0;
}
}
}