diff test/org/tmatesoft/hg/test/StatusOutputParser.java @ 93:d55d4eedfc57

Switch to Path instead of String in filenames returned by various status operations
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 27 Jan 2011 21:15:21 +0100
parents 61eedab3eb3e
children af1f3b78b918
line wrap: on
line diff
--- a/test/org/tmatesoft/hg/test/StatusOutputParser.java	Thu Jan 27 06:31:58 2011 +0100
+++ b/test/org/tmatesoft/hg/test/StatusOutputParser.java	Thu Jan 27 21:15:21 2011 +0100
@@ -17,14 +17,17 @@
 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;
 
+import org.tmatesoft.hg.core.Path;
+import org.tmatesoft.hg.repo.StatusCollector;
+import org.tmatesoft.hg.util.PathPool;
+import org.tmatesoft.hg.util.PathRewrite;
+
 /**
  *
  * @author Artem Tikhomirov
@@ -33,61 +36,70 @@
 public class StatusOutputParser implements OutputParser {
 
 	private final Pattern pattern;
-	private List<String> modified, added, removed, clean, missing, unknown, ignored;
-	private Map<String, String> copied;
-	private final boolean winPathSeparator;
+	// although using StatusCollector.Record is not really quite honest for testing,
+	// it's deemed acceptable as long as that class is primitive 'collect all results'
+	private StatusCollector.Record result = new StatusCollector.Record();
+	private final PathPool pathHelper;
 
 	public StatusOutputParser() {
 //		pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE);
 		pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE);
-		winPathSeparator = File.separatorChar == '\\';
+		pathHelper = new PathPool(new PathRewrite() {
+			
+			private final boolean winPathSeparator = File.separatorChar == '\\';
+
+			public String rewrite(String s) {
+				if (winPathSeparator) {
+					// Java impl always give slashed path, while Hg uses local, os-specific convention
+					s = s.replace('\\', '/'); 
+				}
+				return s;
+			}
+		});
 	}
 
 	public void reset() {
-		modified = added = removed = clean = missing = unknown = ignored = null;
-		copied = null;
+		result = new StatusCollector.Record();
 	}
 
 	public void parse(CharSequence seq) {
 		Matcher m = pattern.matcher(seq);
+		Path lastAdded = null;
 		while (m.find()) {
 			String fname = m.group(2);
 			switch ((int) m.group(1).charAt(0)) {
 			case (int) 'M' : {
-				modified = doAdd(modified, fname);
+				result.modified(pathHelper.path(fname));
 				break;
 			}
 			case (int) 'A' : {
-				added = doAdd(added, fname);
+				result.added(lastAdded = pathHelper.path(fname));
 				break;
 			}
 			case (int) 'R' : {
-				removed = doAdd(removed, fname);
+				result.removed(pathHelper.path(fname));
 				break;
 			}
 			case (int) '?' : {
-				unknown = doAdd(unknown, fname);
+				result.unknown(pathHelper.path(fname));
 				break;
 			}
 			case (int) 'I' : {
-				ignored = doAdd(ignored, fname);
+				result.ignored(pathHelper.path(fname));
 				break;
 			}
 			case (int) 'C' : {
-				clean = doAdd(clean, fname);
+				result.clean(pathHelper.path(fname));
 				break;
 			}
 			case (int) '!' : {
-				missing = doAdd(missing, fname);
+				result.missing(pathHelper.path(fname));
 				break;
 			}
 			case (int) ' ' : {
-				if (copied == null) {
-					copied = new TreeMap<String, String>();
-				}
 				// last added is copy destination
 				// to get or to remove it - depends on what StatusCollector does in this case
-				copied.put(added.get(added.size() - 1), toJavaImplConvention(fname));
+				result.copied(pathHelper.path(fname), lastAdded);
 				break;
 			}
 			}
@@ -95,61 +107,39 @@
 	}
 
 	// 
-	public List<String> getModified() {
-		return proper(modified);
-	}
-
-	public List<String> getAdded() {
-		return proper(added);
-	}
-
-	public List<String> getRemoved() {
-		return proper(removed);
-	}
-
-	public Map<String,String> getCopied() {
-		if (copied == null) {
-			return Collections.emptyMap();
-		}
-		return Collections.unmodifiableMap(copied);
-	}
-
-	public List<String> getClean() {
-		return proper(clean);
-	}
-
-	public List<String> getMissing() {
-		return proper(missing);
+	public List<Path> getModified() {
+		return result.getModified();
 	}
 
-	public List<String> getUnknown() {
-		return proper(unknown);
-	}
-
-	public List<String> getIgnored() {
-		return proper(ignored);
-	}
-	
-	private List<String> proper(List<String> l) {
-		if (l == null) {
-			return Collections.emptyList();
+	public List<Path> getAdded() {
+		List<Path> rv = new LinkedList<Path>(result.getAdded());
+		for (Path p : result.getCopied().keySet()) {
+			rv.remove(p); // remove only one duplicate
 		}
-		return Collections.unmodifiableList(l);
+		return rv;
 	}
 
-	private List<String> doAdd(List<String> l, String s) {
-		if (l == null) {
-			l = new LinkedList<String>();
-		}
-		l.add(toJavaImplConvention(s));
-		return l;
+	public List<Path> getRemoved() {
+		return result.getRemoved();
 	}
 
-	private String toJavaImplConvention(String s) {
-		if (winPathSeparator) {
-			// Java impl always give slashed path, while Hg uses local, os-specific convention
-			s = s.replace('\\', '/'); 
-		}
-		return s;
+	public Map<Path,Path> getCopied() {
+		return result.getCopied();
+	}
+
+	public List<Path> getClean() {
+		return result.getClean();
+	}
+
+	public List<Path> getMissing() {
+		return result.getMissing();
+	}
+
+	public List<Path> getUnknown() {
+		return result.getUnknown();
+	}
+
+	public List<Path> getIgnored() {
+		return result.getIgnored();
 	}
 }