1 7 package com.tirsen.nanning.samples; 8 9 import com.tirsen.nanning.config.AspectSystem; 10 import com.tirsen.nanning.config.InterceptorAspect; 11 import com.tirsen.nanning.config.Introductor; 12 import junit.framework.TestCase; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.apache.commons.logging.impl.SimpleLog; 19 import org.apache.commons.logging.impl.NoOpLog; 20 import org.apache.commons.logging.Log; 21 22 30 public class TraceInterceptorTest extends TestCase { 31 public static class MockLogger implements Log { 32 private List expectMessages = new ArrayList (); 33 private List actualMessages = new ArrayList (); 34 35 public void error(Object message, Throwable throwable) { 36 actualMessages.add("ERROR " + message); 37 } 38 39 public void debug(Object message) { 40 actualMessages.add(String.valueOf(message)); 41 } 42 43 public void expectAddMessage(String messageToExpect) { 44 expectMessages.add(messageToExpect); 45 } 46 47 public void verify() { 48 Iterator actualIterator = actualMessages.iterator(); 49 Iterator expectIterator = expectMessages.iterator(); 50 while (expectIterator.hasNext()) { 51 String expectedMessage = (String ) expectIterator.next(); 52 assertTrue("log output not as expected", actualIterator.hasNext()); 53 String actualMessage = (String ) actualIterator.next(); 54 assertTrue("log output does not match: " + expectedMessage, actualMessage.startsWith(expectedMessage)); 55 } 56 } 57 58 public void reset() { 59 actualMessages.clear(); 60 expectMessages.clear(); 61 } 62 63 public boolean isDebugEnabled() { 65 return true; 66 } 67 68 public boolean isErrorEnabled() { 69 return false; 70 } 71 72 public boolean isFatalEnabled() { 73 return false; 74 } 75 76 public boolean isInfoEnabled() { 77 return false; 78 } 79 80 public boolean isTraceEnabled() { 81 return false; 82 } 83 84 public boolean isWarnEnabled() { 85 return false; 86 } 87 88 public void trace(Object o) { 89 } 90 91 public void trace(Object o, Throwable throwable) { 92 } 93 94 public void debug(Object o, Throwable throwable) { 95 } 96 97 public void info(Object o) { 98 } 99 100 public void info(Object o, Throwable throwable) { 101 } 102 103 public void warn(Object o) { 104 } 105 106 public void warn(Object o, Throwable throwable) { 107 } 108 109 public void error(Object o) { 110 } 111 112 public void fatal(Object o) { 113 } 114 115 public void fatal(Object o, Throwable throwable) { 116 } 117 } 119 120 public static interface Intf { 121 String call(String arg, String arg2); 122 } 123 124 public static class Impl implements Intf { 125 public String call(String arg, String arg2) { 126 return "hej tillbax!"; 127 } 128 } 129 130 public static class ErrorImpl implements Intf { 131 public String call(String arg, String arg2) { 132 throw new RuntimeException ("ERROR"); 133 } 134 } 135 136 public void testLogInterceptor() throws InstantiationException , IllegalAccessException { 137 AspectSystem system = new AspectSystem(); 138 MockLogger mockLogger = new MockLogger(); 139 system.addAspect(new InterceptorAspect(new TraceInterceptor(mockLogger))); 140 system.addAspect(new Introductor(Intf.class, Impl.class)); 141 142 mockLogger.expectAddMessage(">>> call(hej, svej)"); 143 mockLogger.expectAddMessage("<<< call(hej, svej), took"); 144 Intf intf = (Intf) system.newInstance(Intf.class); 145 intf.call("hej", "svej"); 146 mockLogger.verify(); 147 } 148 } 149 | Popular Tags |