comparison 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
comparison
equal deleted inserted replaced
537:5a455624be4f 538:dd4f6311af52
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.test; 17 package org.tmatesoft.hg.test;
18 18
19 import org.junit.Assert; 19 import java.io.File;
20 import java.io.FileWriter;
21 import java.nio.ByteBuffer;
22
20 import org.junit.Test; 23 import org.junit.Test;
24 import org.tmatesoft.hg.repo.CommitFacility;
25 import org.tmatesoft.hg.repo.HgLookup;
26 import org.tmatesoft.hg.repo.HgRepository;
21 27
22 /** 28 /**
23 * 29 *
24 * @author Artem Tikhomirov 30 * @author Artem Tikhomirov
25 * @author TMate Software Ltd. 31 * @author TMate Software Ltd.
26 */ 32 */
27 public class TestCommit { 33 public class TestCommit {
28 34
29 @Test 35 @Test
30 public void testCommitToEmpty() throws Exception { 36 public void testCommitToNonEmpty() throws Exception {
31 Assert.fail(); 37 File repoLoc = RepoUtils.initEmptyTempRepo("test-commit2non-empty");
38 FileWriter fw = new FileWriter(new File(repoLoc, "file1"));
39 fw.write("hello");
40 fw.close();
41 new ExecHelper(new OutputParser.Stub(true), repoLoc).run("hg", "commit", "--addremove", "-m", "FIRST");
42 //
43 HgRepository hgRepo = new HgLookup().detect(repoLoc);
44 CommitFacility cf = new CommitFacility(hgRepo, 0 /*NO_REVISION*/);
45 // FIXME test diff for processing changed newlines - if a whole line or just changed endings are in the patch!
46 cf.add(hgRepo.getFileNode("file1"), new ByteArraySupplier("hello\nworld".getBytes()));
47 cf.commit("commit 1");
48 // /tmp/test-commit2non-empty/.hg/ store/data/file1.i dumpData
49 }
50
51 public static void main(String[] args) throws Exception {
52 new TestCommit().testCommitToNonEmpty();
53 String input = "abcdefghijklmnopqrstuvwxyz";
54 ByteArraySupplier bas = new ByteArraySupplier(input.getBytes());
55 ByteBuffer bb = ByteBuffer.allocate(7);
56 byte[] result = new byte[26];
57 int rpos = 0;
58 while (bas.read(bb) != -1) {
59 bb.flip();
60 bb.get(result, rpos, bb.limit());
61 rpos += bb.limit();
62 bb.clear();
63 }
64 if (input.length() != rpos) {
65 throw new AssertionError();
66 }
67 String output = new String(result);
68 if (!input.equals(output)) {
69 throw new AssertionError();
70 }
71 System.out.println(output);
72 }
73
74 static class ByteArraySupplier implements CommitFacility.ByteDataSupplier {
75
76 private final byte[] data;
77 private int pos = 0;
78
79 public ByteArraySupplier(byte[] source) {
80 data = source;
81 }
82
83 public int read(ByteBuffer buf) {
84 if (pos >= data.length) {
85 return -1;
86 }
87 int count = Math.min(buf.remaining(), data.length - pos);
88 buf.put(data, pos, count);
89 pos += count;
90 return count;
91 }
32 } 92 }
33 } 93 }