001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jci;
019    
020    import java.io.File;
021    import java.io.FileOutputStream;
022    import java.io.FileWriter;
023    import java.io.IOException;
024    import junit.framework.TestCase;
025    import org.apache.commons.io.FileUtils;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * 
031     * @author tcurdt
032     */
033    public abstract class AbstractTestCase extends TestCase {
034    
035        private final Log log = LogFactory.getLog(AbstractTestCase.class);
036        
037        protected File directory;
038    
039        @Override
040        protected void setUp() throws Exception {
041    
042            System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
043    
044            directory = createTempDirectory();
045            assertTrue(directory.exists());
046            assertTrue(directory.isDirectory());
047        }
048        
049        
050        protected File createDirectory( final String pName ) throws Exception {
051            final File newDirectory = new File(directory, pName);
052            assertTrue(newDirectory.mkdir());
053            assertTrue(newDirectory.exists());
054            assertTrue(newDirectory.isDirectory());
055            return newDirectory;
056        }
057        
058        protected File writeFile( final String pName, final byte[] pData ) throws Exception {
059            final File file = new File(directory, pName);
060            final File parent = file.getParentFile();
061            if (!parent.mkdirs() && !parent.isDirectory()) {
062                throw new IOException("could not create" + parent);
063            }
064    
065            log.debug("writing file " + pName + " (" + pData.length + " bytes)");
066            
067            final FileOutputStream os = new FileOutputStream(file);
068            os.write(pData);
069            os.close();
070            
071            assertTrue(file.exists());
072            assertTrue(file.isFile());
073            
074            return file;
075        }
076    
077        protected File writeFile( final String pName, final String pText ) throws Exception {
078            final File file = new File(directory, pName);
079            final File parent = file.getParentFile();
080            if (!parent.mkdirs() && !parent.isDirectory()) {
081                throw new IOException("could not create" + parent);
082            }
083            log.debug("writing " + file);
084            final FileWriter writer = new FileWriter(file);
085            writer.write(pText);
086            writer.close();
087            
088            assertTrue(file.exists());
089            assertTrue(file.isFile());
090            
091            return file;
092        }
093        
094        protected void delay() {
095            try {
096                Thread.sleep(1500);
097            } catch (final InterruptedException e) {
098            }
099        }
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    }