790N/A/*
790N/A * CDDL HEADER START
790N/A *
790N/A * The contents of this file are subject to the terms of the
790N/A * Common Development and Distribution License (the "License").
790N/A * You may not use this file except in compliance with the License.
790N/A *
790N/A * See LICENSE.txt included in this distribution for the specific
790N/A * language governing permissions and limitations under the License.
790N/A *
790N/A * When distributing Covered Code, include this CDDL HEADER in each
790N/A * file and include the License file at LICENSE.txt.
790N/A * If applicable, add the following below this CDDL HEADER, with the
790N/A * fields enclosed by brackets "[]" replaced with your own identifying
790N/A * information: Portions Copyright [yyyy] [name of copyright owner]
790N/A *
790N/A * CDDL HEADER END
790N/A */
790N/A
790N/A/*
967N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
790N/A * Use is subject to license terms.
790N/A */
790N/A
790N/Apackage org.opensolaris.opengrok.history;
790N/A
790N/Aimport java.io.File;
790N/Aimport java.io.IOException;
790N/Aimport java.util.List;
790N/Aimport org.junit.After;
790N/Aimport org.junit.Test;
1024N/Aimport org.opensolaris.opengrok.util.TestRepository;
790N/Aimport static org.junit.Assert.*;
790N/A
790N/A/**
790N/A * Tests for MercurialRepository.
790N/A */
790N/Apublic class MercurialRepositoryTest {
790N/A
790N/A /**
790N/A * Revision numbers present in the Mercurial test repository, in the
790N/A * order they are supposed to be returned from getHistory().
790N/A */
1238N/A private static final String[] REVISIONS = {
790N/A "2:585a1b3f2efb", "1:f24a5fd7a85d", "0:816b6279ae9c"
790N/A };
790N/A
790N/A private TestRepository repository;
790N/A
790N/A /**
790N/A * Set up a test repository. Should be called by the tests that need it.
790N/A * The test repository will be destroyed automatically when the test
790N/A * finishes.
790N/A */
790N/A private void setUpTestRepository() throws IOException {
790N/A repository = new TestRepository();
790N/A repository.create(getClass().getResourceAsStream("repositories.zip"));
790N/A }
790N/A
790N/A @After
790N/A public void tearDown() {
790N/A if (repository != null) {
790N/A repository.destroy();
790N/A repository = null;
790N/A }
790N/A }
790N/A
790N/A @Test
790N/A public void testGetHistory() throws Exception {
790N/A setUpTestRepository();
790N/A File root = new File(repository.getSourceRoot(), "mercurial");
790N/A MercurialRepository mr =
790N/A (MercurialRepository) RepositoryFactory.getRepository(root);
790N/A History hist = mr.getHistory(root);
790N/A List<HistoryEntry> entries = hist.getHistoryEntries();
790N/A assertEquals(REVISIONS.length, entries.size());
790N/A for (int i = 0; i < entries.size(); i++) {
790N/A HistoryEntry e = entries.get(i);
790N/A assertEquals(REVISIONS[i], e.getRevision());
790N/A assertNotNull(e.getAuthor());
790N/A assertNotNull(e.getDate());
790N/A assertNotNull(e.getFiles());
790N/A assertNotNull(e.getMessage());
790N/A }
790N/A }
790N/A
790N/A @Test
790N/A public void testGetHistoryPartial() throws Exception {
790N/A setUpTestRepository();
790N/A File root = new File(repository.getSourceRoot(), "mercurial");
790N/A MercurialRepository mr =
790N/A (MercurialRepository) RepositoryFactory.getRepository(root);
790N/A // Get all but the oldest revision
790N/A History hist = mr.getHistory(root, REVISIONS[REVISIONS.length - 1]);
790N/A List<HistoryEntry> entries = hist.getHistoryEntries();
790N/A assertEquals(REVISIONS.length - 1, entries.size());
790N/A for (int i = 0; i < entries.size(); i++) {
790N/A HistoryEntry e = entries.get(i);
790N/A assertEquals(REVISIONS[i], e.getRevision());
790N/A assertNotNull(e.getAuthor());
790N/A assertNotNull(e.getDate());
790N/A assertNotNull(e.getFiles());
790N/A assertNotNull(e.getMessage());
790N/A }
790N/A }
790N/A
791N/A /**
791N/A * Test that {@code getHistory()} throws an exception if the revision
791N/A * argument doesn't match any of the revisions in the history.
791N/A */
791N/A @Test
791N/A public void testGetHistoryWithNoSuchRevision() throws Exception {
791N/A setUpTestRepository();
791N/A File root = new File(repository.getSourceRoot(), "mercurial");
791N/A MercurialRepository mr =
791N/A (MercurialRepository) RepositoryFactory.getRepository(root);
791N/A
791N/A // Get the sequence number and the hash from one of the revisions.
791N/A String[] revisionParts = REVISIONS[1].split(":");
791N/A assertEquals(2, revisionParts.length);
791N/A int number = Integer.parseInt(revisionParts[0]);
791N/A String hash = revisionParts[1];
791N/A
791N/A // Construct a revision identifier that doesn't exist.
791N/A String constructedRevision = (number + 1) + ":" + hash;
791N/A try {
791N/A mr.getHistory(root, constructedRevision);
791N/A fail("getHistory() should have failed");
791N/A } catch (HistoryException he) {
791N/A String msg = he.getMessage();
967N/A if (msg != null && msg.contains("not found in the repository")) {
791N/A // expected exception, do nothing
791N/A } else {
791N/A // unexpected exception, rethrow it
791N/A throw he;
791N/A }
791N/A }
791N/A }
791N/A
790N/A}