Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.apache.camel.component.file;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Set;
- import java.util.TreeSet;
- import org.apache.camel.ContextTestSupport;
- import org.apache.camel.Exchange;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.component.mock.MockEndpoint;
- import org.apache.camel.impl.JndiRegistry;
- /**
- * Unit test for the file filter option using directories
- */
- public class FileConsumerFileInDirectoryFilterTest extends ContextTestSupport {
- private final String fileUrl = "file://target/directoryfilter/?recursive=true&filter=#myFilter";
- private final Set<String> names = new TreeSet<String>();
- @Override
- protected JndiRegistry createRegistry() throws Exception {
- JndiRegistry jndi = super.createRegistry();
- jndi.bind("myFilter", new MyDirectoryFilter<Object>());
- return jndi;
- }
- @Override
- protected void setUp() throws Exception {
- deleteDirectory("target/directoryfilter");
- super.setUp();
- }
- public void testFilterFilesInsideDirectory() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(2);
- mock.expectedBodiesReceived("Pass1","Pass2");
- template.sendBodyAndHeader("file:target/directoryfilter/dir/", "Pass1",
- Exchange.FILE_NAME, "hello1.txt");
- template.sendBodyAndHeader("file:target/directoryfilter/dir/", "Pass2",
- Exchange.FILE_NAME, "hello2.txt");
- template.sendBodyAndHeader("file:target/directoryfilter/dir/", "This is a file to be skipped",
- Exchange.FILE_NAME, "skipme.txt");
- //Thread.sleep(1000000000);
- mock.assertIsSatisfied();
- // check names
- assertEquals(4, names.size());
- // copy to list so its easier to index
- List<String> list = new ArrayList<String>(names);
- Collections.sort(list);
- assertEquals("dir", list.get(0));
- // windows or unix paths
- assertTrue(list.get(0), list.get(1).equals("dir/hello1.txt") || list.get(1).equals("dir\\pass1.txt"));
- assertTrue(list.get(1), list.get(2).equals("dir/hello2.txt") || list.get(2).equals("dir\\pass2.txt"));
- assertTrue(list.get(2), list.get(3).equals("dir/skipme.txt") || list.get(2).equals("dir\\skipme.txt"));
- }
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
- public void configure() throws Exception {
- from(fileUrl).convertBodyTo(String.class).to("mock:result");
- }
- };
- }
- // START SNIPPET: e1
- public class MyDirectoryFilter<T> implements GenericFileFilter<T> {
- public boolean accept(GenericFile<T> file) {
- names.add(file.getFileName());
- // if (file.isDirectory())
- // return true;
- //return file.getFileName().contains("hello"); Works only if checking directory previously
- return !file.getFileName().contains("skip"); //works always
- }
- }
- // END SNIPPET: e1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement