📄 90f3d594288d001c1487de563adc4702
字号:
package cn.edu.nju.software.sd.cll.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import junit.framework.TestCase;
import main.cn.edu.nju.software.sd.cll.Level;
import main.cn.edu.nju.software.sd.cll.Logger;
public class MyTest extends TestCase {
private Logger parent;
private Logger child;
private Logger grandpa;
private String parentfile = "D://CLL/my/parent.txt";
private String childfile = "D://CLL/my/child.txt";
private String grandpafile = "D://CLL/my/grand.txt";
protected void setUp() throws Exception
{
super.setUp();
parent = Logger.getLogger("cn.nju");
child = Logger.getLogger("cn.nju.edu");
grandpa = Logger.getLogger("cn");
parent.log(Level.TRACE, "parent trace");
parent.log(Level.DEBUG, "parent debug");
parent.log(Level.INFO, "parent info");
parent.log(Level.WARNING, "parent warning");
parent.log(Level.ERROR, "parent error");
parent.log(Level.FATAL, "parent fatal");
parent.setLevel(Level.ERROR);
child.log(Level.TRACE, "child trace");
child.log(Level.DEBUG, "child debug");
child.log(Level.INFO, "child info");
child.log(Level.WARNING, "child warning");
child.log(Level.ERROR, "child error");
child.log(Level.FATAL, "child fatal");
grandpa.log(Level.TRACE, "grandpa trace");
grandpa.log(Level.DEBUG, "grandpa debug");
grandpa.log(Level.INFO, "grandpa info");
grandpa.log(Level.WARNING, "grandpa warning");
grandpa.log(Level.ERROR, "grandpa error");
grandpa.log(Level.FATAL, "grandpa fatal");
}
public void testLevel() {
String parentContent = readFile(parentfile);
assertTrue(parentContent != null
&& parentContent.indexOf("parent trace") != -1
&& parentContent.indexOf("parent debug") != -1
&& parentContent.indexOf("parent info") != -1
&& parentContent.indexOf("parent warning") != -1
&& parentContent.indexOf("parent error") != -1
&& parentContent.indexOf("parent fatal") != -1);
assertTrue(parentContent != null
&& parentContent.indexOf("child trace") != -1
&& parentContent.indexOf("child debug") != -1
&& parentContent.indexOf("child info") != -1
&& parentContent.indexOf("child warning") != -1
&& parentContent.indexOf("child error") != -1
&& parentContent.indexOf("child fatal") != -1);
String childContent = readFile(childfile);
assertTrue(childContent != null
&& childContent.indexOf("child trace") == -1
&& childContent.indexOf("child debug") == -1
&& childContent.indexOf("child info") == -1
&& childContent.indexOf("child warning") == -1
&& childContent.indexOf("child error") == -1
&& childContent.indexOf("child fatal") != -1);
}
public void testFormat() {
String childContent = readFile(childfile);
if (childContent != null) {
assertTrue(childContent
.toLowerCase()
.indexOf(
"cn.edu.nju.swi.test.impl-fatal-setup-implchild fatal") != -1);
}
}
public void testDestination() {
String parentContent = readFile(parentfile);
String childContent = readFile(childfile);
assertTrue(parentContent.indexOf("cn.edu.nju.swi.test-") != -1);
assertTrue(parentContent.indexOf("cn.edu.nju.swi.test.noimpl-") != -1);
assertNotNull(childContent);
// 手工检测implChild有控制台输出, 在每个test的junit report中可以看到
}
private String readFile(String filename) {
try {
FileReader reader = new FileReader(filename);
char[] cbuf = new char[100];
StringBuffer result = new StringBuffer();
while (reader.read(cbuf) != -1) {
result.append(cbuf);
}
return new String(result);
} catch (FileNotFoundException e) {
assertEquals("The file exists.", "The file" + filename
+ "doesn't exist.");
} catch (IOException e) {
assertEquals("No exception.", "I/O Read file" + filename
+ " exception.");
}
return null;
}
public void setupFile() throws IOException {
FileWriter parentWriter = new FileWriter(parentfile);
parentWriter.append("");
parentWriter.flush();
FileWriter childWriter = new FileWriter(childfile);
childWriter.append("");
childWriter.flush();
FileWriter grandWriter = new FileWriter(grandpafile);
grandWriter.append("");
grandWriter.flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -