1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.aragost.javahg.commands;
27
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32
33 import org.junit.Assert;
34 import org.junit.Test;
35
36 import com.aragost.javahg.BaseRepository;
37 import com.aragost.javahg.Repository;
38 import com.aragost.javahg.test.AbstractTestCase;
39 import com.google.common.io.ByteStreams;
40
41 public class CatCommandTest extends AbstractTestCase {
42
43 @Test
44 public void test() throws IOException {
45 String data = "This is a binary file:\n\0\1\2\3\4\5\6\7";
46 Repository repo = getTestRepository();
47 writeFile("a", data);
48 commit();
49
50 InputStream stream = CatCommand.on(repo).execute("a");
51 byte[] result = new byte[data.getBytes().length];
52 Assert.assertEquals(result.length, stream.read(result));
53 Assert.assertArrayEquals(data.getBytes(), result);
54 }
55
56 @Test
57 public void testLargeFile() throws IOException, InterruptedException {
58 BaseRepository repo = getTestRepository();
59 byte[] s = new byte[1024];
60 File file = new File(repo.getDirectory(), "large");
61 FileOutputStream out = new FileOutputStream(file);
62 for (int i = 0; i < 1024; i++) {
63 out.write(s);
64 }
65 out.close();
66 commit();
67 InputStream stream = CatCommand.on(repo).execute("large");
68 Assert.assertEquals(1024 * 1024, ByteStreams.toByteArray(stream).length);
69 }
70
71 @Test
72 public void testNoSuchFile() throws IOException {
73 Repository repo = getTestRepository();
74 CatCommand cmd = CatCommand.on(repo);
75 try {
76 cmd.execute("a");
77 assertFailedExecution(cmd);
78 } catch (ExecutionException e) {
79 Assert.assertSame(cmd, e.getCommand());
80 Assert.assertEquals("a: no such file in rev 000000000000\n", cmd.getErrorString());
81 }
82 }
83
84 }