kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.test; kitaev@213: kitaev@213: import static org.junit.Assert.assertArrayEquals; kitaev@213: kitaev@213: import org.junit.Assert; kitaev@213: import org.junit.Test; kitaev@213: import org.tmatesoft.hg.internal.ByteArrayChannel; kitaev@213: import org.tmatesoft.hg.repo.HgDataFile; kitaev@213: import org.tmatesoft.hg.repo.HgRepository; kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class TestByteChannel { kitaev@213: kitaev@213: private HgRepository repo; kitaev@213: kitaev@213: public static void main(String[] args) throws Exception { kitaev@213: // HgRepoFacade rf = new HgRepoFacade(); kitaev@213: // rf.init(); kitaev@213: // HgDataFile file = rf.getRepository().getFileNode("src/org/tmatesoft/hg/internal/KeywordFilter.java"); kitaev@213: // for (int i = file.getLastRevision(); i >= 0; i--) { kitaev@213: // System.out.print("Content for revision:" + i); kitaev@213: // compareContent(file, i); kitaev@213: // System.out.println(" OK"); kitaev@213: // } kitaev@213: //CatCommand cmd = rf.createCatCommand(); kitaev@213: } kitaev@213: kitaev@213: // private static void compareContent(HgDataFile file, int rev) throws Exception { kitaev@213: // byte[] oldAccess = file.content(rev); kitaev@213: // ByteArrayChannel ch = new ByteArrayChannel(); kitaev@213: // file.content(rev, ch); kitaev@213: // byte[] newAccess = ch.toArray(); kitaev@213: // Assert.assertArrayEquals(oldAccess, newAccess); kitaev@213: // // don't trust anyone (even JUnit) kitaev@213: // if (!Arrays.equals(oldAccess, newAccess)) { kitaev@213: // throw new RuntimeException("Failed:" + rev); kitaev@213: // } kitaev@213: // } kitaev@213: kitaev@213: @Test kitaev@213: public void testContent() throws Exception { kitaev@213: repo = Configuration.get().find("log-1"); kitaev@213: final byte[] expectedContent = new byte[] { 'a', ' ', 13, 10 }; kitaev@213: ByteArrayChannel ch = new ByteArrayChannel(); kitaev@213: repo.getFileNode("dir/b").content(0, ch); kitaev@213: assertArrayEquals(expectedContent, ch.toArray()); kitaev@213: repo.getFileNode("d").content(HgRepository.TIP, ch = new ByteArrayChannel() ); kitaev@213: assertArrayEquals(expectedContent, ch.toArray()); kitaev@213: } kitaev@213: kitaev@213: @Test kitaev@213: public void testStripMetadata() throws Exception { kitaev@213: repo = Configuration.get().find("log-1"); kitaev@213: ByteArrayChannel ch = new ByteArrayChannel(); kitaev@213: HgDataFile dir_b = repo.getFileNode("dir/b"); kitaev@213: Assert.assertTrue(dir_b.isCopy()); kitaev@213: Assert.assertEquals("b", dir_b.getCopySourceName().toString()); kitaev@213: Assert.assertEquals("e44751cdc2d14f1eb0146aa64f0895608ad15917", dir_b.getCopySourceRevision().toString()); kitaev@213: dir_b.content(0, ch); kitaev@213: // assert rawContent has 1 10 ... 1 10 kitaev@213: assertArrayEquals("a \r\n".getBytes(), ch.toArray()); kitaev@213: // kitaev@213: // try once again to make sure metadata records/extracts correct offsets kitaev@213: dir_b.content(0, ch = new ByteArrayChannel()); kitaev@213: assertArrayEquals("a \r\n".getBytes(), ch.toArray()); kitaev@213: } kitaev@213: }