1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.trace;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  import junit.framework.AssertionFailedError;
44  import junit.framework.TestCase;
45  
46  /***
47   * Tests for Trace.
48   *
49   * @version $Revision: 1.6 $
50   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51   */
52  public class TraceTest extends TestCase {
53  
54      /***
55       *
56       */
57      private class LogEntry {
58          private final Thread thread_;
59          private final TraceItem traceItem_;
60          /*** @param item The trace item. */
61          public LogEntry( final TraceItem item ) {
62              try {
63                  traceItem_ = (TraceItem)item.clone();
64              }
65              catch( final CloneNotSupportedException e ) {
66                  throw new AssertionFailedError("TraceItem not cloneable");
67              }
68              thread_ = Thread.currentThread();
69          }
70      }
71  
72      /***
73       *
74       */
75      private class LogTraceWriter implements TraceWriter {
76          private final List log_;
77          /*** @param log The list that will collect the log items */
78          public LogTraceWriter( final List log ) {
79              log_ = log;
80          }
81          /*** @param item The item to be written */
82          public void write( final TraceItem item ) {
83              log_.add( new LogEntry(item) );
84          }
85      }
86  
87      /***
88       * Create a test.
89       * @param name The name of the test.
90       */
91      public TraceTest( final String name ) {
92          super(name);
93      }
94  
95      /***
96       * Test that we are logging on a seperate thread when buffering is
97       * enabled.
98       */
99      public void testSecondThreadWhenBuffered() {
100         final List log = Collections.synchronizedList(new ArrayList());
101         final LogTraceWriter logWriter = new LogTraceWriter(log);
102         final TraceChannel channel = new TraceChannel("test");
103         channel.addTraceWriter(logWriter);
104 
105         Trace.getController().setBufferingEnabled(true);
106         Trace.print(channel, "foo");
107         Trace.getController().setBufferingEnabled(false);
108         Trace.print(channel, "bar");
109         Trace.getController().setBufferingEnabled(true);
110         Trace.print(channel, "test");
111         Trace.flush();
112 
113         final Thread currentThread = Thread.currentThread();
114 
115         assertEquals( "Number of items", 3, log.size() );
116         LogEntry logEntry;
117 
118         logEntry = (LogEntry)log.get(0);
119         assertEquals( "First item contents", "foo", logEntry.traceItem_.getMessage() );
120         assertTrue( "First item thread", currentThread != logEntry.thread_ );
121 
122         logEntry = (LogEntry)log.get(1);
123         assertEquals( "Second item contents", "bar", logEntry.traceItem_.getMessage() );
124         assertTrue( "Second item thread", currentThread == logEntry.thread_ );
125 
126         logEntry = (LogEntry)log.get(2);
127         assertEquals( "Third item contents", "test", logEntry.traceItem_.getMessage() );
128         assertTrue( "Third item thread", currentThread != logEntry.thread_ );
129     }
130 }