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

📄 e06fee382c8d001c1487de563adc4702

📁 一个类似于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;
import main.cn.edu.nju.software.sd.cll.Format;
import main.cn.edu.nju.software.sd.cll.FileHandler;

public class MyTest extends TestCase {
	private Logger parent;
	private Logger child;
	private Logger grandpa;
	private Logger other;
	
	private String grandpafile = "D://CLL/my/grand.txt";
	private String otherfile = "D://CLL/my/other.txt";
	
	protected void setUp() throws Exception
	{
		super.setUp();
		
		parent = Logger.getLogger("cn.edu");
		child = Logger.getLogger("cn.edu.nju");
		grandpa = Logger.getLogger("cn");
		other = Logger.getLogger("other");
		
		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");

		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");
		
		other.log(Level.TRACE, "other trace");
		other.log(Level.DEBUG, "other debug");
		other.log(Level.INFO, "other info");
		other.log(Level.WARNING, "other warning");
		other.log(Level.ERROR, "other error");
		other.log(Level.FATAL, "other fatal");
		
		other.setLevel(Level.ERROR);
		other.setFormat(new Format("%m-%n-%e-%f"));
		other.addHandler(new FileHandler(otherfile));
		
		other.log(Level.TRACE, "other trace");
		other.log(Level.DEBUG, "other debug");
		other.log(Level.INFO, "other info");
		other.log(Level.WARNING, "other warning");
		other.log(Level.ERROR, "other error");
		other.log(Level.FATAL, "other fatal");
	}
	
	public void testLevel() {
		String grandContent = readFile(grandpafile);
		assertTrue(grandContent != null
				&& grandContent.indexOf("parent trace") != -1
				&& grandContent.indexOf("parent debug") != -1
				&& grandContent.indexOf("parent info") != -1
				&& grandContent.indexOf("parent warning") != -1
				&& grandContent.indexOf("parent error") != -1
				&& grandContent.indexOf("parent fatal") != -1);
		assertTrue(grandContent != null
				&& grandContent.indexOf("child trace") != -1
				&& grandContent.indexOf("child debug") != -1
				&& grandContent.indexOf("child info") != -1
				&& grandContent.indexOf("child warning") != -1
				&& grandContent.indexOf("child error") != -1
				&& grandContent.indexOf("child fatal") != -1);
		assertTrue(grandContent != null
				&& grandContent.indexOf("grandpa trace") != -1
				&& grandContent.indexOf("grandpa debug") != -1
				&& grandContent.indexOf("grandpa info") != -1
				&& grandContent.indexOf("grandpa warning") != -1
				&& grandContent.indexOf("grandpa error") != -1
				&& grandContent.indexOf("grandpa fatal") != -1);

		String otherContent = readFile(otherfile);
		assertTrue(otherContent != null
				&& otherContent.indexOf("other trace") == -1
				&& otherContent.indexOf("other debug") == -1
				&& otherContent.indexOf("other info") == -1
				&& otherContent.indexOf("other warning") == -1
				&& otherContent.indexOf("other error") == -1
				&& otherContent.indexOf("other fatal") != -1);
	}
	
	public void testFormat() {
		String otherContent = readFile(otherfile);
		if (otherContent != null) {
			assertTrue(otherContent
					.toLowerCase()
					.indexOf(
							"cn.edu.nju-fatal-setup-child fatal") != -1);
		}
	}
	
	public void testDestination() {
		String parentContent = readFile(parentfile);
		String childContent = readFile(childfile);

		assertTrue(parentContent.indexOf("cn.edu-") != -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 + -