263N/A/*
263N/A * CDDL HEADER START
263N/A *
263N/A * The contents of this file are subject to the terms of the
1024N/A * Common Development and Distribution License (the "License").
263N/A * You may not use this file except in compliance with the License.
263N/A *
263N/A * See LICENSE.txt included in this distribution for the specific
263N/A * language governing permissions and limitations under the License.
263N/A *
263N/A * When distributing Covered Code, include this CDDL HEADER in each
263N/A * file and include the License file at LICENSE.txt.
263N/A * If applicable, add the following below this CDDL HEADER, with the
263N/A * fields enclosed by brackets "[]" replaced with your own identifying
263N/A * information: Portions Copyright [yyyy] [name of copyright owner]
263N/A *
263N/A * CDDL HEADER END
263N/A */
263N/A
263N/A/*
1024N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
263N/A * Use is subject to license terms.
263N/A */
1024N/A
263N/Apackage org.opensolaris.opengrok.history;
263N/A
263N/Aimport java.io.ByteArrayOutputStream;
263N/Aimport java.io.File;
263N/Aimport java.io.FileOutputStream;
263N/Aimport java.io.IOException;
263N/Aimport java.io.InputStream;
263N/Aimport java.util.zip.ZipEntry;
263N/Aimport java.util.zip.ZipInputStream;
263N/Aimport org.junit.After;
263N/Aimport org.junit.AfterClass;
263N/Aimport org.junit.Before;
263N/Aimport org.junit.BeforeClass;
263N/Aimport org.junit.Test;
263N/Aimport static org.junit.Assert.*;
263N/A
263N/A/**
263N/A * Test the SCCSget class
263N/A * @author Trond Norbye
263N/A */
263N/Apublic class SCCSgetTest {
263N/A
265N/A private static boolean haveSccs = true;
263N/A private File sccsfile;
263N/A private File sccsdir;
263N/A
263N/A public SCCSgetTest() {
263N/A }
263N/A
263N/A @BeforeClass
263N/A public static void setUpClass() throws Exception {
265N/A // Check to see if we have sccs..
265N/A Process p = null;
265N/A try {
265N/A p = Runtime.getRuntime().exec("sccs help help");
265N/A p.waitFor();
265N/A haveSccs = (p.exitValue() == 0);
265N/A } catch (Exception e) {
265N/A haveSccs = false;
265N/A } finally {
265N/A try {
265N/A if (p != null) {
265N/A p.destroy();
265N/A }
265N/A } catch (Exception e) {
265N/A
265N/A }
1064N/A }
1064N/A try {
1064N/A p = Runtime.getRuntime().exec("sccs --version");
1064N/A p.waitFor();
1064N/A haveSccs = (p.exitValue() == 0);
1064N/A } catch (Exception e) {
1064N/A haveSccs = false;
1064N/A } finally {
1064N/A try {
1064N/A if (p != null) {
1064N/A p.destroy();
1064N/A }
1064N/A } catch (Exception e) {
1064N/A
1064N/A }
265N/A }
263N/A }
263N/A
263N/A @AfterClass
263N/A public static void tearDownClass() throws Exception {
263N/A }
263N/A
263N/A @Before
367N/A public void setUp() throws IOException {
265N/A if (!haveSccs) {
265N/A return;
265N/A }
263N/A try {
263N/A sccsdir = File.createTempFile("s.test", "sccs");
263N/A sccsdir.delete();
263N/A if (!sccsdir.mkdirs()) {
263N/A fail("Failed to set up the test-directory");
263N/A }
263N/A sccsfile = new File(sccsdir, "s.note.txt");
1044N/A InputStream in = getClass().getResourceAsStream("s.note.txt");
263N/A FileOutputStream out = new FileOutputStream(sccsfile);
263N/A byte[] buffer = new byte[8192];
263N/A int nr;
263N/A
263N/A while ((nr = in.read(buffer, 0, buffer.length)) != -1) {
263N/A out.write(buffer, 0, nr);
263N/A }
263N/A out.flush();
263N/A in.close();
263N/A out.close();
263N/A } catch (IOException ex) {
263N/A if (sccsfile != null) {
263N/A sccsfile.delete();
263N/A sccsdir.delete();
263N/A }
367N/A throw ex;
263N/A }
263N/A }
263N/A
263N/A @After
263N/A public void tearDown() {
263N/A if (sccsfile != null) {
263N/A sccsfile.delete();
263N/A }
265N/A
263N/A if (sccsdir != null) {
263N/A sccsdir.delete();
263N/A }
263N/A }
263N/A
263N/A private String readInput(InputStream in) throws IOException {
263N/A ByteArrayOutputStream out = new ByteArrayOutputStream();
263N/A byte[] buffer = new byte[32 * 1024];
263N/A int len;
263N/A
263N/A while ((len = in.read(buffer)) != -1) {
263N/A if (len > 0) {
263N/A out.write(buffer, 0, len);
263N/A }
263N/A }
265N/A
263N/A return out.toString();
263N/A }
263N/A
263N/A /**
263N/A * Test of getRevision method, of class SCCSget.
263N/A */
263N/A @Test
263N/A public void getRevision() throws Exception {
265N/A if (!haveSccs) {
265N/A System.out.println("sccs not available. Skipping test");
265N/A return;
265N/A }
1044N/A ZipInputStream zstream = new ZipInputStream(getClass().getResourceAsStream("sccs-revisions.zip"));
263N/A ZipEntry entry;
265N/A
263N/A while ((entry = zstream.getNextEntry()) != null) {
263N/A String expected = readInput(zstream);
285N/A InputStream sccs = SCCSget.getRevision("sccs",sccsfile, entry.getName());
263N/A String got = readInput(sccs);
263N/A sccs.close();
265N/A zstream.closeEntry();
263N/A assertEquals(expected, got);
263N/A }
263N/A zstream.close();
263N/A }
263N/A}