View Javadoc
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.commons.vfs2;
18  
19  import java.util.Iterator;
20  
21  import org.junit.AfterClass;
22  import org.junit.Assert;
23  import org.junit.BeforeClass;
24  import org.junit.Test;
25  
26  /**
27   * Tests {@link FileObject}s with iterators its implementation the {@link Iterable} interface to allow a FileObject to
28   * be the target of the Java 5 "foreach" statement.
29   */
30  public class FileIteratorTest {
31  
32      private static FileObject BaseFolder;
33  
34      private final static int FileCount = 14;
35  
36      /**
37       * Creates a RAM FS.
38       *
39       * @throws Exception
40       */
41      @BeforeClass
42      public static void setUpClass() throws Exception {
43          BaseFolder = VFS.getManager().resolveFile("ram://" + FileIteratorTest.class.getName());
44          BaseFolder.deleteAll();
45          BaseFolder.resolveFile("a.htm").createFile();
46          BaseFolder.resolveFile("a.html").createFile();
47          BaseFolder.resolveFile("a.xhtml").createFile();
48          BaseFolder.resolveFile("b.htm").createFile();
49          BaseFolder.resolveFile("b.html").createFile();
50          BaseFolder.resolveFile("b.xhtml").createFile();
51          BaseFolder.resolveFile("c.htm").createFile();
52          BaseFolder.resolveFile("c.html").createFile();
53          BaseFolder.resolveFile("c.xhtml").createFile();
54          BaseFolder.resolveFile("subdir1").createFolder();
55          BaseFolder.resolveFile("subdir1/subfile1.txt").createFile();
56          BaseFolder.resolveFile("subdir2").createFolder();
57          BaseFolder.resolveFile("subdir2/subfile1.txt").createFile();
58      }
59  
60      /**
61       * Deletes RAM FS files.
62       *
63       * @throws Exception
64       */
65      @AfterClass
66      public static void tearDownClass() throws Exception {
67          if (BaseFolder != null) {
68              BaseFolder.deleteAll();
69          }
70      }
71  
72      @Test
73      public void testIterator() throws FileSystemException {
74          final FileObject[] findFiles = BaseFolder.findFiles(Selectors.SELECT_ALL);
75          Assert.assertEquals(FileCount, findFiles.length);
76          final FileObject[] listFiles = BaseFolder.getChildren();
77          Assert.assertTrue(FileCount > listFiles.length);
78          int i = 0;
79          for (final FileObject actualFile : BaseFolder) {
80              final FileObject expectedFile = findFiles[i];
81              Assert.assertEquals(expectedFile, actualFile);
82              i++;
83          }
84          final Iterator<FileObject> iter = BaseFolder.iterator();
85          i = 0;
86          while (iter.hasNext()) {
87              final FileObject expectedFile = findFiles[i];
88              Assert.assertEquals(expectedFile, iter.next());
89              i++;
90          }
91      }
92  }