View Javadoc

1   package org.apache.torque.generator.control;
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.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.io.FileUtils;
31  import org.apache.torque.generator.BaseTest;
32  import org.apache.torque.generator.configuration.UnitDescriptor;
33  import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
34  import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
35  import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
36  import org.junit.Test;
37  
38  /**
39   * Tests whether the output encoding handling is correct.
40   */
41  public class OutputEncodingTest extends BaseTest
42  {
43      @Test
44      public void testOutputEncoding() throws Exception
45      {
46          File targetDir = new File("target/test/outputEncoding");
47          FileUtils.deleteDirectory(targetDir);
48          Controller controller = new Controller();
49          List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>();
50          CustomProjectPaths projectPaths = new CustomProjectPaths(
51                  new Maven2DirectoryProjectPaths(
52                          new File("src/test/outputEncoding")));
53          projectPaths.setOutputDirectory(null, targetDir);
54          unitDescriptors.add(new UnitDescriptor(
55                  UnitDescriptor.Packaging.DIRECTORY,
56                  projectPaths,
57                  new DefaultTorqueGeneratorPaths()));
58          controller.run(unitDescriptors);
59  
60          assertTrue(targetDir.exists());
61          // We cannot check the default encoding file content because
62          // there are default charsets where the generated characters
63          // do not exist (e.G. US 7-bit ASCII)
64          // -> this will produce error. So we check only file existence.
65          assertTrue(new File(targetDir, "defaultEncoding.txt").exists());
66          checkFile(
67                  new File(targetDir, "iso-8859-1.txt"),
68                  "iso-8859-1");
69          checkFile(
70                  new File(targetDir, "utf8.txt"),
71                  "utf-8");
72      }
73  
74      private void checkFile(File file, String encoding)
75              throws IOException
76      {
77          assertTrue(file.exists());
78          String content = FileUtils.readFileToString(file, encoding);
79          assertEquals("Test Outlet output; foo=öäü; bar=ÖÄÜ", content);
80      }
81  }