📄 dptestcase.java
字号:
package cn.edu.nju.software.sd.cll;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import junit.framework.TestCase;
import cn.edu.nju.software.sd.cll.Level;
import cn.edu.nju.software.sd.cll.Logger;
public class DPTestCase extends TestCase {
private Logger parentLogger;
private Logger implChildLogger;
private Logger noimplChildLogger;
private String parentFileName = "C:/cll/parent.txt";
private String implChildFileName = "C:/cll/implChild.txt";
// parentLogger: name-- cn.edu.nju.swi.test
// level--trace format-- %n-%e-%f-%s-%t-%m destination-- C:/CLL/parent.txt
// implChildLogger: name-- cn.edu.nju.swi.test.impl
// level--fatal format-- %n-%e-%f-%m
// destination-- C:/CLL/implChild.txt, Console
protected void setUp() throws Exception {
super.setUp();
setupFile();
parentLogger = Logger.getLogger("cn.edu.nju.swi.test");
implChildLogger = Logger.getLogger("cn.edu.nju.swi.test.impl");
noimplChildLogger = Logger.getLogger("cn.edu.nju.swi.test.noimpl");
parentLogger.log(Level.TRACE, "parent trace");
parentLogger.log(Level.DEBUG, "parent debug");
parentLogger.log(Level.INFO, "parent info");
parentLogger.log(Level.WARNING, "parent warning");
parentLogger.log(Level.ERROR, "parent error");
parentLogger.log(Level.FATAL, "parent fatal");
implChildLogger.log(Level.TRACE, "implchild trace");
implChildLogger.log(Level.DEBUG, "implchild debug");
implChildLogger.log(Level.INFO, "implchild info");
implChildLogger.log(Level.WARNING, "implchild warning");
implChildLogger.log(Level.ERROR, "implchild error");
implChildLogger.log(Level.FATAL, "implchild fatal");
noimplChildLogger.log(Level.TRACE, "implnochild trace");
noimplChildLogger.log(Level.DEBUG, "implnochild debug");
noimplChildLogger.log(Level.INFO, "implnochild info");
noimplChildLogger.log(Level.WARNING, "implnochild warning");
noimplChildLogger.log(Level.ERROR, "implnochild error");
noimplChildLogger.log(Level.FATAL, "implnochild fatal");
}
public void testLevel() {
String parentContent = readFile(parentFileName);
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("implnochild trace") != -1
&& parentContent.indexOf("implnochild debug") != -1
&& parentContent.indexOf("implnochild info") != -1
&& parentContent.indexOf("implnochild warning") != -1
&& parentContent.indexOf("implnochild error") != -1
&& parentContent.indexOf("implnochild fatal") != -1);
String implChildContent = readFile(implChildFileName);
assertTrue(implChildContent != null
&& implChildContent.indexOf("implchild trace") == -1
&& implChildContent.indexOf("implchild debug") == -1
&& implChildContent.indexOf("implchild info") == -1
&& implChildContent.indexOf("implchild warning") == -1
&& implChildContent.indexOf("implchild error") == -1
&& implChildContent.indexOf("implchild fatal") != -1);
}
// %n-%e-%f-%m
public void testFormat() {
String implChildContent = readFile(implChildFileName);
if (implChildContent != null) {
assertTrue(implChildContent
.toLowerCase()
.indexOf(
"cn.edu.nju.swi.test.impl-fatal-setup-implchild fatal") != -1);
}
}
public void testDestination() {
String parentContent = readFile(parentFileName);
String implChildContent = readFile(implChildFileName);
assertTrue(parentContent.indexOf("cn.edu.nju.swi.test-") != -1);
assertTrue(parentContent.indexOf("cn.edu.nju.swi.test.noimpl-") != -1);
assertNotNull(implChildContent);
// 手工检测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(parentFileName);
parentWriter.append("");
parentWriter.flush();
FileWriter childWriter = new FileWriter(implChildFileName);
childWriter.append("");
childWriter.flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -