de.fhg.iais.commons.stream.OutputStreamTestBase.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.commons.stream.OutputStreamTestBase.java

Source

package de.fhg.iais.commons.stream;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;

import de.fhg.iais.commons.annotation.OnlyCalledFromTests;

@OnlyCalledFromTests
public class OutputStreamTestBase {
    private static File DIRECTORY = new File("target/tmp/OutputStreamTest");
    protected static File FILE = new File(DIRECTORY, "testfile.tmp");
    protected static String FILE_STRING = FILE.getPath();
    protected static byte[] CONTENT = new byte[] { 1, 2, 3 };
    protected static byte[] CONTENT2 = new byte[] { 4, 5, 6 };

    @BeforeClass
    public static void setup() {
        if (!DIRECTORY.mkdirs() && !DIRECTORY.isDirectory()) {
            throw new RuntimeException("Directory \"" + DIRECTORY + "\" can't be created");
        }
    }

    @Before
    @After
    public void removeFile() {
        FILE.delete();
        Assert.assertTrue("Testfile exists and could not be deleted", !FILE.exists());
    }

    protected void checkContentAndRemoveFile(byte[] content) throws IOException, FileNotFoundException {
        checkContent(content);
        removeFile();
    }

    protected void checkContent(byte[] content) throws FileNotFoundException, IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(FILE));
        Assert.assertArrayEquals(content, IOUtils.toByteArray(is));
        IOUtils.closeQuietly(is);
    }

    @AfterClass
    public static void cleanup() throws IOException {
        FileUtils.deleteDirectory(DIRECTORY);
    }
}