Advertisement
Guest User

Untitled

a guest
Dec 21st, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.camel.component.file;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.TreeSet;
  24.  
  25. import org.apache.camel.ContextTestSupport;
  26. import org.apache.camel.Exchange;
  27. import org.apache.camel.builder.RouteBuilder;
  28. import org.apache.camel.component.mock.MockEndpoint;
  29. import org.apache.camel.impl.JndiRegistry;
  30.  
  31. /**
  32.  * Unit test for the file filter option using directories
  33.  */
  34. public class FileConsumerFileInDirectoryFilterTest extends ContextTestSupport {
  35.  
  36.     private final String fileUrl = "file://target/directoryfilter/?recursive=true&filter=#myFilter";
  37.     private final Set<String> names = new TreeSet<String>();
  38.  
  39.     @Override
  40.     protected JndiRegistry createRegistry() throws Exception {
  41.         JndiRegistry jndi = super.createRegistry();
  42.         jndi.bind("myFilter", new MyDirectoryFilter<Object>());
  43.         return jndi;
  44.     }
  45.  
  46.     @Override
  47.     protected void setUp() throws Exception {
  48.         deleteDirectory("target/directoryfilter");
  49.         super.setUp();
  50.     }
  51.  
  52.     public void testFilterFilesInsideDirectory() throws Exception {
  53.         MockEndpoint mock = getMockEndpoint("mock:result");
  54.         mock.expectedMessageCount(2);
  55.         mock.expectedBodiesReceived("Pass1","Pass2");
  56.  
  57.         template.sendBodyAndHeader("file:target/directoryfilter/dir/", "Pass1",
  58.                 Exchange.FILE_NAME, "hello1.txt");
  59.  
  60.         template.sendBodyAndHeader("file:target/directoryfilter/dir/", "Pass2",
  61.                 Exchange.FILE_NAME, "hello2.txt");
  62.  
  63.         template.sendBodyAndHeader("file:target/directoryfilter/dir/", "This is a file to be skipped",
  64.                 Exchange.FILE_NAME, "skipme.txt");
  65.        
  66.         //Thread.sleep(1000000000);
  67.         mock.assertIsSatisfied();
  68.  
  69.         // check names
  70.         assertEquals(4, names.size());
  71.         // copy to list so its easier to index
  72.         List<String> list = new ArrayList<String>(names);
  73.         Collections.sort(list);
  74.  
  75.         assertEquals("dir", list.get(0));
  76.         // windows or unix paths
  77.         assertTrue(list.get(0), list.get(1).equals("dir/hello1.txt") || list.get(1).equals("dir\\pass1.txt"));
  78.         assertTrue(list.get(1), list.get(2).equals("dir/hello2.txt") || list.get(2).equals("dir\\pass2.txt"));
  79.         assertTrue(list.get(2), list.get(3).equals("dir/skipme.txt") || list.get(2).equals("dir\\skipme.txt"));
  80.  
  81.     }
  82.  
  83.     protected RouteBuilder createRouteBuilder() throws Exception {
  84.         return new RouteBuilder() {
  85.             public void configure() throws Exception {
  86.                 from(fileUrl).convertBodyTo(String.class).to("mock:result");
  87.             }
  88.         };
  89.     }
  90.  
  91.     // START SNIPPET: e1
  92.     public class MyDirectoryFilter<T> implements GenericFileFilter<T> {
  93.  
  94.         public boolean accept(GenericFile<T> file) {
  95.             names.add(file.getFileName());
  96.            
  97. //          if (file.isDirectory())
  98. //                  return true;
  99.            
  100.             //return file.getFileName().contains("hello"); Works only if checking directory previously
  101.             return !file.getFileName().contains("skip"); //works always
  102.         }
  103.  
  104.     }
  105.     // END SNIPPET: e1
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement