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  
18  package org.apache.commons.jci;
19  
20  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import junit.framework.TestCase;
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * 
31   * @author tcurdt
32   */
33  public abstract class AbstractTestCase extends TestCase {
34  
35      private final Log log = LogFactory.getLog(AbstractTestCase.class);
36      
37      protected File directory;
38  
39      @Override
40      protected void setUp() throws Exception {
41  
42          System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
43  
44          directory = createTempDirectory();
45          assertTrue(directory.exists());
46          assertTrue(directory.isDirectory());
47      }
48      
49      
50      protected File createDirectory( final String pName ) throws Exception {
51          final File newDirectory = new File(directory, pName);
52          assertTrue(newDirectory.mkdir());
53          assertTrue(newDirectory.exists());
54          assertTrue(newDirectory.isDirectory());
55          return newDirectory;
56      }
57      
58      protected File writeFile( final String pName, final byte[] pData ) throws Exception {
59          final File file = new File(directory, pName);
60          final File parent = file.getParentFile();
61          if (!parent.mkdirs() && !parent.isDirectory()) {
62              throw new IOException("could not create" + parent);
63          }
64  
65          log.debug("writing file " + pName + " (" + pData.length + " bytes)");
66          
67          final FileOutputStream os = new FileOutputStream(file);
68          os.write(pData);
69          os.close();
70          
71          assertTrue(file.exists());
72          assertTrue(file.isFile());
73          
74          return file;
75      }
76  
77      protected File writeFile( final String pName, final String pText ) throws Exception {
78          final File file = new File(directory, pName);
79          final File parent = file.getParentFile();
80          if (!parent.mkdirs() && !parent.isDirectory()) {
81              throw new IOException("could not create" + parent);
82          }
83          log.debug("writing " + file);
84          final FileWriter writer = new FileWriter(file);
85          writer.write(pText);
86          writer.close();
87          
88          assertTrue(file.exists());
89          assertTrue(file.isFile());
90          
91          return file;
92      }
93      
94      protected void delay() {
95          try {
96              Thread.sleep(1500);
97          } catch (final InterruptedException e) {
98          }
99      }
100     
101     protected File createTempDirectory() throws IOException {
102         final File tempFile = File.createTempFile("jci", null);
103         
104         if (!tempFile.delete()) {
105             throw new IOException();
106         }
107         
108         if (!tempFile.mkdir()) {
109             throw new IOException();
110         }
111         
112         return tempFile;         
113     }
114 
115 
116     @Override
117     protected void tearDown() throws Exception {
118         FileUtils.deleteDirectory(directory);
119     }
120 }