Log in Help
Print
Homegatewikicowtestintegrationgateversioningsvnkit 〉 SvnKitSpringTest.java
 
/*
 *  SvnKitSpringTest.java
 *  Copyright (c) 1998-2008, The University of Sheffield.
 *
 *  This code is from the GATE project (http://gate.ac.uk/) and is free
 *  software licenced under the GNU General Public License version 3. It is
 *  distributed without any warranty. For more details see COPYING.txt in the
 *  top level directory (or at http://gatewiki.sf.net/COPYING.txt).
 *  
 *  Hamish Cunningham, 9th Jan 2007
 */

package gate.versioning.svnkit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.codehaus.groovy.grails.commons.ApplicationHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNStatusType;
import org.tmatesoft.svn.examples.wc.WorkingCopy;


/**
 * SVNKit tests running from Spring.
 */
public class SvnKitSpringTest extends TestCase
{
  /** Spring bean factory / context. */
  ApplicationContext factory = ApplicationHolder.getApplication().getMainContext();

  /** Spring file system context, to find stuff in the test dir */
  FileSystemXmlApplicationContext fsContext = new
    FileSystemXmlApplicationContext(null, factory);

  /** The sandbox manager. */
  SandboxManager sMgr = (SandboxManager) factory.getBean("sandboxManager");

  /** Logger */
  static Logger log = Logger.getLogger("gate.versioning.svnkit");

  /** Create the test case */
  public SvnKitSpringTest(String testName) { super(testName); }

  // some constants
  String testDirName =
    "test/scratch/svn-kit-spring-test";
  String repDirName =
    "test/scratch/svn-kit-spring-test/test-svnrep";
  String importDirName =
    "test/scratch/svn-kit-spring-test/import-dir";
  String importFileName =
    "test/scratch/svn-kit-spring-test/import-dir/import-file.txt";
  String coDirName =
    "test/scratch/svn-kit-spring-test/working-copy";
  String importFileText = "This is an example file.";
  File testDirFile = new File(testDirName);
  File importDirFile = new File(importDirName);
  File coDirFile = new File(coDirName);

  /** Set up a repository, import something and check out a copy. */
  public void setUp() throws Exception {
    log.info("============= SvnKitSpringTest.setup() ======================");

    // delete any existing test directory
    log.info("deleting " + testDirName);
    if(testDirFile.exists()) gate.util.Files.rmdir(testDirFile);

    // create an SVN repository (also creates the test directory)
    log.info("creating rep " + repDirName);
    FileSystemResource repDirRes =
      (FileSystemResource) fsContext.getResource(repDirName);
    SVNURL repositoryURL = sMgr.createLocalRepository(repDirRes.getFile());
    SVNURL trunkURL = repositoryURL.appendPath("trunk", false);
    log.info("trunk is: " + trunkURL);

    // create an import dir and populate the repository with some stuff
    log.info("creating import dir and file " + importFileName);
    WorkingCopy.createLocalDir(
      importDirFile, new File[] { new File(importFileName) },
      new String[] { importFileText }
    );
    Sandbox tmpSandbox = new Sandbox("","",sMgr.getConfigDir());
    log.info("importing dir " + importDirName);
    SVNCommitInfo commitInfo = tmpSandbox.importDirectory(
      importDirFile, trunkURL, "test", true
    );

    // check out a copy
    Sandbox sandbox = new Sandbox("","",sMgr.getConfigDir());
    sandbox.checkout(trunkURL, SVNRevision.HEAD, coDirFile, true);

    log.info("=============================================================");
  } // setUp()

  /**
   * Test the API in the way the interactions with the other beans will work.
   */
  public void testVersioning() throws Exception
  {
    log.info("============= SvnKitSpringTest.springVersioning() ===========");

    boolean status;

    // ask spring for a resource representing the checked out working copy
    // from setUp()
    FileSystemResource sbRes =
      (FileSystemResource) fsContext.getResource(coDirName);
    assertTrue(coDirName + " is not a dir", sbRes.getFile().isDirectory());

    // isSandboxDir
    FileSystemResource tdRes =
      (FileSystemResource) fsContext.getResource(testDirName);
    status = sMgr.isSandboxDir(tdRes.getFile());
    assertTrue(testDirName + " is a sandbox but shouldn't be", ! status);
    status = sMgr.isSandboxDir(sbRes.getFile());
    assertTrue(coDirName + " is not a sandbox", status);

    // add the sandbox to the manager
    log.info("adding sandbox for " + coDirName);
    status = sMgr.addSandbox(sbRes.getFile(),"","");
    assertTrue("sandbox add failed", status);
    assertEquals("wrong number of sandboxes", 1, sMgr.getSandboxList().size());

    // adding it again should do nothing
    log.info("adding again");
    status = sMgr.addSandbox(sbRes.getFile(),"","");
    assertTrue("sandbox add succeeded", ! status);
    assertEquals(
      "wrong number of sandboxes (B)", 1, sMgr.getSandboxList().size()
    );

    // forget, add again
    log.info("forgetting");
    sMgr.forgetSandbox(sbRes.getFile());
    assertEquals(
      "wrong number of sandboxes (C)", 0, sMgr.getSandboxList().size()
    );
    status = sMgr.addSandbox(sbRes.getFile(),"","");
    assertTrue("sandbox add failed (B)", status);

    // get the sandbox
    log.info("getting");
    Sandbox sb = sMgr.getSandbox(sMgr.getSandboxId(sbRes.getFile()));
    assertNotNull("sandbox is null", sb);
    
    // get the sandbox map
    log.info("getting the sandbox map");
    Map<String, Sandbox> sbMap = sMgr.getSandboxMap();
    assertNotNull("sandbox map is null", sbMap);
    assertEquals(
      "wrong number of sandboxes (C)", 1, sMgr.getSandboxMap().size()
    );
    sb = sbMap.get(sMgr.getSandboxId(sbRes.getFile()));
    assertNotNull("sandbox is null (b)", sb);

    // do status
    SVNStatus svnStatus = sb.doStatus(sbRes.getFile(), true);
    log.debug("getFile().getName() = " + svnStatus.getFile().getName());
    SVNStatusType contentsStatus = svnStatus.getContentsStatus();
    logStatus(contentsStatus);

    // add a file and do status
    File testFile = new File(sbRes.getFile(), "test.txt");
    FileWriter fos = null;
    try {
      fos = new FileWriter(testFile);
      fos.write("lsdsldflsdfl\n");
    } catch (FileNotFoundException fnfe) {
      log.error("the file '" + testFile.getAbsolutePath() + "' not found", fnfe);
    } catch (IOException ioe) {
      log.error("error writing file '" + testFile.getAbsolutePath() + "'", ioe);
    } finally {
      if (fos != null)
        try { fos.close(); } catch (IOException ioe) { }
    }
    sb.addEntry(testFile);
    svnStatus = sb.doStatus(testFile, true);
    contentsStatus = svnStatus.getContentsStatus();
    logStatus(contentsStatus);

    SVNCommitInfo[] commInfo = sb.commit(new File[] {testFile}, false, "added");
    assertEquals("Wrong number of SVNCommitInfos returned by commit", 1,
        commInfo.length);
    long committedRevision = commInfo[0].getNewRevision();
    log.debug("committed to revision " + committedRevision);
    svnStatus = sb.doStatus(testFile, true);
    contentsStatus = svnStatus.getContentsStatus();
    logStatus(contentsStatus);

    long currentRevision = svnStatus.getRevision().getNumber();
    log.debug("current revision = " + currentRevision);

    long updatedRevision = sb.update(testFile, SVNRevision.HEAD, true);
    log.debug("updated to revision " + updatedRevision);

    log.info("=============================================================");
  } // testVersioning()

  public void logStatus(SVNStatusType contentsStatus) {
    if(contentsStatus == SVNStatusType.STATUS_MODIFIED) {
      log.debug("contentStatus=MODIFIED");
    } else if(contentsStatus == SVNStatusType.STATUS_CONFLICTED) {
      log.debug("contentStatus=CONFLICTED");
    } else if(contentsStatus == SVNStatusType.STATUS_DELETED) {
      log.debug("contentStatus=DELETED");
    } else if(contentsStatus == SVNStatusType.STATUS_ADDED) {
      log.debug("contentStatus=ADDED");
    } else if(contentsStatus == SVNStatusType.STATUS_UNVERSIONED) {
      log.debug("contentStatus=UNVERSIONED");
    } else if(contentsStatus == SVNStatusType.STATUS_EXTERNAL) {
      log.debug("contentStatus=EXTERNAL");
    } else if(contentsStatus == SVNStatusType.STATUS_IGNORED) {
      log.debug("contentStatus=IGNORED");
    } else if(contentsStatus == SVNStatusType.STATUS_MISSING) {
      log.debug("contentStatus=MISSING");
    } else if(contentsStatus == SVNStatusType.STATUS_INCOMPLETE) {
      log.debug("contentStatus=INCOMPLETE");
    } else if(contentsStatus == SVNStatusType.STATUS_OBSTRUCTED) {
      log.debug("contentStatus=OBSTRUCTED");
    } else if(contentsStatus == SVNStatusType.STATUS_REPLACED) {
      log.debug("contentStatus=REPLACED");
    } else if(contentsStatus == SVNStatusType.STATUS_NONE) {
      log.debug("contentStatus=NONE");
    } else if(contentsStatus == SVNStatusType.STATUS_NORMAL) {
      log.debug("contentStatus=NORMAL");
    }
  } // logStatus(SVNStatusType)
} // SvnKitSpringTest