mirror of https://github.com/alibaba/arthas.git
Testcase for file utils
commit
89e39fb2fc
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.taobao.arthas.core.testtool;
|
||||
|
||||
import com.taobao.arthas.core.shell.cli.CliToken;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author earayu
|
||||
*/
|
||||
public class TestUtils {
|
||||
|
||||
public static <T> List<T> newArrayList(T ... items){
|
||||
List<T> list = new ArrayList<T>();
|
||||
if(items!=null) {
|
||||
Collections.addAll(list, items);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* copied from https://github.com/apache/commons-io/blob/master/src/test/java/org/apache/commons/io/testtools/TestUtils.java
|
||||
* Assert that the content of a file is equal to that in a byte[].
|
||||
*
|
||||
* @param b0 the expected contents
|
||||
* @param file the file to check
|
||||
* @throws IOException If an I/O error occurs while reading the file contents
|
||||
*/
|
||||
public static void assertEqualContent(final byte[] b0, final File file) throws IOException {
|
||||
int count = 0, numRead = 0;
|
||||
final byte[] b1 = new byte[b0.length];
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
while (count < b0.length && numRead >= 0) {
|
||||
numRead = is.read(b1, count, b0.length);
|
||||
count += numRead;
|
||||
}
|
||||
Assert.assertEquals("Different number of bytes: ", b0.length, count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
Assert.assertEquals("byte " + i + " differs", b0[i], b1[i]);
|
||||
}
|
||||
}finally {
|
||||
if(is!=null){
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package com.taobao.arthas.core.util;
|
||||
|
||||
import com.taobao.arthas.core.testtool.TestUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
import static org.hamcrest.CoreMatchers.startsWith;
|
||||
|
||||
public class FileUtilsTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private File getTestDirectory() {
|
||||
return temporaryFolder.getRoot();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetTestDirectory(){
|
||||
Assert.assertNotNull(getTestDirectory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenOutputStreamIsDirectory() throws IOException {
|
||||
thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' exists but is a directory")));
|
||||
FileUtils.openOutputStream(getTestDirectory(), true);
|
||||
|
||||
thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' exists but is a directory")));
|
||||
FileUtils.openOutputStream(getTestDirectory(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenOutputStreamCannotWrite() throws IOException {
|
||||
thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' cannot be written to")));
|
||||
File targetFile = temporaryFolder.newFile("cannotWrite.txt");
|
||||
targetFile.setWritable(false);
|
||||
FileUtils.openOutputStream(targetFile, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenOutputStream() throws IOException {
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileOutputStream outputStream = FileUtils.openOutputStream(targetFile, true);
|
||||
Assert.assertNotNull(outputStream);
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteByteArrayToFile() throws IOException {
|
||||
String data = "test data";
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileUtils.writeByteArrayToFile(targetFile, data.getBytes());
|
||||
TestUtils.assertEqualContent(data.getBytes(), targetFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteByteArrayToFileWithAppend() throws IOException {
|
||||
String data = "test data";
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileUtils.writeByteArrayToFile(targetFile, data.getBytes(), true);
|
||||
TestUtils.assertEqualContent(data.getBytes(), targetFile);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testReadFileToString() throws IOException {
|
||||
String data = "test data";
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileUtils.writeByteArrayToFile(targetFile, data.getBytes(), true);
|
||||
String content = FileUtils.readFileToString(targetFile, Charset.defaultCharset());
|
||||
TestUtils.assertEqualContent(content.getBytes(), targetFile);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSaveCommandHistory() throws IOException {
|
||||
//cls
|
||||
int[] command1 = new int[]{99,108,115};
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileUtils.saveCommandHistory(TestUtils.newArrayList(command1), targetFile);
|
||||
TestUtils.assertEqualContent("cls\n".getBytes(), targetFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadCommandHistory() throws IOException {
|
||||
//cls
|
||||
int[] command1 = new int[]{99,108,115};
|
||||
File targetFile = temporaryFolder.newFile("targetFile.txt");
|
||||
FileUtils.saveCommandHistory(TestUtils.newArrayList(command1), targetFile);
|
||||
List<int[]> content = FileUtils.loadCommandHistory(targetFile);
|
||||
Assert.assertArrayEquals(command1, content.get(0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue