⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10556133278d001c1487de563adc4702

📁 一个类似于log4j的小软件
💻
字号:
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, "implchild trace");
		child.log(Level.DEBUG, "implchild debug");
		child.log(Level.INFO, "implchild info");
		child.log(Level.WARNING, "implchild warning");
		child.log(Level.ERROR, "implchild error");
		child.log(Level.FATAL, "implchild fatal");

		grandpa.log(Level.TRACE, "implnochild trace");
		grandpa.log(Level.DEBUG, "implnochild debug");
		grandpa.log(Level.INFO, "implnochild info");
		grandpa.log(Level.WARNING, "implnochild warning");
		grandpa.log(Level.ERROR, "implnochild error");
		grandpa.log(Level.FATAL, "implnochild 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("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(childfile);
		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);
	}
	
	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 + -