Commits

mquigley committed c5b6cd2

Minor improvements and tweaks. A few more tests.

  • Participants
  • Parent commits baa7ce6
  • Branches Dev_0.0.2

Comments (0)

Files changed (6)

File src/main/java/com/quigley/filesystem/FilesystemInventoryVisitor.java

 	
 	public void visit(FilesystemPath path) {
 		boolean include = true;
-		if(includeTokens.size() > 0) {
+		if(includeTokens != null && includeTokens.size() > 0) {
 			include = false;
 			for(String includeToken : includeTokens) {
 				if(path.asString().indexOf(includeToken) != -1) {
 			}
 		}
 		boolean exclude = false;
-		if(excludeTokens.size() > 0) {
+		if(excludeTokens != null && excludeTokens.size() > 0) {
 			for(String excludeToken : excludeTokens) {
 				if(path.asString().indexOf(excludeToken) != -1) {
 					exclude = true;

File src/main/java/com/quigley/filesystem/FilesystemIterator.java

     }
 
 	private void visit(FilesystemPath path) {
-    	for(FilesystemVisitor visitor : visitors) {
-    		visitor.visit(path);
-    	}
+		if(visitors != null) {
+	    	for(FilesystemVisitor visitor : visitors) {
+	    		visitor.visit(path);
+	    	}
+		}
     }
 
     public List<FilesystemVisitor> getVisitors() {

File src/main/java/com/quigley/filesystem/FilesystemOperation.java

 package com.quigley.filesystem;
 
-import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.channels.FileChannel;
 import java.util.ArrayList;
 import java.util.List;
 
+import com.quigley.filesystem.utils.StreamCopier;
+
 public class FilesystemOperation {
 	public static void copy(FilesystemPath source, FilesystemPath dest) throws FilesystemException {
 		copy(source.asFile(), dest.asFile());
 		}
 	}
 	
+	public static List<FilesystemPath> copyTree(FilesystemPath sourceTree, FilesystemPath destTree) {
+		return copyTree(sourceTree, destTree, null, null);
+	}
+	
 	public static List<FilesystemPath> copyTree(FilesystemPath sourceTree, FilesystemPath destTree, List<String> includeTokens, List<String> excludeTokens) throws FilesystemException {
 		FilesystemInventoryVisitor visitor = new FilesystemInventoryVisitor();
 		visitor.setExcludeTokens(excludeTokens);
 		visitor.setIncludeTokens(includeTokens);
+		visitor.setRootTrim(sourceTree);
 		visitor.setIncludeDirectories(false);
 		FilesystemIterator iterator = new FilesystemIterator(visitor);
 		iterator.iterate(sourceTree);
 		
 		List<FilesystemPath> copiedFiles = new ArrayList<FilesystemPath>();
 		for(FilesystemPath sourcePath : visitor.getPaths()) {
-			FilesystemPath relativeSourcePath = FilesystemPath.removeCommonParent(sourcePath, sourceTree);
-			copiedFiles.add(relativeSourcePath);
-			FilesystemPath destPath = destTree.add(relativeSourcePath);
+			copiedFiles.add(sourcePath);
+			FilesystemPath destPath = destTree.add(sourcePath);
 			destPath.parent().asFile().mkdirs();
-			copy(sourcePath, destPath);
+			copy(sourceTree.add(sourcePath), destPath);
 		}
 		
 		return copiedFiles;
 	}
 
 	public static String readFileAsString(File path) throws IOException {
-		StringBuilder fileData = new StringBuilder(1000);
-		BufferedReader reader = new BufferedReader(new FileReader(path));
-
-		char[] buf = new char[1024];
-		int numRead = 0;
-		while ((numRead = reader.read(buf)) != -1) {
-			fileData.append(buf, 0, numRead);
-		}
-		reader.close();
-
-		return fileData.toString();
+		FileInputStream in = new FileInputStream(path);
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		StreamCopier.copy(in, out);
+		in.close();
+		out.close();
+		return new String(out.toByteArray());
 	}
 	
 	public static void writeStringToFile(String data, File path) throws IOException {
-		FileWriter writer = new FileWriter(path);
-		writer.write(data);
-		writer.close();
+		ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
+		FileOutputStream out = new FileOutputStream(path);
+		StreamCopier.copy(in, out);
+		in.close();
+		out.close();
+	}
+	
+	public static boolean recursiveDelete(FilesystemPath path) {
+		return recursiveDelete(path.asFile());
 	}
 	
 	public static boolean recursiveDelete(File path) {

File src/main/java/com/quigley/filesystem/FilesystemSizeVisitor.java

+package com.quigley.filesystem;
+
+public class FilesystemSizeVisitor implements FilesystemVisitor {
+	public void visit(FilesystemPath path) {
+		System.out.println("visiting: " + path);
+		size += path.asFile().length();
+	}
+	
+	public long getSize() {
+		return size;
+	}
+
+	private long size;
+}

File src/test/java/com/quigley/filesystem/FilesystemOperationTest.java

+package com.quigley.filesystem;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FilesystemOperationTest {
+	@Test
+	public void testCopyFile() throws IOException {
+		FilesystemPath testRoot = createTempFolder();
+		log.info("testRoot = " + testRoot);
+		
+		FilesystemPath src = new FilesystemPath("pom.xml");
+		FilesystemPath dest = testRoot.add(new FilesystemPath("pom.xml"));
+		FilesystemOperation.copy(src, dest);
+		
+		assertTrue(dest.asFile().exists() && dest.asFile().isFile());
+		
+		assertTrue(FilesystemOperation.recursiveDelete(testRoot));
+		assertFalse(testRoot.asFile().exists());
+	}
+	
+	@Test
+	public void testCopyTree() throws IOException {
+		FilesystemPath dest = createTempFolder();
+		log.info("dest = " + dest);
+
+		FilesystemPath src = new FilesystemPath("src/test/java");
+		List<FilesystemPath> copies = FilesystemOperation.copyTree(src, dest);
+		
+		for(FilesystemPath f : copies) {
+			log.info("copied: " + f);
+		}
+		
+		FilesystemInventoryVisitor v1 = new FilesystemInventoryVisitor();
+		v1.setRootTrim(src);
+		FilesystemIterator i1 = new FilesystemIterator();
+		i1.addVisitor(v1);
+		i1.iterate(src);
+		List<String> s1 = new ArrayList<String>();
+		for(FilesystemPath path : v1.getPaths()) {
+			s1.add(path.toString());
+		}
+		Collections.sort(s1);
+		
+		for(String s : s1) {
+			log.info("s1 = " + s);
+		}
+		
+		FilesystemInventoryVisitor v2 = new FilesystemInventoryVisitor();
+		v2.setRootTrim(dest);
+		FilesystemIterator i2 = new FilesystemIterator();
+		i2.addVisitor(v2);
+		i2.iterate(dest);
+		List<String> s2 = new ArrayList<String>();
+		for(FilesystemPath path : v2.getPaths()) {
+			s2.add(path.toString());
+		}
+		Collections.sort(s2);
+
+		for(String s : s2) {
+			log.info("s2 = " + s);
+		}
+		
+		assertTrue(s1.equals(s2));
+		
+		assertTrue(FilesystemOperation.recursiveDelete(dest));
+		assertFalse(dest.asFile().exists());		
+	}
+	
+	private FilesystemPath createTempFolder() throws IOException {
+		File tempF = File.createTempFile("temp", ".dir");
+		tempF.delete();
+		tempF.mkdir();
+		return new FilesystemPath(tempF.getAbsolutePath());
+	}
+	
+	private static Logger log = LoggerFactory.getLogger(FilesystemOperationTest.class);
+}

File src/test/java/com/quigley/filesystem/FilesystemVisitorTest.java

+package com.quigley.filesystem;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FilesystemVisitorTest {
+	@Test
+	public void testFilesystemSizeVisitorOnTree() {
+		FilesystemSizeVisitor visitor = new FilesystemSizeVisitor();
+		FilesystemIterator iterator = new FilesystemIterator(visitor);
+		iterator.iterate(new FilesystemPath("src/main"));
+		log.info("src/main = " + FilesystemSizeFormatter.format(visitor.getSize()));
+	}
+	
+	private static Logger log = LoggerFactory.getLogger(FilesystemVisitorTest.class);
+}