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: protected void setUp() throws Exception {
040:
041: System.setProperty("org.apache.commons.logging.Log",
042: "org.apache.commons.logging.impl.SimpleLog");
043:
044: directory = createTempDirectory();
045: assertTrue(directory.exists());
046: assertTrue(directory.isDirectory());
047: }
048:
049: protected File createDirectory(final String pName) throws Exception {
050: final File newDirectory = new File(directory, pName);
051: assertTrue(newDirectory.mkdir());
052: assertTrue(newDirectory.exists());
053: assertTrue(newDirectory.isDirectory());
054: return newDirectory;
055: }
056:
057: protected File writeFile(final String pName, final byte[] pData)
058: throws Exception {
059: final File file = new File(directory, pName);
060: final File parent = file.getParentFile();
061: if (!parent.exists()) {
062: if (!parent.mkdirs()) {
063: throw new IOException("could not create" + parent);
064: }
065: }
066:
067: log.debug("writing file " + pName + " (" + pData.length
068: + " bytes)");
069:
070: final FileOutputStream os = new FileOutputStream(file);
071: os.write(pData);
072: os.close();
073:
074: assertTrue(file.exists());
075: assertTrue(file.isFile());
076:
077: return file;
078: }
079:
080: protected File writeFile(final String pName, final String pText)
081: throws Exception {
082: final File file = new File(directory, pName);
083: final File parent = file.getParentFile();
084: if (!parent.exists()) {
085: if (!parent.mkdirs()) {
086: throw new IOException("could not create" + parent);
087: }
088: }
089: log.debug("writing " + file);
090: final FileWriter writer = new FileWriter(file);
091: writer.write(pText);
092: writer.close();
093:
094: assertTrue(file.exists());
095: assertTrue(file.isFile());
096:
097: return file;
098: }
099:
100: protected void delay() {
101: try {
102: Thread.sleep(1500);
103: } catch (final InterruptedException e) {
104: }
105: }
106:
107: protected File createTempDirectory() throws IOException {
108: final File tempFile = File.createTempFile("jci", null);
109:
110: if (!tempFile.delete()) {
111: throw new IOException();
112: }
113:
114: if (!tempFile.mkdir()) {
115: throw new IOException();
116: }
117:
118: return tempFile;
119: }
120:
121: protected void tearDown() throws Exception {
122: FileUtils.deleteDirectory(directory);
123: }
124: }
|