Mercurial > hg4j
diff test/org/tmatesoft/hg/test/TestCommit.java @ 538:dd4f6311af52
Commit: first working version
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 05 Feb 2013 22:30:21 +0100 |
parents | 243202f1bda5 |
children | 9edfd5a223b8 |
line wrap: on
line diff
--- a/test/org/tmatesoft/hg/test/TestCommit.java Tue Feb 05 20:06:22 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestCommit.java Tue Feb 05 22:30:21 2013 +0100 @@ -16,8 +16,14 @@ */ package org.tmatesoft.hg.test; -import org.junit.Assert; +import java.io.File; +import java.io.FileWriter; +import java.nio.ByteBuffer; + import org.junit.Test; +import org.tmatesoft.hg.repo.CommitFacility; +import org.tmatesoft.hg.repo.HgLookup; +import org.tmatesoft.hg.repo.HgRepository; /** * @@ -27,7 +33,61 @@ public class TestCommit { @Test - public void testCommitToEmpty() throws Exception { - Assert.fail(); + public void testCommitToNonEmpty() throws Exception { + File repoLoc = RepoUtils.initEmptyTempRepo("test-commit2non-empty"); + FileWriter fw = new FileWriter(new File(repoLoc, "file1")); + fw.write("hello"); + fw.close(); + new ExecHelper(new OutputParser.Stub(true), repoLoc).run("hg", "commit", "--addremove", "-m", "FIRST"); + // + HgRepository hgRepo = new HgLookup().detect(repoLoc); + CommitFacility cf = new CommitFacility(hgRepo, 0 /*NO_REVISION*/); + // FIXME test diff for processing changed newlines - if a whole line or just changed endings are in the patch! + cf.add(hgRepo.getFileNode("file1"), new ByteArraySupplier("hello\nworld".getBytes())); + cf.commit("commit 1"); + // /tmp/test-commit2non-empty/.hg/ store/data/file1.i dumpData + } + + public static void main(String[] args) throws Exception { + new TestCommit().testCommitToNonEmpty(); + String input = "abcdefghijklmnopqrstuvwxyz"; + ByteArraySupplier bas = new ByteArraySupplier(input.getBytes()); + ByteBuffer bb = ByteBuffer.allocate(7); + byte[] result = new byte[26]; + int rpos = 0; + while (bas.read(bb) != -1) { + bb.flip(); + bb.get(result, rpos, bb.limit()); + rpos += bb.limit(); + bb.clear(); + } + if (input.length() != rpos) { + throw new AssertionError(); + } + String output = new String(result); + if (!input.equals(output)) { + throw new AssertionError(); + } + System.out.println(output); + } + + static class ByteArraySupplier implements CommitFacility.ByteDataSupplier { + + private final byte[] data; + private int pos = 0; + + public ByteArraySupplier(byte[] source) { + data = source; + } + + public int read(ByteBuffer buf) { + if (pos >= data.length) { + return -1; + } + int count = Math.min(buf.remaining(), data.length - pos); + buf.put(data, pos, count); + pos += count; + return count; + } } }