Cleanup (FileUtil tests grouped)

This commit is contained in:
Roman Shevchenko
2013-10-09 13:46:07 +02:00
parent 9fdcf659f2
commit 569a70d33b
3 changed files with 162 additions and 220 deletions
@@ -1,105 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.util.io;
import com.intellij.openapi.util.text.StringUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author lene
* @since 29.03.11
*/
public class FileUtilFindFileTest {
private static File myTempFile;
private static File myFirstFile;
private static File mySecondFile;
@BeforeClass
public static void setUp() throws Exception {
myTempFile = FileUtil.createTempDirectory("tEF", "",false); //NON-NLS
myFirstFile = new File(myTempFile, "first");
mySecondFile = new File(myTempFile, "second"); //NON-NLS
assertTrue(myFirstFile.createNewFile());
assertTrue(mySecondFile.createNewFile());
}
@AfterClass
public static void tearDown() throws Exception {
FileUtil.delete(myTempFile);
}
@Test
public void nonExistingFileInNonExistentDirectory() throws Exception {
String path = FileUtil.findFileInProvidedPath("123", "zero");//NON-NLS
assertTrue(StringUtil.isEmpty(path));
}
@Test
public void nonExistingFileInDirectory() throws Exception {
String path = FileUtil.findFileInProvidedPath(myTempFile.getAbsolutePath(), "zero");//NON-NLS
assertTrue(StringUtil.isEmpty(path));
}
@Test
public void nonExistingFile() throws Exception {
String path =
FileUtil.findFileInProvidedPath(myFirstFile.getAbsolutePath() + "123", myFirstFile.getName() + "123");
assertTrue(StringUtil.isEmpty(path));
}
@Test
public void existingFileInDirectory() throws Exception {
String path = FileUtil.findFileInProvidedPath(myTempFile.getAbsolutePath(), "first");
assertEquals(path, myFirstFile.getAbsolutePath());
}
@Test
public void existingFile() throws Exception {
String path = FileUtil.findFileInProvidedPath(myFirstFile.getAbsolutePath(), "first");
assertEquals(path, myFirstFile.getAbsolutePath());
}
@Test
public void twoFilesOrderInDirectory() throws Exception {
String path = FileUtil.findFileInProvidedPath(myTempFile.getAbsolutePath(), "first", "second"); //NON-NLS
assertEquals(path, myFirstFile.getAbsolutePath());
}
@Test
public void twoFilesOrderInDirectory2() throws Exception {
String path = FileUtil.findFileInProvidedPath(myTempFile.getAbsolutePath(), "second", "first"); //NON-NLS
assertEquals(path, mySecondFile.getAbsolutePath());
}
@Test
public void twoFilesOrder() throws Exception {
String path = FileUtil.findFileInProvidedPath(myFirstFile.getAbsolutePath(), "first", "second");//NON-NLS
assertEquals(path, myFirstFile.getAbsolutePath());
}
@Test
public void twoFilesOrder2() throws Exception {
String path = FileUtil.findFileInProvidedPath(myFirstFile.getAbsolutePath(), "second", "first"); //NON-NLS
assertEquals(path, myFirstFile.getAbsolutePath());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,10 @@
*/
package com.intellij.openapi.util.io;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Processor;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
@@ -26,69 +26,85 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* Created with IntelliJ IDEA.
* User: Irina.Chernushina
* Date: 11/22/12
* Time: 11:41 AM
* @author Irina.Chernushina, lene
*/
public class FileUtilHeavyTest {
private File myTempDirectory;
private static File myTempDirectory;
private static File myVisitorTestDirectory;
private static File myFindTestDirectory;
private static File myFindTestFirstFile;
private static File myFindTestSecondFile;
@Before
public void setUp() throws Exception {
myTempDirectory = FileUtil.createTempDirectory(getClass().getSimpleName() + ".", ".tmp");
@BeforeClass
public static void setUp() throws Exception {
myTempDirectory = FileUtil.createTempDirectory("FileUtilHeavyTest.", ".tmp");
myVisitorTestDirectory = IoTestUtil.createTestDir(myTempDirectory, "visitor_test_dir");
File dir1 = IoTestUtil.createTestDir(myVisitorTestDirectory, "dir1");
IoTestUtil.createTestFile(dir1, "1");
IoTestUtil.createTestFile(dir1, "2");
File dir2 = IoTestUtil.createTestDir(myVisitorTestDirectory, "dir2");
IoTestUtil.createTestFile(dir2, "1");
IoTestUtil.createTestFile(dir2, "2");
File dir21 = IoTestUtil.createTestDir(dir2, "inner");
IoTestUtil.createTestFile(dir21, "1");
IoTestUtil.createTestFile(dir21, "2");
myFindTestDirectory = IoTestUtil.createTestDir(myTempDirectory, "find_file_test_dir");
myFindTestFirstFile = IoTestUtil.createTestFile(myFindTestDirectory, "first");
myFindTestSecondFile = IoTestUtil.createTestFile(myFindTestDirectory, "second");
}
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() {
if (myTempDirectory != null) {
FileUtil.delete(myTempDirectory);
}
}
@Test
public void testSimpleRecursiveIteration() throws Exception {
final Tree tree = new Tree(myTempDirectory);
public void testProcessSimple() {
final Map<String, Integer> result = new HashMap<String, Integer>();
FileUtil.processFilesRecursively(myTempDirectory, new Processor<File>() {
FileUtil.processFilesRecursively(myVisitorTestDirectory, new Processor<File>() {
@Override
public boolean process(File file) {
final Integer integer = result.get(file.getName());
Integer integer = result.get(file.getName());
result.put(file.getName(), integer == null ? 1 : (integer + 1));
return true;
}
});
Assert.assertEquals(6, result.size());
Assert.assertEquals(1, result.get(myTempDirectory.getName()).intValue());
Assert.assertEquals(3, result.get("1").intValue());
Assert.assertEquals(3, result.get("2").intValue());
Assert.assertEquals(1, result.get("dir1").intValue());
assertEquals(6, result.size());
assertEquals(1, result.get(myVisitorTestDirectory.getName()).intValue());
assertEquals(3, result.get("1").intValue());
assertEquals(3, result.get("2").intValue());
assertEquals(1, result.get("dir1").intValue());
}
@Test
public void testStops() throws Exception {
final Tree tree = new Tree(myTempDirectory);
public void testProcessStops() {
final int[] cnt = new int[]{0};
FileUtil.processFilesRecursively(myTempDirectory, new Processor<File>() {
FileUtil.processFilesRecursively(myVisitorTestDirectory, new Processor<File>() {
@Override
public boolean process(File file) {
++ cnt[0];
++cnt[0];
return false;
}
});
Assert.assertEquals(1, cnt[0]);
assertEquals(1, cnt[0]);
}
@Test
public void testDirectoryFilter() throws Exception {
final Tree tree = new Tree(myTempDirectory);
public void testProcessDirectoryFilter() {
final Map<String, Integer> result = new HashMap<String, Integer>();
FileUtil.processFilesRecursively(myTempDirectory, new Processor<File>() {
FileUtil.processFilesRecursively(myVisitorTestDirectory, new Processor<File>() {
@Override
public boolean process(File file) {
final Integer integer = result.get(file.getName());
Integer integer = result.get(file.getName());
result.put(file.getName(), integer == null ? 1 : (integer + 1));
return true;
}
@@ -98,54 +114,100 @@ public class FileUtilHeavyTest {
return ! "dir2".equals(file.getName());
}
});
Assert.assertEquals(5, result.size());
Assert.assertEquals(1, result.get(myTempDirectory.getName()).intValue());
Assert.assertEquals(1, result.get("1").intValue());
Assert.assertEquals(1, result.get("2").intValue());
Assert.assertEquals(1, result.get("dir1").intValue());
Assert.assertEquals(1, result.get("dir2").intValue());
Assert.assertNull(result.get("dir21"));
assertEquals(5, result.size());
assertEquals(1, result.get(myVisitorTestDirectory.getName()).intValue());
assertEquals(1, result.get("1").intValue());
assertEquals(1, result.get("2").intValue());
assertEquals(1, result.get("dir1").intValue());
assertEquals(1, result.get("dir2").intValue());
assertNull(result.get("dir21"));
}
private static class Tree {
private final File dir1;
private final File file11;
private final File file12;
@Test
public void nonExistingFileInNonExistentDirectory() {
String path = FileUtil.findFileInProvidedPath("123", "zero");
assertTrue(StringUtil.isEmpty(path));
}
private final File dir2;
private final File file21;
private final File file22;
@Test
public void nonExistingFileInDirectory() {
String path = FileUtil.findFileInProvidedPath(myFindTestDirectory.getAbsolutePath(), "zero");
assertTrue(StringUtil.isEmpty(path));
}
private final File dir21;
private final File file211;
private final File file212;
@Test
public void nonExistingFile() {
String path = FileUtil.findFileInProvidedPath(myFindTestFirstFile.getAbsolutePath() + "123", myFindTestFirstFile.getName() + "123");
assertTrue(StringUtil.isEmpty(path));
}
private Tree(final File root) throws IOException {
dir1 = new File(root, "dir1");
dir2 = new File(root, "dir2");
dir21 = new File(dir2, "inner");
@Test
public void existingFileInDirectory() {
String path = FileUtil.findFileInProvidedPath(myFindTestDirectory.getAbsolutePath(), "first");
assertEquals(path, myFindTestFirstFile.getAbsolutePath());
}
Assert.assertTrue(dir1.mkdir());
Assert.assertTrue(dir2.mkdir());
Assert.assertTrue(dir21.mkdir());
@Test
public void existingFile() {
String path = FileUtil.findFileInProvidedPath(myFindTestFirstFile.getAbsolutePath(), "first");
assertEquals(path, myFindTestFirstFile.getAbsolutePath());
}
file11 = new File(dir1, "1");
file12 = new File(dir1, "2");
@Test
public void twoFilesOrderInDirectory() {
String path = FileUtil.findFileInProvidedPath(myFindTestDirectory.getAbsolutePath(), "first", "second");
assertEquals(path, myFindTestFirstFile.getAbsolutePath());
}
file21 = new File(dir2, "1");
file22 = new File(dir2, "2");
@Test
public void twoFilesOrderInDirectory2() {
String path = FileUtil.findFileInProvidedPath(myFindTestDirectory.getAbsolutePath(), "second", "first");
assertEquals(path, myFindTestSecondFile.getAbsolutePath());
}
file211 = new File(dir21, "1");
file212 = new File(dir21, "2");
@Test
public void twoFilesOrder() {
String path = FileUtil.findFileInProvidedPath(myFindTestFirstFile.getAbsolutePath(), "first", "second");
assertEquals(path, myFindTestFirstFile.getAbsolutePath());
}
file11.createNewFile();
file12.createNewFile();
@Test
public void twoFilesOrder2() {
String path = FileUtil.findFileInProvidedPath(myFindTestFirstFile.getAbsolutePath(), "second", "first");
assertEquals(path, myFindTestFirstFile.getAbsolutePath());
}
file21.createNewFile();
file22.createNewFile();
@Test
public void testRepeatableOperation() throws IOException {
abstract class CountableIOOperation implements FileUtilRt.RepeatableIOOperation<Boolean, IOException> {
private int count = 0;
file211.createNewFile();
file212.createNewFile();
@Override
public Boolean execute(boolean lastAttempt) throws IOException {
count++;
return stop(lastAttempt) ? true : null;
}
protected abstract boolean stop(boolean lastAttempt);
}
CountableIOOperation successful = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return true; }
};
FileUtilRt.doIOOperation(successful);
assertEquals(1, successful.count);
CountableIOOperation failed = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return false; }
};
FileUtilRt.doIOOperation(failed);
assertEquals(10, failed.count);
CountableIOOperation lastShot = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return lastAttempt; }
};
FileUtilRt.doIOOperation(lastShot);
assertEquals(10, lastShot.count);
}
}
@@ -22,13 +22,13 @@ import com.intellij.util.ThreeState;
import com.intellij.util.containers.Convertor;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.*;
public class FileUtilTest {
public class FileUtilLightTest {
private static final char UNIX_SEPARATOR = '/';
private static final char WINDOWS_SEPARATOR = '\\';
@@ -102,62 +102,47 @@ public class FileUtilTest {
@Test
public void testRemoveAncestors() throws Exception {
final String[] arr = {"/a/b/c", "/a", "/a/b", "/d/e", "/b/c", "/a/d", "/b/c/ttt", "/a/ewq.euq"};
final String[] expectedResult = {"/a","/b/c","/d/e"};
@SuppressWarnings("unchecked") final Collection<String> result = FileUtil.removeAncestors(Arrays.asList(arr), Convertor.SELF, PairProcessor.TRUE);
assertArrayEquals(expectedResult, ArrayUtil.toStringArray(result));
List<String> data = Arrays.asList("/a/b/c", "/a", "/a/b", "/d/e", "/b/c", "/a/d", "/b/c/ttt", "/a/ewq.euq");
String[] expected = {"/a","/b/c","/d/e"};
@SuppressWarnings("unchecked") Collection<String> result = FileUtil.removeAncestors(data, Convertor.SELF, PairProcessor.TRUE);
assertArrayEquals(expected, ArrayUtil.toStringArray(result));
}
@Test
public void testCheckImmediateChildren() throws Exception {
final String root = "/a";
final String[] arr = {"/a/b/c", "/a", "/a/b", "/d/e", "/b/c", "/a/d", "/a/b/c/d/e"};
final ThreeState[] expectedResult = {ThreeState.UNSURE, ThreeState.YES, ThreeState.YES, ThreeState.NO, ThreeState.NO, ThreeState.YES, ThreeState.UNSURE};
final ThreeState[] expectedResult2 = {ThreeState.UNSURE, ThreeState.NO, ThreeState.YES, ThreeState.NO, ThreeState.NO, ThreeState.YES, ThreeState.UNSURE};
String root = "/a";
String[] data = {"/a/b/c", "/a", "/a/b", "/d/e", "/b/c", "/a/d", "/a/b/c/d/e"};
ThreeState[] expected1 = {ThreeState.UNSURE, ThreeState.YES, ThreeState.YES, ThreeState.NO, ThreeState.NO, ThreeState.YES, ThreeState.UNSURE};
ThreeState[] expected2 = {ThreeState.UNSURE, ThreeState.NO, ThreeState.YES, ThreeState.NO, ThreeState.NO, ThreeState.YES, ThreeState.UNSURE};
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
final ThreeState state = FileUtil.isAncestorThreeState(root, s, false);
assertEquals(String.valueOf(i), expectedResult[i], state);
for (int i = 0; i < data.length; i++) {
ThreeState state = FileUtil.isAncestorThreeState(root, data[i], false);
assertEquals(String.valueOf(i), expected1[i], state);
}
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
final ThreeState state = FileUtil.isAncestorThreeState(root, s, true);
assertEquals(String.valueOf(i), expectedResult2[i], state);
for (int i = 0; i < data.length; i++) {
ThreeState state = FileUtil.isAncestorThreeState(root, data[i], true);
assertEquals(String.valueOf(i), expected2[i], state);
}
}
@Test
public void testRepeatableOperation() throws Exception {
abstract class CountableIOOperation implements FileUtilRt.RepeatableIOOperation<Boolean, IOException> {
private int count = 0;
public void testStartsWith() {
assertTrue(FileUtil.startsWith("/usr/local/jeka", "/usr/local/jeka"));
assertTrue(FileUtil.startsWith("/usr/local/jeka", "/usr/local/"));
assertTrue(FileUtil.startsWith("/usr/local/jeka", "/usr/"));
assertTrue(FileUtil.startsWith("/usr/local/jeka", "/usr"));
assertTrue(FileUtil.startsWith("/usr/local/jeka", "/"));
assertTrue(FileUtil.startsWith("c:/idea", "c:/"));
assertTrue(FileUtil.startsWith("c:/idea", "c:"));
assertTrue(FileUtil.startsWith("c:/idea", ""));
assertTrue(FileUtil.startsWith("c:/idea/x", "C:/IDEA", false));
@Override
public Boolean execute(boolean lastAttempt) throws IOException {
count++;
return stop(lastAttempt) ? true : null;
}
protected abstract boolean stop(boolean lastAttempt);
}
CountableIOOperation successful = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return true; }
};
FileUtilRt.doIOOperation(successful);
assertEquals(1, successful.count);
CountableIOOperation failed = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return false; }
};
FileUtilRt.doIOOperation(failed);
assertEquals(10, failed.count);
CountableIOOperation lastShot = new CountableIOOperation() {
@Override protected boolean stop(boolean lastAttempt) { return lastAttempt; }
};
FileUtilRt.doIOOperation(lastShot);
assertEquals(10, lastShot.count);
assertFalse(FileUtil.startsWith("/usr/local/jeka", "/usr/local/jek"));
assertFalse(FileUtil.startsWith("/usr/local/jeka", "/usr/local/aaa"));
assertFalse(FileUtil.startsWith("/usr/local/jeka", "/usr/local/jeka/"));
assertFalse(FileUtil.startsWith("/usr/local/jeka", "/aaa"));
assertFalse(FileUtil.startsWith("c:/idea2", "c:/idea"));
assertFalse(FileUtil.startsWith("c:/idea_branches/i18n", "c:/idea"));
}
}