edit the function of readFile to read files with the specific file
prefix, and add the test and pass it.
This commit is contained in:
parent
4daa76633e
commit
c4195d93da
|
@ -0,0 +1 @@
|
|||
test
|
|
@ -1,6 +1,8 @@
|
|||
package org.bench4q.agent.storage;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -53,8 +55,28 @@ public class DoubleBufferStorage {
|
|||
this.setCurrentBufferIndex(0);
|
||||
}
|
||||
|
||||
public String readFile(String path) {
|
||||
return this.getLocalStorage().readFile(path);
|
||||
public String readFiles(String pathPrefix) {
|
||||
int pos = pathPrefix.lastIndexOf(System.getProperty("file.separator"));
|
||||
String dirPath = pathPrefix.substring(0, pos);
|
||||
final String prefix = pathPrefix.substring(pos + 1);
|
||||
String result = new String();
|
||||
|
||||
File dirFile = new File(dirPath);
|
||||
if (!dirFile.exists()) {
|
||||
return "";
|
||||
}
|
||||
File[] files = dirFile.listFiles(new FilenameFilter() {
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.contains(prefix);
|
||||
}
|
||||
});
|
||||
for (File file : files) {
|
||||
if (!file.exists()) {
|
||||
continue;
|
||||
}
|
||||
result += this.getLocalStorage().readFile(file.getAbsolutePath());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean writeFile(String content, String path) {
|
||||
|
@ -64,7 +86,7 @@ public class DoubleBufferStorage {
|
|||
byte[] cache = new byte[THRESHOLD];
|
||||
while ((size = inputStream.read(cache, 0, THRESHOLD)) != -1) {
|
||||
if (isCurrentBufferFull(size)) {
|
||||
doSave(regeneratePath(path));
|
||||
doSave(calculateSavePath(path));
|
||||
changeToTheMaxRemainBuffer();
|
||||
}
|
||||
this.getCurrentBuffer().writeToCurrentBuffer(size, cache);
|
||||
|
@ -76,12 +98,12 @@ public class DoubleBufferStorage {
|
|||
return getCurrentBuffer().getRemainSize() <= THRESHOLD + size;
|
||||
}
|
||||
|
||||
public String regeneratePath(String path, int index) {
|
||||
public String calculateSavePath(String path, int index) {
|
||||
return path.substring(0, path.lastIndexOf(".")) + "_" + index + ".xml";
|
||||
}
|
||||
|
||||
private String regeneratePath(String path) {
|
||||
return regeneratePath(path, this.getCurrentBufferIndex());
|
||||
private String calculateSavePath(String path) {
|
||||
return calculateSavePath(path, this.getCurrentBufferIndex());
|
||||
}
|
||||
|
||||
private void doSave(final String path) {
|
||||
|
@ -128,4 +150,8 @@ public class DoubleBufferStorage {
|
|||
private Buffer getCurrentBuffer() {
|
||||
return this.getBufferList().get(this.getCurrentBufferIndex());
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.bench4q.agent.storage.Buffer;
|
||||
import org.bench4q.agent.storage.DoubleBufferStorage;
|
||||
import org.junit.After;
|
||||
|
@ -59,7 +58,7 @@ public class TestDoubleBufferStorage {
|
|||
}
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
String realSavePathString = this.getDoubleBufferStorage()
|
||||
.regeneratePath(SAVE_PATH, i);
|
||||
.calculateSavePath(SAVE_PATH, i);
|
||||
File output = new File(realSavePathString);
|
||||
if (output.exists()) {
|
||||
output.delete();
|
||||
|
@ -86,7 +85,7 @@ public class TestDoubleBufferStorage {
|
|||
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
public void testWriteWithJustLargerThanOneBufferContent() throws Exception {
|
||||
String realSavePathString = this.getDoubleBufferStorage()
|
||||
.regeneratePath(
|
||||
.calculateSavePath(
|
||||
SAVE_PATH,
|
||||
(Integer) this.invokePrivateMethod(
|
||||
"getCurrentBufferIndex", null, null));
|
||||
|
@ -94,7 +93,7 @@ public class TestDoubleBufferStorage {
|
|||
while (!this.getDoubleBufferStorage().forTest.isSuccess()) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
String readContent = this.getDoubleBufferStorage().readFile(
|
||||
String readContent = this.getDoubleBufferStorage().readFiles(
|
||||
realSavePathString);
|
||||
assertTrue(readContent.length() <= 48 * 1024);
|
||||
assertTrue(readContent.length() >= 46 * 1024);
|
||||
|
@ -108,7 +107,7 @@ public class TestDoubleBufferStorage {
|
|||
}
|
||||
|
||||
private void doWriteFile(int size) {
|
||||
String saveContent = this.getDoubleBufferStorage().readFile(
|
||||
String saveContent = this.getDoubleBufferStorage().readFiles(
|
||||
INPUT_FILE_PATH);
|
||||
for (int i = 0; i < size; i++) {
|
||||
this.getDoubleBufferStorage().writeFile(saveContent, SAVE_PATH);
|
||||
|
@ -140,12 +139,12 @@ public class TestDoubleBufferStorage {
|
|||
private int getResultLength() {
|
||||
int resultLength = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
String path = this.getDoubleBufferStorage().regeneratePath(
|
||||
String path = this.getDoubleBufferStorage().calculateSavePath(
|
||||
SAVE_PATH, i);
|
||||
if (!new File(path).exists()) {
|
||||
continue;
|
||||
}
|
||||
String readContentString = this.getDoubleBufferStorage().readFile(
|
||||
String readContentString = this.getDoubleBufferStorage().readFiles(
|
||||
path);
|
||||
resultLength += readContentString.length();
|
||||
}
|
||||
|
@ -169,11 +168,18 @@ public class TestDoubleBufferStorage {
|
|||
this.invokePrivateMethod("getCurrentBufferIndex", null, null));
|
||||
}
|
||||
|
||||
public Object invokePrivateMethod(String methodName,
|
||||
private Object invokePrivateMethod(String methodName,
|
||||
Class<?>[] paramClasses, Object[] params) throws Exception {
|
||||
Class<?> class1 = this.getDoubleBufferStorage().getClass();
|
||||
Method method = class1.getDeclaredMethod(methodName, paramClasses);
|
||||
method.setAccessible(true);
|
||||
return method.invoke(this.getDoubleBufferStorage(), params);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFiles() {
|
||||
String content = this.getDoubleBufferStorage().readFiles(
|
||||
INPUT_FILE_PATH);
|
||||
assertEquals(content.length(), 521);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue