<commit_before>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.file;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class FileUtilTest {
	private LinkedList<Path> srcDirs;
	private LinkedList<Path> srcFiles;
	private LinkedList<Path> dstDirs;
	private LinkedList<Path> dstFiles;
	
	private Path baseDir;
	private Path srcDir;
	private Path dstDir;
	private Path simplePath;
	
	private static final String SIMPLE_PATH = "C:\\foo\\bar\\";
	
	@Before
	public void setUp() throws Exception {
		srcDirs = new LinkedList<>();
		srcFiles = new LinkedList<>();
		dstDirs = new LinkedList<>();
		dstFiles = new LinkedList<>();

		baseDir = Files.createTempDirectory("testFileUtil");

		srcDir = baseDir.resolve("src").resolve("test");
		srcDirs.add(srcDir);

		dstDir = baseDir.resolve("dst").resolve("test");
		dstDirs.add(dstDir);
		
		simplePath = Paths.get(SIMPLE_PATH);
	}

	@Test
	public void testPathTokenList_LocalFileName() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("C:\\test\\me\\testfile.txt");
		String mustContain[] = { "C:", "test", "me", "testfile", ".txt" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testPathTokenList_LocalFileName_specialChar() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("C:\\test\\me\\test.me\\testfile.txt");
		String mustContain[] = { "C:", "test", "me", "testfile", ".txt", "test.me" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testPathTokenList_NetworkFileName() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("\\\\nas\\test\\me\\test.me\\testfile.txt");
		String mustContain[] = { "\\\\nas", "test", "me", "testfile", ".txt", "test.me" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testDirectoryMover() throws IOException {

		/*
		 * \test │ a.txt │ └───dir1 │ b.txt │ └───dir2 c.txt
		 */

		buildStructure(srcDirs, srcFiles, srcDir);

		createFiles(srcDirs, srcFiles);

		// guard condition
		for (Path p : srcFiles) {
			assertTrue(p.toFile().exists());
		}

		// copy /src/test/ to /dst/
		FileUtil.moveDirectory(srcDir, dstDir.getParent());

		for (Path p : dstFiles) {
			assertTrue(p.toString(), p.toFile().exists());
		}
	}

	@Test
	public void testCopyFile() throws IOException {
		buildStructure(srcDirs, srcFiles, srcDir);

		createFiles(srcDirs, srcFiles);
		/*
		 * ...\srcDir\src\test\dir1\dir2\c.txt
		 * 
		 * to
		 * 
		 * ...\dstdir\...\srcDir\src\test\dir1\dir2\c.txt
		 */

		FileUtil.moveFileWithStructure(srcFiles.get(2), dstDir);
		assertTrue(Files.exists(dstDir.resolve(srcFiles.get(2).getRoot().relativize(srcFiles.get(2)))));
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileSourceNull() throws IOException {
		FileUtil.moveFileWithStructure(null, dstDir);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileDestinationNull() throws IOException {
		FileUtil.moveFileWithStructure(srcDir, null);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileDestinationNoRootComponent() throws IOException {
		Path noRoot = Paths.get("foo/");
		FileUtil.moveFileWithStructure(noRoot, dstDir);
	}

	private void buildStructure(LinkedList<Path> dirs, LinkedList<Path> files, Path base) {
		dirs.add(base.resolve("dir1"));
		dirs.add(base.resolve("dir1").resolve("dir2"));

		files.add(base.resolve("a.txt"));
		files.add(base.resolve("dir1").resolve("b.txt"));
		files.add(base.resolve("dir1").resolve("dir2").resolve("c.txt"));
	}

	private void createFiles(LinkedList<Path> dirs, LinkedList<Path> files) throws IOException {
		for (Path p : dirs) {
			p.toFile().mkdirs();
		}

		for (Path p : files) {
			p.toFile().createNewFile();
		}

	}

	@Test
	public void testRemoveDriveLetter() {
		Path pathToTest = buildAbsolutePath("test", "me", "now", "squirrel.jpg");
		Path path = FileUtil.removeDriveLetter(pathToTest);

		assertThat(path, is(buildRelativePath("test", "me", "now", "squirrel.jpg")));
	}

	private Path buildAbsolutePath(String... elements) {
		Path absolutePath = null;
		Iterable<Path> roots = FileSystems.getDefault().getRootDirectories();
		Path root = null;
<commit_msg>Check return values for File.mkDirs()<commit_after>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.file;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class FileUtilTest {
	private LinkedList<Path> srcDirs;
	private LinkedList<Path> srcFiles;
	private LinkedList<Path> dstDirs;
	private LinkedList<Path> dstFiles;
	
	private Path baseDir;
	private Path srcDir;
	private Path dstDir;
	private Path simplePath;
	
	private static final String SIMPLE_PATH = "C:\\foo\\bar\\";
	
	
	@Before
	public void setUp() throws Exception {
		srcDirs = new LinkedList<>();
		srcFiles = new LinkedList<>();
		dstDirs = new LinkedList<>();
		dstFiles = new LinkedList<>();

		baseDir = Files.createTempDirectory("testFileUtil");

		srcDir = baseDir.resolve("src").resolve("test");
		srcDirs.add(srcDir);

		dstDir = baseDir.resolve("dst").resolve("test");
		dstDirs.add(dstDir);
		
		simplePath = Paths.get(SIMPLE_PATH);
	}

	@Test
	public void testPathTokenList_LocalFileName() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("C:\\test\\me\\testfile.txt");
		String mustContain[] = { "C:", "test", "me", "testfile", ".txt" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testPathTokenList_LocalFileName_specialChar() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("C:\\test\\me\\test.me\\testfile.txt");
		String mustContain[] = { "C:", "test", "me", "testfile", ".txt", "test.me" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testPathTokenList_NetworkFileName() {
		LinkedList<String> result = (LinkedList<String>) FileUtil.pathTokenList("\\\\nas\\test\\me\\test.me\\testfile.txt");
		String mustContain[] = { "\\\\nas", "test", "me", "testfile", ".txt", "test.me" };
		assertThat(result, hasItems(mustContain));
	}

	@Test
	public void testDirectoryMover() throws IOException {

		/*
		 * \test │ a.txt │ └───dir1 │ b.txt │ └───dir2 c.txt
		 */

		buildStructure(srcDirs, srcFiles, srcDir);

		createFiles(srcDirs, srcFiles);

		// guard condition
		for (Path p : srcFiles) {
			assertTrue(p.toFile().exists());
		}

		// copy /src/test/ to /dst/
		FileUtil.moveDirectory(srcDir, dstDir.getParent());

		for (Path p : dstFiles) {
			assertTrue(p.toString(), p.toFile().exists());
		}
	}

	@Test
	public void testCopyFile() throws IOException {
		buildStructure(srcDirs, srcFiles, srcDir);

		createFiles(srcDirs, srcFiles);
		/*
		 * ...\srcDir\src\test\dir1\dir2\c.txt
		 * 
		 * to
		 * 
		 * ...\dstdir\...\srcDir\src\test\dir1\dir2\c.txt
		 */

		FileUtil.moveFileWithStructure(srcFiles.get(2), dstDir);
		assertTrue(Files.exists(dstDir.resolve(srcFiles.get(2).getRoot().relativize(srcFiles.get(2)))));
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileSourceNull() throws IOException {
		FileUtil.moveFileWithStructure(null, dstDir);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileDestinationNull() throws IOException {
		FileUtil.moveFileWithStructure(srcDir, null);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCopyFileDestinationNoRootComponent() throws IOException {
		Path noRoot = Paths.get("foo/");
		FileUtil.moveFileWithStructure(noRoot, dstDir);
	}

	private void buildStructure(LinkedList<Path> dirs, LinkedList<Path> files, Path base) {
		dirs.add(base.resolve("dir1"));
		dirs.add(base.resolve("dir1").resolve("dir2"));

		files.add(base.resolve("a.txt"));
		files.add(base.resolve("dir1").resolve("b.txt"));
		files.add(base.resolve("dir1").resolve("dir2").resolve("c.txt"));
	}

	private void createFiles(LinkedList<Path> dirs, LinkedList<Path> files) throws IOException {
		for (Path p : dirs) {
			if(p.toFile().mkdirs() ==false) {
				throw new IOException("Folder setup failed");
			}
		}

		for (Path p : files) {
			if (p.toFile().createNewFile()) {
				throw new IOException("File setup failed");
			}
		}
	}

	@Test
	public void testRemoveDriveLetter() {
		Path pathToTest = buildAbsolutePath("test", "me", "now", "squirrel.jpg");
		Path path = FileUtil.removeDriveLetter(pathToTest);

		assertThat(path, is(buildRelativePath("test", "me", "now", "squirrel.jpg")));
	}

	private Path buildAbsolutePath(String... elements) {
		Path absolutePath = null;
		Iterable<Path> roots = FileSystems.getDefault().getRootDirectories();
		Path root = null;

{'fraction_non_alphanumeric': 0.15700877371964905, 'fraction_numerical': 0.0038767598449296063, 'mean_word_length': 4.74282366725249, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '257426', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3249, 'n_words': 642}