tikhomirov@536: /* tikhomirov@536: * Copyright (c) 2013 TMate Software Ltd tikhomirov@536: * tikhomirov@536: * This program is free software; you can redistribute it and/or modify tikhomirov@536: * it under the terms of the GNU General Public License as published by tikhomirov@536: * the Free Software Foundation; version 2 of the License. tikhomirov@536: * tikhomirov@536: * This program is distributed in the hope that it will be useful, tikhomirov@536: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@536: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@536: * GNU General Public License for more details. tikhomirov@536: * tikhomirov@536: * For information on how to redistribute this software under tikhomirov@536: * the terms of a license other than GNU General Public License tikhomirov@536: * contact TMate Software at support@hg4j.com tikhomirov@536: */ tikhomirov@536: package org.tmatesoft.hg.test; tikhomirov@536: tikhomirov@536: import static org.junit.Assert.assertEquals; tikhomirov@536: import static org.junit.Assert.assertTrue; tikhomirov@536: import static org.tmatesoft.hg.internal.RequiresFile.*; tikhomirov@623: import static org.tmatesoft.hg.util.LogFacility.Severity.Debug; tikhomirov@536: tikhomirov@536: import java.io.File; tikhomirov@536: import java.io.FileOutputStream; tikhomirov@559: import java.io.FileWriter; tikhomirov@536: import java.io.IOException; tikhomirov@536: import java.util.ArrayList; tikhomirov@623: import java.util.Arrays; tikhomirov@623: import java.util.Iterator; tikhomirov@623: import java.util.LinkedList; tikhomirov@536: tikhomirov@623: import org.tmatesoft.hg.core.HgException; tikhomirov@622: import org.tmatesoft.hg.core.HgIOException; tikhomirov@623: import org.tmatesoft.hg.internal.FileUtils; tikhomirov@536: import org.tmatesoft.hg.internal.RepoInitializer; tikhomirov@623: import org.tmatesoft.hg.internal.StreamLogFacility; tikhomirov@536: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@536: tikhomirov@536: /** tikhomirov@536: * tikhomirov@536: * @author Artem Tikhomirov tikhomirov@536: * @author TMate Software Ltd. tikhomirov@536: */ tikhomirov@536: public class RepoUtils { tikhomirov@536: tikhomirov@622: static File initEmptyTempRepo(String dirName) throws IOException, HgIOException { tikhomirov@536: File dest = createEmptyDir(dirName); tikhomirov@536: RepoInitializer ri = new RepoInitializer(); tikhomirov@536: ri.setRequires(STORE | FNCACHE | DOTENCODE); tikhomirov@536: ri.initEmptyRepository(new File(dest, ".hg")); tikhomirov@536: return dest; tikhomirov@536: } tikhomirov@536: tikhomirov@536: static File createEmptyDir(String dirName) throws IOException { tikhomirov@536: File dest = new File(Configuration.get().getTempDir(), dirName); tikhomirov@536: if (dest.exists()) { tikhomirov@536: TestClone.rmdir(dest); tikhomirov@536: } tikhomirov@536: dest.mkdirs(); tikhomirov@536: return dest; tikhomirov@536: } tikhomirov@536: tikhomirov@623: static File cloneRepoToTempLocation(String configRepoName, String name, boolean noupdate) throws HgException, IOException, InterruptedException { tikhomirov@536: return cloneRepoToTempLocation(Configuration.get().find(configRepoName), name, noupdate); tikhomirov@536: } tikhomirov@536: tikhomirov@536: static File cloneRepoToTempLocation(HgRepository repo, String name, boolean noupdate) throws IOException, InterruptedException { tikhomirov@536: File testRepoLoc = createEmptyDir(name); tikhomirov@536: ExecHelper eh = new ExecHelper(new OutputParser.Stub(), testRepoLoc.getParentFile()); tikhomirov@536: ArrayList cmd = new ArrayList(); tikhomirov@559: cmd.add("hg"); tikhomirov@559: cmd.add("clone"); tikhomirov@536: if (noupdate) { tikhomirov@536: cmd.add("--noupdate"); tikhomirov@536: } tikhomirov@536: cmd.add(repo.getWorkingDir().toString()); tikhomirov@536: cmd.add(testRepoLoc.getName()); tikhomirov@536: eh.run(cmd.toArray(new String[cmd.size()])); tikhomirov@536: assertEquals("[sanity]", 0, eh.getExitValue()); tikhomirov@536: return testRepoLoc; tikhomirov@536: } tikhomirov@623: tikhomirov@623: static File copyRepoToTempLocation(String configRepoName, String newRepoName) throws HgException, IOException { tikhomirov@623: File testRepoLoc = createEmptyDir(newRepoName); tikhomirov@623: final File srcDir = Configuration.get().find(configRepoName).getWorkingDir(); tikhomirov@623: Iterator it = new Iterator() { tikhomirov@623: private final LinkedList queue = new LinkedList(); tikhomirov@623: { tikhomirov@623: queue.addAll(Arrays.asList(srcDir.listFiles())); tikhomirov@623: } tikhomirov@623: public boolean hasNext() { tikhomirov@623: return !queue.isEmpty(); tikhomirov@623: } tikhomirov@623: public File next() { tikhomirov@623: File n = queue.removeFirst(); tikhomirov@623: if (n.isDirectory()) { tikhomirov@623: queue.addAll(Arrays.asList(n.listFiles())); tikhomirov@623: } tikhomirov@623: return n; tikhomirov@623: } tikhomirov@623: public void remove() { tikhomirov@623: throw new UnsupportedOperationException(); tikhomirov@623: } tikhomirov@623: }; tikhomirov@623: FileUtils fu = new FileUtils(new StreamLogFacility(Debug, true, System.err)); tikhomirov@623: String srcPrefix = srcDir.getAbsolutePath(); tikhomirov@623: while (it.hasNext()) { tikhomirov@623: File next = it.next(); tikhomirov@623: assert next.getAbsolutePath().startsWith(srcPrefix); tikhomirov@623: String relPath = next.getAbsolutePath().substring(srcPrefix.length()); tikhomirov@623: File dest = new File(testRepoLoc, relPath); tikhomirov@623: if (next.isDirectory()) { tikhomirov@623: dest.mkdir(); tikhomirov@623: } else { tikhomirov@623: fu.copy(next, dest); tikhomirov@623: dest.setLastModified(next.lastModified()); tikhomirov@623: } tikhomirov@623: } tikhomirov@623: return testRepoLoc; tikhomirov@623: } tikhomirov@536: tikhomirov@559: static void modifyFileAppend(File f, Object content) throws IOException { tikhomirov@536: assertTrue(f.isFile()); tikhomirov@536: FileOutputStream fos = new FileOutputStream(f, true); tikhomirov@559: if (content == null) { tikhomirov@559: content = "XXX".getBytes(); tikhomirov@559: } tikhomirov@559: if (content instanceof byte[]) { tikhomirov@559: fos.write((byte[]) content); tikhomirov@559: } else { tikhomirov@559: fos.write(String.valueOf(content).getBytes()); tikhomirov@559: } tikhomirov@536: fos.close(); tikhomirov@536: } tikhomirov@536: tikhomirov@559: static void createFile(File f, Object content) throws IOException { tikhomirov@559: if (content == null) { tikhomirov@559: f.createNewFile(); tikhomirov@559: return; tikhomirov@559: } tikhomirov@559: if (content instanceof byte[]) { tikhomirov@559: FileOutputStream fos = new FileOutputStream(f); tikhomirov@559: fos.write((byte[]) content); tikhomirov@559: fos.close(); tikhomirov@559: } else { tikhomirov@559: FileWriter fw = new FileWriter(f); tikhomirov@559: fw.write(String.valueOf(content)); tikhomirov@559: fw.close(); tikhomirov@559: } tikhomirov@559: } tikhomirov@536: }