tikhomirov@115: /* tikhomirov@388: * Copyright (c) 2011-2012 TMate Software Ltd tikhomirov@115: * tikhomirov@115: * This program is free software; you can redistribute it and/or modify tikhomirov@115: * it under the terms of the GNU General Public License as published by tikhomirov@115: * the Free Software Foundation; version 2 of the License. tikhomirov@115: * tikhomirov@115: * This program is distributed in the hope that it will be useful, tikhomirov@115: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@115: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@115: * GNU General Public License for more details. tikhomirov@115: * tikhomirov@115: * For information on how to redistribute this software under tikhomirov@115: * the terms of a license other than GNU General Public License tikhomirov@130: * contact TMate Software at support@hg4j.com tikhomirov@115: */ tikhomirov@115: package org.tmatesoft.hg.test; tikhomirov@115: tikhomirov@388: import static org.junit.Assert.*; tikhomirov@388: tikhomirov@388: import java.io.File; tikhomirov@388: import java.io.FileWriter; tikhomirov@388: import java.io.IOException; tikhomirov@388: import java.util.Collections; tikhomirov@388: import java.util.Map; tikhomirov@115: tikhomirov@115: import org.junit.Assert; tikhomirov@157: import org.junit.Test; tikhomirov@388: import org.tmatesoft.hg.internal.BasicSessionContext; tikhomirov@115: import org.tmatesoft.hg.internal.ByteArrayChannel; tikhomirov@388: import org.tmatesoft.hg.internal.Internals; tikhomirov@115: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@388: import org.tmatesoft.hg.repo.HgLookup; tikhomirov@157: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@115: tikhomirov@115: /** tikhomirov@115: * tikhomirov@115: * @author Artem Tikhomirov tikhomirov@115: * @author TMate Software Ltd. tikhomirov@115: */ tikhomirov@115: public class TestByteChannel { tikhomirov@115: tikhomirov@157: private HgRepository repo; tikhomirov@157: tikhomirov@115: public static void main(String[] args) throws Exception { tikhomirov@157: // HgRepoFacade rf = new HgRepoFacade(); tikhomirov@157: // rf.init(); tikhomirov@157: // HgDataFile file = rf.getRepository().getFileNode("src/org/tmatesoft/hg/internal/KeywordFilter.java"); tikhomirov@157: // for (int i = file.getLastRevision(); i >= 0; i--) { tikhomirov@157: // System.out.print("Content for revision:" + i); tikhomirov@157: // compareContent(file, i); tikhomirov@157: // System.out.println(" OK"); tikhomirov@157: // } tikhomirov@116: //CatCommand cmd = rf.createCatCommand(); tikhomirov@116: } tikhomirov@116: tikhomirov@157: // private static void compareContent(HgDataFile file, int rev) throws Exception { tikhomirov@157: // byte[] oldAccess = file.content(rev); tikhomirov@157: // ByteArrayChannel ch = new ByteArrayChannel(); tikhomirov@157: // file.content(rev, ch); tikhomirov@157: // byte[] newAccess = ch.toArray(); tikhomirov@157: // Assert.assertArrayEquals(oldAccess, newAccess); tikhomirov@157: // // don't trust anyone (even JUnit) tikhomirov@157: // if (!Arrays.equals(oldAccess, newAccess)) { tikhomirov@157: // throw new RuntimeException("Failed:" + rev); tikhomirov@157: // } tikhomirov@157: // } tikhomirov@157: tikhomirov@157: @Test tikhomirov@157: public void testContent() throws Exception { tikhomirov@157: repo = Configuration.get().find("log-1"); tikhomirov@157: final byte[] expectedContent = new byte[] { 'a', ' ', 13, 10 }; tikhomirov@115: ByteArrayChannel ch = new ByteArrayChannel(); tikhomirov@157: repo.getFileNode("dir/b").content(0, ch); tikhomirov@157: assertArrayEquals(expectedContent, ch.toArray()); tikhomirov@157: repo.getFileNode("d").content(HgRepository.TIP, ch = new ByteArrayChannel() ); tikhomirov@157: assertArrayEquals(expectedContent, ch.toArray()); tikhomirov@157: } tikhomirov@157: tikhomirov@157: @Test tikhomirov@157: public void testStripMetadata() throws Exception { tikhomirov@157: repo = Configuration.get().find("log-1"); tikhomirov@157: ByteArrayChannel ch = new ByteArrayChannel(); tikhomirov@157: HgDataFile dir_b = repo.getFileNode("dir/b"); tikhomirov@157: Assert.assertTrue(dir_b.isCopy()); tikhomirov@157: Assert.assertEquals("b", dir_b.getCopySourceName().toString()); tikhomirov@157: Assert.assertEquals("e44751cdc2d14f1eb0146aa64f0895608ad15917", dir_b.getCopySourceRevision().toString()); tikhomirov@157: dir_b.content(0, ch); tikhomirov@157: // assert rawContent has 1 10 ... 1 10 tikhomirov@157: assertArrayEquals("a \r\n".getBytes(), ch.toArray()); tikhomirov@157: // tikhomirov@157: // try once again to make sure metadata records/extracts correct offsets tikhomirov@157: dir_b.content(0, ch = new ByteArrayChannel()); tikhomirov@157: assertArrayEquals("a \r\n".getBytes(), ch.toArray()); tikhomirov@115: } tikhomirov@388: tikhomirov@388: @Test tikhomirov@388: public void testWorkingCopyFileAccess() throws Exception { tikhomirov@536: final File repoDir = RepoUtils.initEmptyTempRepo("testWorkingCopyFileAccess"); tikhomirov@388: final Map props = Collections.singletonMap(Internals.CFG_PROPERTY_REVLOG_STREAM_CACHE, false); tikhomirov@431: repo = new HgLookup(new BasicSessionContext(props, null)).detect(repoDir); tikhomirov@388: File f1 = new File(repoDir, "file1"); tikhomirov@388: final String c1 = "First", c2 = "Second", c3 = "Third"; tikhomirov@388: ByteArrayChannel ch; tikhomirov@388: ExecHelper exec = new ExecHelper(new OutputParser.Stub(), repoDir); tikhomirov@388: // commit cset 0 tikhomirov@388: write(f1, c1); tikhomirov@388: exec.run("hg", "add"); tikhomirov@388: Assert.assertEquals(0, exec.getExitValue()); tikhomirov@388: exec.run("hg", "commit", "-m", "c0"); tikhomirov@388: Assert.assertEquals(0, exec.getExitValue()); tikhomirov@388: // commit cset 1 tikhomirov@388: write(f1, c2); tikhomirov@388: exec.run("hg", "commit", "-m", "c1"); tikhomirov@388: assertEquals(0, exec.getExitValue()); tikhomirov@388: // tikhomirov@388: // modify working copy tikhomirov@388: write(f1, c3); tikhomirov@388: // tikhomirov@388: HgDataFile df = repo.getFileNode(f1.getName()); tikhomirov@388: // 1. Shall take content of the file from the dir tikhomirov@388: df.workingCopy(ch = new ByteArrayChannel()); tikhomirov@388: assertArrayEquals(c3.getBytes(), ch.toArray()); tikhomirov@388: // 2. Shall supply working copy even if no local file is there tikhomirov@388: f1.delete(); tikhomirov@388: assertFalse(f1.exists()); tikhomirov@388: df = repo.getFileNode(f1.getName()); tikhomirov@388: df.workingCopy(ch = new ByteArrayChannel()); tikhomirov@388: assertArrayEquals(c2.getBytes(), ch.toArray()); tikhomirov@388: // tikhomirov@388: // 3. Shall extract revision of the file that corresponds actual parents (from dirstate) not the TIP as it was tikhomirov@388: exec.run("hg", "update", "-r", "0"); tikhomirov@388: assertEquals(0, exec.getExitValue()); tikhomirov@388: f1.delete(); tikhomirov@388: assertFalse(f1.exists()); tikhomirov@388: // there's no file and workingCopy shall do some extra work to find out actual revision to check out tikhomirov@388: df = repo.getFileNode(f1.getName()); tikhomirov@388: df.workingCopy(ch = new ByteArrayChannel()); tikhomirov@388: assertArrayEquals(c1.getBytes(), ch.toArray()); tikhomirov@388: } tikhomirov@388: tikhomirov@388: private static void write(File f, String content) throws IOException { tikhomirov@388: FileWriter fw = new FileWriter(f); tikhomirov@388: fw.write(content); tikhomirov@388: fw.close(); tikhomirov@388: } tikhomirov@115: }