View Javadoc

1   package org.apache.torque.generator.source.stream;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.torque.generator.file.FileTestUtils.createSetFrom;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.File;
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.apache.torque.generator.configuration.ConfigurationException;
32  import org.apache.torque.generator.configuration.ConfigurationHandlers;
33  import org.apache.torque.generator.control.ControllerState;
34  import org.apache.torque.generator.file.Fileset;
35  import org.apache.torque.generator.source.Source;
36  import org.apache.torque.generator.source.SourceException;
37  import org.junit.Test;
38  
39  public class FileSourceProviderTest
40  {
41      private static File TEST_BASE_DIR = new File("src/test/file");
42  
43      @Test
44      public void testReadFiles()
45              throws ConfigurationException, SourceException
46      {
47          Fileset fileset = new Fileset(
48                  TEST_BASE_DIR,
49                  createSetFrom("*.properties"),
50                  null);
51          FileSourceProvider fileSourceProvider
52                  = new FileSourceProvider(null, fileset, false);
53          fileSourceProvider.init(
54                  new ConfigurationHandlers(),
55                  new ControllerState());
56  
57          // We read all sources and add the first property key to a hash set.
58          // As all the source files only have one key equal to the base filename
59          // we can make sure we have read the expected files correctly.
60          Set<String> resultKeys = new HashSet<String>();
61          {
62              assertTrue(fileSourceProvider.hasNext());
63              Source source = fileSourceProvider.next();
64              Object key = source.getRootElement().getChild("entry")
65                      .getAttribute("key");
66              resultKeys.add((String) key);
67          }
68          {
69              assertTrue(fileSourceProvider.hasNext());
70              Source source = fileSourceProvider.next();
71              Object key = source.getRootElement().getChild("entry")
72                      .getAttribute("key");
73              resultKeys.add((String) key);
74          }
75          assertFalse(fileSourceProvider.hasNext());
76          Set<String> expectedKeys = new HashSet<String>();
77          expectedKeys.add("1");
78          expectedKeys.add("11");
79          assertEquals(expectedKeys, resultKeys);
80      }
81  
82      @Test
83      public void testReadFilesCombineFiles()
84              throws ConfigurationException, SourceException
85      {
86          Fileset fileset = new Fileset(
87                  TEST_BASE_DIR,
88                  createSetFrom("*.properties"),
89                  null);
90          FileSourceProvider fileSourceProvider
91                  = new FileSourceProvider(null, fileset, true);
92          fileSourceProvider.init(
93                  new ConfigurationHandlers(),
94                  new ControllerState());
95  
96          // We read all sources and add the first property key to a hash set.
97          // As all the source files only have one key equal to the base filename
98          // we can make sure we have read the expected files correctly.
99          Set<String> resultKeys = new HashSet<String>();
100         assertTrue(fileSourceProvider.hasNext());
101         Source source = fileSourceProvider.next();
102 
103         assertEquals(2, source.getRootElement().getChildren("file").size());
104         Object key = source.getRootElement()
105                 .getChildren("file").get(0)
106                 .getChild("properties")
107                 .getChild("entry")
108                 .getAttribute("key");
109         resultKeys.add((String) key);
110         key = source.getRootElement()
111                 .getChildren("file").get(1)
112                 .getChild("properties")
113                 .getChild("entry")
114                 .getAttribute("key");
115         resultKeys.add((String) key);
116         Set<String> expectedKeys = new HashSet<String>();
117         expectedKeys.add("1");
118         expectedKeys.add("11");
119         assertEquals(expectedKeys, resultKeys);
120 
121         Set<String> resultPaths = new HashSet<String>();
122         String expectedPath = (String) source.getRootElement()
123                 .getChildren("file").get(0).getAttribute("path");
124         expectedPath = expectedPath.replace('\\', '/');
125         resultPaths.add(expectedPath);
126         expectedPath = (String) source.getRootElement()
127                 .getChildren("file").get(1).getAttribute("path");
128         expectedPath = expectedPath.replace('\\', '/');
129         resultPaths.add(expectedPath);
130         Set<String> expectedPaths = new HashSet<String>();
131         expectedPaths.add("src/test/file/./1.properties");
132         expectedPaths.add("src/test/file/./11.properties");
133         assertEquals(expectedPaths, resultPaths);
134 
135         assertFalse(fileSourceProvider.hasNext());
136     }
137 
138     @Test(expected = ConfigurationException.class)
139     public void testReadFilesBasedirNull()
140             throws ConfigurationException, SourceException
141     {
142         Fileset fileset = new Fileset(
143                 null,
144                 createSetFrom("*.properties"),
145                 null);
146         FileSourceProvider fileSourceProvider
147                 = new FileSourceProvider(null, fileset, false);
148         fileSourceProvider.init(
149                 new ConfigurationHandlers(),
150                 new ControllerState());
151     }
152 }