View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
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  }