comparison test/org/tmatesoft/hg/test/TestTransaction.java @ 665:dde18bc7053b v1.2m1

Test Copy-on-Write transactions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 10 Jul 2013 20:16:37 +0200
parents
children
comparison
equal deleted inserted replaced
664:ae2d439fbed3 665:dde18bc7053b
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import static org.junit.Assert.*;
20
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.tmatesoft.hg.internal.BasicSessionContext;
28 import org.tmatesoft.hg.internal.COWTransaction;
29 import org.tmatesoft.hg.internal.Transaction;
30
31 /**
32 * Check transaction rollback/commit as it's tricky to test transactions as part of pull/push commands
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 public class TestTransaction {
38
39 @Rule
40 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
41
42 @Test
43 public void testCopyOnWriteTransaction() throws Exception {
44 final BasicSessionContext ctx = new BasicSessionContext(null);
45 Transaction.Factory f = new COWTransaction.Factory();
46 File dir = RepoUtils.createEmptyDir("test-transaction-cow");
47 File f1 = new File(dir, "f1");
48 File f2 = new File(dir, "f2");
49 File f3 = new File(dir, "f3");
50 RepoUtils.createFile(f1, "1");
51 assertTrue(f1.exists());
52 assertFalse(f2.exists());
53 assertFalse(f3.exists());
54 //
55 // transaction commit
56 Transaction tr1 = f.create(ctx);
57 File tf1 = tr1.prepare(f1);
58 RepoUtils.modifyFileAppend(tf1, "2");
59 tr1.done(tf1);
60 File tf2 = tr1.prepare(f2);
61 errorCollector.assertTrue(tf2.exists());
62 RepoUtils.modifyFileAppend(tf2, "A");
63 tr1.done(tf2);
64 tr1.commit();
65 errorCollector.assertTrue(f1.isFile());
66 errorCollector.assertTrue(f2.isFile());
67 errorCollector.assertEquals("12", read(f1));
68 errorCollector.assertEquals("A", read(f2));
69 //
70 // transaction rollback
71 assertFalse(f3.exists());
72 Transaction tr2 = f.create(ctx);
73 tf1 = tr2.prepare(f1);
74 RepoUtils.modifyFileAppend(tf1, "3");
75 tr2.done(tf1);
76 errorCollector.assertEquals("123", read(tf1));
77 tf2 = tr2.prepare(f2);
78 RepoUtils.modifyFileAppend(tf2, "B");
79 tr2.done(tf2);
80 errorCollector.assertEquals("AB", read(tf2));
81 File tf3 = tr2.prepare(f3);
82 errorCollector.assertTrue(tf3.exists());
83 RepoUtils.modifyFileAppend(tf3, "!");
84 tr2.done(tf3);
85 errorCollector.assertEquals("!", read(tf3));
86 tr2.rollback();
87 errorCollector.assertTrue(f1.isFile());
88 errorCollector.assertTrue(f2.isFile());
89 errorCollector.assertFalse(f3.isFile());
90 errorCollector.assertEquals("12", read(f1));
91 errorCollector.assertEquals("A", read(f2));
92 }
93
94 String read(File f) throws IOException {
95 StringBuilder sb = new StringBuilder();
96 FileReader fr = new FileReader(f);
97 int ch;
98 while ((ch = fr.read()) != -1) {
99 sb.append((char) ch);
100 }
101 fr.close();
102 return sb.toString();
103 }
104 }