377N/A/*
377N/A * CDDL HEADER START
377N/A *
377N/A * The contents of this file are subject to the terms of the
377N/A * Common Development and Distribution License (the "License").
377N/A * You may not use this file except in compliance with the License.
377N/A *
377N/A * See LICENSE.txt included in this distribution for the specific
377N/A * language governing permissions and limitations under the License.
377N/A *
377N/A * When distributing Covered Code, include this CDDL HEADER in each
377N/A * file and include the License file at LICENSE.txt.
377N/A * If applicable, add the following below this CDDL HEADER, with the
377N/A * fields enclosed by brackets "[]" replaced with your own identifying
377N/A * information: Portions Copyright [yyyy] [name of copyright owner]
377N/A *
377N/A * CDDL HEADER END
377N/A */
377N/A
377N/A/*
377N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
377N/A * Use is subject to license terms.
377N/A */
377N/Apackage org.opensolaris.opengrok.util;
377N/A
377N/Aimport java.io.File;
377N/Aimport java.io.FileOutputStream;
377N/Aimport java.io.IOException;
377N/Aimport java.io.InputStream;
377N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
377N/Aimport static org.junit.Assert.*;
377N/A
377N/A/**
377N/A * A source repository to be used during a test
377N/A *
377N/A * @author Trond Norbye
377N/A */
377N/Apublic class TestRepository {
377N/A
377N/A private File sourceRoot;
377N/A private File dataRoot;
377N/A
1470N/A /**
1470N/A * Create a temporary repository from the given bundle.
1470N/A * @param inputBundle an archived test repository
1470N/A * @throws IOException
1470N/A */
377N/A public void create(InputStream inputBundle) throws IOException {
377N/A File sourceBundle = null;
377N/A try {
496N/A sourceRoot = FileUtilities.createTemporaryDirectory("source");
496N/A dataRoot = FileUtilities.createTemporaryDirectory("data");
377N/A sourceBundle = File.createTempFile("srcbundle", ".zip");
377N/A
377N/A if (sourceBundle.exists()) {
377N/A assertTrue(sourceBundle.delete());
377N/A }
377N/A
377N/A assertNotNull(inputBundle);
377N/A FileOutputStream out = new FileOutputStream(sourceBundle);
377N/A FileUtilities.copyFile(inputBundle, out);
377N/A out.close();
377N/A FileUtilities.extractArchive(sourceBundle, sourceRoot);
1470N/A RuntimeEnvironment.getConfig().setSourceRoot(sourceRoot.getAbsolutePath());
1470N/A RuntimeEnvironment.getConfig().setDataRoot(dataRoot.getAbsolutePath());
377N/A } finally {
377N/A if (sourceBundle != null) {
377N/A sourceBundle.delete();
377N/A }
377N/A }
377N/A }
377N/A
1470N/A /**
1470N/A * Delete the repository's source root.
1470N/A */
377N/A public void destroy() {
377N/A if (sourceRoot != null) {
377N/A FileUtilities.removeDirs(sourceRoot);
377N/A }
480N/A purgeData();
480N/A }
480N/A
1470N/A /**
1470N/A * Delete the repository's data root.
1470N/A */
480N/A public void purgeData() {
377N/A if (dataRoot != null) {
377N/A FileUtilities.removeDirs(dataRoot);
377N/A }
377N/A }
377N/A
1470N/A /**
1470N/A * Get the absolute path of the repository's source root.
1470N/A * @return the repository's source root.
1470N/A */
489N/A public String getSourceRoot() {
489N/A return sourceRoot.getAbsolutePath();
377N/A }
377N/A
1470N/A /**
1470N/A * Get the absolute path of the repository's data root.
1470N/A * @return the repository's data root.
1470N/A */
489N/A public String getDataRoot() {
489N/A return dataRoot.getAbsolutePath();
377N/A }
377N/A}