Source Code Cross Referenced for StandardTests.java in  » Library » apache-common-Logging » org » apache » commons » logging » log4j » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common Logging » org.apache.commons.logging.log4j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.logging.log4j;
018:
019:        import java.io.ByteArrayInputStream;
020:        import java.io.ByteArrayOutputStream;
021:        import java.io.ObjectInputStream;
022:        import java.io.ObjectOutputStream;
023:        import java.util.ArrayList;
024:        import java.util.List;
025:
026:        import junit.framework.TestCase;
027:
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:
031:        /**
032:         * Abstract set of tests that can be executed with various classpaths set.
033:         * <p>
034:         * The tests verify that when running on a system with Log4J present,
035:         * Log4J is selected and that the logger basically works.
036:         */
037:
038:        public abstract class StandardTests extends TestCase {
039:
040:            /**
041:             * Simple structure to store information about messages that actually get
042:             * logged by the underlying logging library.
043:             */
044:            public static class LogEvent {
045:                public String msg;
046:                public String level;
047:                public Throwable throwable;
048:            }
049:
050:            // ------------------------------------------------------------------- 
051:            // JUnit Infrastructure Methods
052:            // ------------------------------------------------------------------- 
053:
054:            /**
055:             * Set up instance variables required by this test case.
056:             */
057:            public void setUp() throws Exception {
058:                LogFactory.releaseAll();
059:            }
060:
061:            /**
062:             * Tear down instance variables required by this test case.
063:             */
064:            public void tearDown() {
065:                LogFactory.releaseAll();
066:            }
067:
068:            // ----------------------------------------------------------- 
069:            // abstract methods
070:            // ----------------------------------------------------------- 
071:
072:            /**
073:             * Modify log4j's setup so that all messages actually logged get redirected
074:             * into the specified list.
075:             * <p>
076:             * This method also sets the logging level to INFO so that we
077:             * can test whether messages are getting properly filtered.
078:             */
079:            public abstract void setUpTestAppender(List logEvents)
080:                    throws Exception;
081:
082:            // ----------------------------------------------------------- Test Methods
083:
084:            /**
085:             * Test that a LogFactory gets created as expected.
086:             */
087:            public void testCreateFactory() {
088:                LogFactory factory = LogFactory.getFactory();
089:                assertNotNull("LogFactory exists", factory);
090:                assertEquals("LogFactory class",
091:                        "org.apache.commons.logging.impl.LogFactoryImpl",
092:                        factory.getClass().getName());
093:
094:                String names[] = factory.getAttributeNames();
095:                assertNotNull("Names exists", names);
096:                assertEquals("Names empty", 0, names.length);
097:            }
098:
099:            /**
100:             * Verify that we can log messages without exceptions.
101:             */
102:            public void testPlainMessages() throws Exception {
103:                List logEvents = new ArrayList();
104:                setUpTestAppender(logEvents);
105:                Log log = LogFactory.getLog("test-category");
106:                logPlainMessages(log);
107:                checkLoggingEvents(logEvents, false);
108:            }
109:
110:            /**
111:             * Verify that we can log exception messages.
112:             */
113:            public void testExceptionMessages() throws Exception {
114:                List logEvents = new ArrayList();
115:                setUpTestAppender(logEvents);
116:                Log log = LogFactory.getLog("test-category");
117:                logExceptionMessages(log);
118:                checkLoggingEvents(logEvents, true);
119:            }
120:
121:            /**
122:             * Test Serializability of Log instance
123:             */
124:            public void testSerializable() throws Exception {
125:                List logEvents = new ArrayList();
126:                setUpTestAppender(logEvents);
127:                Log log = LogFactory.getLog("test-category");
128:
129:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
130:                ObjectOutputStream oos = new ObjectOutputStream(baos);
131:                oos.writeObject(log);
132:                oos.close();
133:                ByteArrayInputStream bais = new ByteArrayInputStream(baos
134:                        .toByteArray());
135:                ObjectInputStream ois = new ObjectInputStream(bais);
136:                Log newLog = (Log) ois.readObject();
137:                ois.close();
138:
139:                // Check the characteristics of the resulting object
140:                logExceptionMessages(newLog);
141:                checkLoggingEvents(logEvents, true);
142:            }
143:
144:            // -------------------------------------------------------- Support Methods
145:
146:            /**
147:             * Verify that the TestAppender has received the expected
148:             * number of messages. This assumes that:
149:             * <ul>
150:             * <li>setUpTestAppender has been called
151:             * <li>logPlainMessages or logExceptionMessages has been
152:             * called to log a known number of messages at known levels.
153:             * </ul>
154:             * 
155:             * @param logEvents is the list of log events received.
156:             * 
157:             * @param thrown False if logPlainMessages was called
158:             * (ie the TestAppender is expected to have received
159:             * logevents with no associated exception info). True if
160:             * logExceptionMessages was called.
161:             */
162:            private void checkLoggingEvents(List logEvents, boolean thrown) {
163:                LogEvent ev;
164:
165:                assertEquals("Unexpected number of log events", 4, logEvents
166:                        .size());
167:
168:                ev = (LogEvent) logEvents.get(0);
169:                assertEquals("Info message expected", "info", ev.msg);
170:                assertEquals("Info level expected", "INFO", ev.level);
171:                assertEquals("Exception data incorrect",
172:                        (ev.throwable != null), thrown);
173:
174:                ev = (LogEvent) logEvents.get(1);
175:                assertEquals("Warn message expected", "warn", ev.msg);
176:                assertEquals("Warn level expected", "WARN", ev.level);
177:                assertEquals("Exception data incorrect",
178:                        (ev.throwable != null), thrown);
179:
180:                ev = (LogEvent) logEvents.get(2);
181:                assertEquals("Error message expected", "error", ev.msg);
182:                assertEquals("Error level expected", "ERROR", ev.level);
183:                assertEquals("Exception data incorrect",
184:                        (ev.throwable != null), thrown);
185:
186:                ev = (LogEvent) logEvents.get(3);
187:                assertEquals("Fatal message expected", "fatal", ev.msg);
188:                assertEquals("Fatal level expected", "FATAL", ev.level);
189:                assertEquals("Exception data incorrect",
190:                        (ev.throwable != null), thrown);
191:            }
192:
193:            /**
194:             * Log plain messages.
195:             */
196:            private void logPlainMessages(Log log) {
197:                log.trace("trace"); // Should not actually get logged
198:                log.debug("debug"); // Should not actually get logged
199:                log.info("info");
200:                log.warn("warn");
201:                log.error("error");
202:                log.fatal("fatal");
203:            }
204:
205:            /**
206:             * Log messages with exceptions
207:             */
208:            private void logExceptionMessages(Log log) {
209:                Throwable t = new IndexOutOfBoundsException();
210:                log.trace("trace", t); // Should not actually get logged
211:                log.debug("debug", t); // Should not actually get logged
212:                log.info("info", t);
213:                log.warn("warn", t);
214:                log.error("error", t);
215:                log.fatal("fatal", t);
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.