alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (TimerInterceptorTest.java)

This example Struts source code file (TimerInterceptorTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

exception, exception, executed, executed, mockactioninvocation, mockactionproxy, mytimerinterceptor, mytimerinterceptor, override, should, simplefooaction, string, timerinterceptor, timerinterceptor

The Struts TimerInterceptorTest.java source code

/*
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.SimpleFooAction;
import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.util.logging.Logger;

/**
 * Unit test for {@link TimerInterceptor}.
 *
 * @author Claus Ibsen
 */
public class TimerInterceptorTest extends XWorkTestCase {

    private MyTimerInterceptor interceptor;
    private MockActionInvocation mai;
    private MockActionProxy ap;


    public void testTimerInterceptor() throws Exception {
        TimerInterceptor real = new TimerInterceptor();
        real.init();
        real.intercept(mai);
        real.destroy();
    }

    public void testInvalidLogLevel() throws Exception {
        TimerInterceptor real = new TimerInterceptor();
        real.setLogLevel("xxxx");
        real.init();
        try {
            real.intercept(mai);
            fail("Should not have reached this point.");
        } catch (IllegalArgumentException e) {
        	// success
        }
    }

    public void testDefault() throws Exception {
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testNoNamespace() throws Exception {
        ap.setNamespace(null);
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testInputMethod() throws Exception {
        ap.setMethod("input");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!input] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testTraceLevel() throws Exception {
        interceptor.setLogLevel("trace");
        interceptor.intercept(mai);
        assertNull(interceptor.message); // no default logging at trace level
        assertEquals("trace", interceptor.getLogLevel());
    }

    public void testDebugLevel() throws Exception {
        interceptor.setLogLevel("debug");
        interceptor.intercept(mai);
        assertNull(interceptor.message); // no default logging at debug level
    }

    public void testInfoLevel() throws Exception {
        interceptor.setLogLevel("info");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testWarnLevel() throws Exception {
        interceptor.setLogLevel("warn");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testErrorLevel() throws Exception {
        interceptor.setLogLevel("error");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testFatalLevel() throws Exception {
        interceptor.setLogLevel("fatal");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testLogCategory() throws Exception {
        interceptor.setLogCategory("com.mycompany.myapp.actiontiming");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertNotSame(interceptor.logger, TimerInterceptor.LOG);
    }

    public void testLogCategoryLevel() throws Exception {
        interceptor.setLogCategory("com.mycompany.myapp.actiontiming");
        interceptor.setLogLevel("error");
        interceptor.intercept(mai);
        assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
        assertNotSame(interceptor.logger, TimerInterceptor.LOG);
        assertEquals("com.mycompany.myapp.actiontiming", interceptor.getLogCategory());
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        interceptor = new MyTimerInterceptor();
        interceptor.init();

        mai = new MockActionInvocation();
        ap = new MockActionProxy();
        ap.setActionName("myAction");
        ap.setNamespace("myApp");
        ap.setMethod("execute");
        mai.setAction(new SimpleFooAction());
        mai.setProxy(ap);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        interceptor.destroy();
        ap = null;
        mai = null;
    }

    private class MyTimerInterceptor extends TimerInterceptor {

        private Logger logger;
        private String message;

        @Override
        protected void doLog(Logger logger, String message) {
            super.doLog(logger, message);

            this.logger = logger;
            this.message = message;
        }
    }

}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts TimerInterceptorTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.