View Javadoc

1   package org.apache.maven.continuum.web.action;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
23  
24  import java.io.BufferedOutputStream;
25  import java.io.FileOutputStream;
26  import java.io.FilterOutputStream;
27  import java.io.IOException;
28  import java.io.OutputStream;
29  import java.io.PrintStream;
30  
31  /**
32   * TestContinuumActionLogging:
33   *
34   * @author jesse
35   * @version $Id: ContinuumActionLoggingTest.java 1372260 2012-08-13 04:29:09Z brett $
36   */
37  public class ContinuumActionLoggingTest
38      extends PlexusInSpringTestCase
39  {
40  
41      StringBuffer testOutput = new StringBuffer();
42  
43  
44      public void setUp()
45          throws Exception
46      {
47          super.setUp();
48  
49          PrintStream systemPrintStream = new PrintStream( new FilteredStream( System.out ), true );
50          System.setOut( systemPrintStream );
51      }
52  
53  
54      public void tearDown()
55      {
56          System.setOut( new PrintStream( new BufferedOutputStream( new FileOutputStream( java.io.FileDescriptor.out ),
57                                                                    128 ), true ) );
58      }
59  
60  
61      public void testActionLogging()
62          throws Exception
63      {
64          TestAction testAction = (TestAction) lookup( "com.opensymphony.xwork2.Action", "testAction" );
65          String testString = "action test string";
66          testAction.setTestString( testString );
67  
68          testAction.execute();
69  
70          assertTrue( testOutput.toString().indexOf( testString ) != -1 );
71      }
72  
73  
74      class FilteredStream
75          extends FilterOutputStream
76      {
77          OutputStream stream;
78  
79          public FilteredStream( OutputStream stream )
80          {
81              super( stream );
82              this.stream = stream;
83          }
84  
85          public void write( byte byteArray[] )
86              throws IOException
87          {
88              testOutput.append( new String( byteArray ) );
89              stream.write( byteArray );
90          }
91  
92          public void write( byte byteArray[], int offset, int length )
93              throws IOException
94          {
95              testOutput.append( new String( byteArray, offset, length ) );
96              stream.write( byteArray, offset, length );
97          }
98      }
99  }