# HG changeset patch # User Artem Tikhomirov # Date 1295630405 -3600 # Node ID 52dc3f4cfc761bdc8b2af8cb6460819cd67bea83 # Parent e21df6259f8374ac136767321e837c0c6dd21907 Primitive test suite in org.tmatesoft diff -r e21df6259f83 -r 52dc3f4cfc76 test/com/tmate/hgkit/ExecHelper.java --- a/test/com/tmate/hgkit/ExecHelper.java Fri Jan 21 06:17:56 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2011 Artem Tikhomirov - */ -package com.tmate.hgkit; - -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.CharBuffer; -import java.util.LinkedList; - -/** - * - * @author artem - */ -public class ExecHelper { - - private final OutputParser parser; - private final File dir; - - public ExecHelper(OutputParser outParser, File workingDir) { - parser = outParser; - dir = workingDir; - } - - public void run(String... cmd) throws IOException, InterruptedException { - Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start(); -// Process p = Runtime.getRuntime().exec(cmd, null, dir); - InputStreamReader stdOut = new InputStreamReader(p.getInputStream()); - LinkedList l = new LinkedList(); - int r = -1; - CharBuffer b = null; - do { - if (b == null || b.remaining() < b.capacity() / 3) { - b = CharBuffer.allocate(512); - l.add(b); - } - r = stdOut.read(b); - } while (r != -1); - int total = 0; - for (CharBuffer cb : l) { - total += cb.position(); - cb.flip(); - } - CharBuffer res = CharBuffer.allocate(total); - for (CharBuffer cb : l) { - res.put(cb); - } - res.flip(); - p.waitFor(); - parser.parse(res); - } -} diff -r e21df6259f83 -r 52dc3f4cfc76 test/com/tmate/hgkit/OutputParser.java --- a/test/com/tmate/hgkit/OutputParser.java Fri Jan 21 06:17:56 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -/* - * Copyright (c) 2011 Artem Tikhomirov - */ -package com.tmate.hgkit; - -/** - * - * @author artem - */ -public interface OutputParser { - - public void parse(CharSequence seq); -} diff -r e21df6259f83 -r 52dc3f4cfc76 test/com/tmate/hgkit/StatusOutputParser.java --- a/test/com/tmate/hgkit/StatusOutputParser.java Fri Jan 21 06:17:56 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2011 Artem Tikhomirov - */ -package com.tmate.hgkit; - -import java.io.File; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * - * @author artem - */ -public class StatusOutputParser implements OutputParser { - - private final Pattern pattern; - private List modified, added, removed, clean, missing, unknown, ignored; - private Map copied; - private final boolean winPathSeparator; - - public StatusOutputParser() { -// pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE); - pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE); - winPathSeparator = File.separatorChar == '\\'; - } - - public void reset() { - modified = added = removed = clean = missing = unknown = ignored = null; - copied = null; - } - - public void parse(CharSequence seq) { - Matcher m = pattern.matcher(seq); - while (m.find()) { - String fname = m.group(2); - switch ((int) m.group(1).charAt(0)) { - case (int) 'M' : { - modified = doAdd(modified, fname); - break; - } - case (int) 'A' : { - added = doAdd(added, fname); - break; - } - case (int) 'R' : { - removed = doAdd(removed, fname); - break; - } - case (int) '?' : { - unknown = doAdd(unknown, fname); - break; - } - case (int) 'I' : { - ignored = doAdd(ignored, fname); - break; - } - case (int) 'C' : { - clean = doAdd(clean, fname); - break; - } - case (int) '!' : { - missing = doAdd(missing, fname); - break; - } - case (int) ' ' : { - if (copied == null) { - copied = new TreeMap(); - } - // last added is copy destination - // to get or to remove it - depends on what StatusCollector does in this case - copied.put(fname, added.get(added.size() - 1)); - break; - } - } - } - } - - // - public List getModified() { - return proper(modified); - } - - public List getAdded() { - return proper(added); - } - - public List getRemoved() { - return proper(removed); - } - - public Map getCopied() { - if (copied == null) { - return Collections.emptyMap(); - } - return Collections.unmodifiableMap(copied); - } - - public List getClean() { - return proper(clean); - } - - public List getMissing() { - return proper(missing); - } - - public List getUnknown() { - return proper(unknown); - } - - public List getIgnored() { - return proper(ignored); - } - - private List proper(List l) { - if (l == null) { - return Collections.emptyList(); - } - return Collections.unmodifiableList(l); - } - - private List doAdd(List l, String s) { - if (l == null) { - l = new LinkedList(); - } - if (winPathSeparator) { - // Java impl always give slashed path, while Hg uses local, os-specific convention - s = s.replace('\\', '/'); - } - l.add(s); - return l; - } -} diff -r e21df6259f83 -r 52dc3f4cfc76 test/com/tmate/hgkit/TestStatus.java --- a/test/com/tmate/hgkit/TestStatus.java Fri Jan 21 06:17:56 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2011 Artem Tikhomirov - */ -package com.tmate.hgkit; - -import java.io.File; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import com.tmate.hgkit.fs.FileWalker; -import com.tmate.hgkit.fs.RepositoryLookup; -import com.tmate.hgkit.ll.HgRepository; -import com.tmate.hgkit.ll.StatusCollector; -import com.tmate.hgkit.ll.WorkingCopyStatusCollector; - -/** - * - * @author artem - */ -public class TestStatus { - - public static void main(String[] args) throws Exception { - HgRepository repo = new RepositoryLookup().detectFromWorkingDir(); - final WorkingCopyStatusCollector wcc = new WorkingCopyStatusCollector(repo, new FileWalker(new File(System.getProperty("user.dir")))); - final StatusOutputParser statusParser = new StatusOutputParser(); - ExecHelper eh = new ExecHelper(statusParser, null); - // - eh.run("hg", "status", "-A"); - StatusCollector.Record r = wcc.status(HgRepository.TIP); - report("hg status -A", r, statusParser); - // - statusParser.reset(); - int revision = 3; - eh.run("hg", "status", "-A", "--rev", String.valueOf(revision)); - r = wcc.status(revision); - report("status -A --rev " + revision, r, statusParser); - // - statusParser.reset(); - eh.run("hg", "status", "-A", "--change", String.valueOf(revision)); - r = new StatusCollector.Record(); - new StatusCollector(repo).change(revision, r); - report("status -A --change " + revision, r, statusParser); - } - - private static void report(String what, StatusCollector.Record r, StatusOutputParser statusParser) { - System.out.println(">>>" + what); - reportNotEqual("MODIFIED", r.getModified(), statusParser.getModified()); - reportNotEqual("ADDED", r.getAdded(), statusParser.getAdded()); - reportNotEqual("REMOVED", r.getRemoved(), statusParser.getRemoved()); - reportNotEqual("CLEAN", r.getClean(), statusParser.getClean()); - reportNotEqual("IGNORED", r.getIgnored(), statusParser.getIgnored()); - reportNotEqual("MISSING", r.getMissing(), statusParser.getMissing()); - reportNotEqual("UNKNOWN", r.getUnknown(), statusParser.getUnknown()); - // TODO compare equals - System.out.println("<<<\n"); - } - - private static void reportNotEqual(String what, Collection l1, Collection l2) { - List diff = difference(l1, l2); - System.out.print(what); - if (!diff.isEmpty()) { - System.out.print(" are NOT the same: "); - for (T t : diff) { - System.out.print(t); - System.out.print(", "); - } - System.out.println(); - } else { - System.out.println(" are the same"); - } - } - - private static List difference(Collection l1, Collection l2) { - LinkedList result = new LinkedList(l2); - for (T t : l1) { - if (l2.contains(t)) { - result.remove(t); - } else { - result.add(t); - } - } - return result; - } -} diff -r e21df6259f83 -r 52dc3f4cfc76 test/org/tmatesoft/hg/test/ExecHelper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/ExecHelper.java Fri Jan 21 18:20:05 2011 +0100 @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@svnkit.com + */ +package org.tmatesoft.hg.test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.CharBuffer; +import java.util.LinkedList; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class ExecHelper { + + private final OutputParser parser; + private final File dir; + + public ExecHelper(OutputParser outParser, File workingDir) { + parser = outParser; + dir = workingDir; + } + + public void run(String... cmd) throws IOException, InterruptedException { + Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start(); +// Process p = Runtime.getRuntime().exec(cmd, null, dir); + InputStreamReader stdOut = new InputStreamReader(p.getInputStream()); + LinkedList l = new LinkedList(); + int r = -1; + CharBuffer b = null; + do { + if (b == null || b.remaining() < b.capacity() / 3) { + b = CharBuffer.allocate(512); + l.add(b); + } + r = stdOut.read(b); + } while (r != -1); + int total = 0; + for (CharBuffer cb : l) { + total += cb.position(); + cb.flip(); + } + CharBuffer res = CharBuffer.allocate(total); + for (CharBuffer cb : l) { + res.put(cb); + } + res.flip(); + p.waitFor(); + parser.parse(res); + } +} diff -r e21df6259f83 -r 52dc3f4cfc76 test/org/tmatesoft/hg/test/OutputParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/OutputParser.java Fri Jan 21 18:20:05 2011 +0100 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@svnkit.com + */ +package org.tmatesoft.hg.test; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public interface OutputParser { + + public void parse(CharSequence seq); +} diff -r e21df6259f83 -r 52dc3f4cfc76 test/org/tmatesoft/hg/test/StatusOutputParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/StatusOutputParser.java Fri Jan 21 18:20:05 2011 +0100 @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@svnkit.com + */ +package org.tmatesoft.hg.test; + +import java.io.File; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class StatusOutputParser implements OutputParser { + + private final Pattern pattern; + private List modified, added, removed, clean, missing, unknown, ignored; + private Map copied; + private final boolean winPathSeparator; + + public StatusOutputParser() { +// pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE); + pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE); + winPathSeparator = File.separatorChar == '\\'; + } + + public void reset() { + modified = added = removed = clean = missing = unknown = ignored = null; + copied = null; + } + + public void parse(CharSequence seq) { + Matcher m = pattern.matcher(seq); + while (m.find()) { + String fname = m.group(2); + switch ((int) m.group(1).charAt(0)) { + case (int) 'M' : { + modified = doAdd(modified, fname); + break; + } + case (int) 'A' : { + added = doAdd(added, fname); + break; + } + case (int) 'R' : { + removed = doAdd(removed, fname); + break; + } + case (int) '?' : { + unknown = doAdd(unknown, fname); + break; + } + case (int) 'I' : { + ignored = doAdd(ignored, fname); + break; + } + case (int) 'C' : { + clean = doAdd(clean, fname); + break; + } + case (int) '!' : { + missing = doAdd(missing, fname); + break; + } + case (int) ' ' : { + if (copied == null) { + copied = new TreeMap(); + } + // last added is copy destination + // to get or to remove it - depends on what StatusCollector does in this case + copied.put(fname, added.get(added.size() - 1)); + break; + } + } + } + } + + // + public List getModified() { + return proper(modified); + } + + public List getAdded() { + return proper(added); + } + + public List getRemoved() { + return proper(removed); + } + + public Map getCopied() { + if (copied == null) { + return Collections.emptyMap(); + } + return Collections.unmodifiableMap(copied); + } + + public List getClean() { + return proper(clean); + } + + public List getMissing() { + return proper(missing); + } + + public List getUnknown() { + return proper(unknown); + } + + public List getIgnored() { + return proper(ignored); + } + + private List proper(List l) { + if (l == null) { + return Collections.emptyList(); + } + return Collections.unmodifiableList(l); + } + + private List doAdd(List l, String s) { + if (l == null) { + l = new LinkedList(); + } + if (winPathSeparator) { + // Java impl always give slashed path, while Hg uses local, os-specific convention + s = s.replace('\\', '/'); + } + l.add(s); + return l; + } +} diff -r e21df6259f83 -r 52dc3f4cfc76 test/org/tmatesoft/hg/test/TestStatus.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/TestStatus.java Fri Jan 21 18:20:05 2011 +0100 @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@svnkit.com + */ +package org.tmatesoft.hg.test; + +import java.io.File; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import com.tmate.hgkit.fs.FileWalker; +import com.tmate.hgkit.fs.RepositoryLookup; +import com.tmate.hgkit.ll.HgRepository; +import com.tmate.hgkit.ll.StatusCollector; +import com.tmate.hgkit.ll.WorkingCopyStatusCollector; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class TestStatus { + + private StatusOutputParser statusParser; + private ExecHelper eh; + private final HgRepository repo; + + public static void main(String[] args) throws Exception { + HgRepository repo = new RepositoryLookup().detectFromWorkingDir(); + TestStatus test = new TestStatus(repo); + test.testLowLevel(); + test.testStatusCommand(); + } + + public TestStatus(HgRepository hgRepo) { + repo = hgRepo; + statusParser = new StatusOutputParser(); + eh = new ExecHelper(statusParser, null); + } + + public void testLowLevel() throws Exception { + final WorkingCopyStatusCollector wcc = new WorkingCopyStatusCollector(repo, new FileWalker(new File(System.getProperty("user.dir")))); + eh.run("hg", "status", "-A"); + StatusCollector.Record r = wcc.status(HgRepository.TIP); + report("hg status -A", r, statusParser); + // + statusParser.reset(); + int revision = 3; + eh.run("hg", "status", "-A", "--rev", String.valueOf(revision)); + r = wcc.status(revision); + report("status -A --rev " + revision, r, statusParser); + // + statusParser.reset(); + eh.run("hg", "status", "-A", "--change", String.valueOf(revision)); + r = new StatusCollector.Record(); + new StatusCollector(repo).change(revision, r); + report("status -A --change " + revision, r, statusParser); + } + + public void testStatusCommand() throws Exception { + throw HgRepository.notImplemented(); + } + + private static void report(String what, StatusCollector.Record r, StatusOutputParser statusParser) { + System.out.println(">>>" + what); + reportNotEqual("MODIFIED", r.getModified(), statusParser.getModified()); + reportNotEqual("ADDED", r.getAdded(), statusParser.getAdded()); + reportNotEqual("REMOVED", r.getRemoved(), statusParser.getRemoved()); + reportNotEqual("CLEAN", r.getClean(), statusParser.getClean()); + reportNotEqual("IGNORED", r.getIgnored(), statusParser.getIgnored()); + reportNotEqual("MISSING", r.getMissing(), statusParser.getMissing()); + reportNotEqual("UNKNOWN", r.getUnknown(), statusParser.getUnknown()); + // TODO compare equals + System.out.println("<<<\n"); + } + + private static void reportNotEqual(String what, Collection l1, Collection l2) { + List diff = difference(l1, l2); + System.out.print(what); + if (!diff.isEmpty()) { + System.out.print(" are NOT the same: "); + for (T t : diff) { + System.out.print(t); + System.out.print(", "); + } + System.out.println(); + } else { + System.out.println(" are the same"); + } + } + + private static List difference(Collection l1, Collection l2) { + LinkedList result = new LinkedList(l2); + for (T t : l1) { + if (l2.contains(t)) { + result.remove(t); + } else { + result.add(t); + } + } + return result; + } +}