001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.log;
016    
017    import com.liferay.portal.log.CaptureAppender;
018    import com.liferay.portal.log.Log4JLoggerTestUtil;
019    
020    import java.lang.reflect.Method;
021    
022    import org.apache.log4j.Level;
023    import org.apache.log4j.spi.LoggingEvent;
024    
025    import org.junit.Assert;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ExpectedLogsUtil {
031    
032            public static void endAssert(
033                    Method method, CaptureAppender captureAppender) {
034    
035                    if (method == null) {
036                            return;
037                    }
038    
039                    ExpectedLogs expectedLogs = method.getAnnotation(ExpectedLogs.class);
040    
041                    if (expectedLogs == null) {
042                            return;
043                    }
044    
045                    try {
046                            for (LoggingEvent loggingEvent :
047                                            captureAppender.getLoggingEvents()) {
048    
049                                    String renderedMessage = loggingEvent.getRenderedMessage();
050    
051                                    if (!_isExpected(expectedLogs, renderedMessage)) {
052                                            Assert.fail(renderedMessage);
053                                    }
054                            }
055                    }
056                    finally {
057                            captureAppender.close();
058                    }
059            }
060    
061            public static CaptureAppender startAssert(Method method) {
062                    if (method == null) {
063                            return null;
064                    }
065    
066                    ExpectedLogs expectedLogs = method.getAnnotation(ExpectedLogs.class);
067    
068                    if (expectedLogs == null) {
069                            return null;
070                    }
071    
072                    Class<?> clazz = expectedLogs.loggerClass();
073    
074                    return Log4JLoggerTestUtil.configureLog4JLogger(
075                            clazz.getName(), Level.toLevel(expectedLogs.level()));
076            }
077    
078            private static boolean _isExpected(
079                    ExpectedLogs expectedLogs, String renderedMessage) {
080    
081                    for (ExpectedLog expectedLog : expectedLogs.expectedLogs()) {
082                            ExpectedType expectedType = expectedLog.expectedType();
083    
084                            if (expectedType == ExpectedType.EXACT) {
085                                    if (renderedMessage.equals(expectedLog.expectedLog())) {
086                                            return true;
087                                    }
088                            }
089                            else if (expectedType == ExpectedType.POSTFIX) {
090                                    if (renderedMessage.endsWith(expectedLog.expectedLog())) {
091                                            return true;
092                                    }
093                            }
094                            else if (expectedType == ExpectedType.PREFIX) {
095                                    if (renderedMessage.startsWith(expectedLog.expectedLog())) {
096                                            return true;
097                                    }
098                            }
099                    }
100    
101                    return false;
102            }
103    
104    }