View Javadoc
1   package org.apache.maven.plugin.surefire;
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.apache.maven.plugin.surefire.log.api.ConsoleLogger;
23  import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
24  import org.apache.maven.plugin.surefire.log.api.PrintStreamLogger;
25  import org.apache.maven.surefire.booter.IsolatedClassLoader;
26  import org.apache.maven.surefire.booter.SurefireReflector;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static org.apache.maven.surefire.util.ReflectionUtils.getMethod;
31  import static org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray;
32  import static org.hamcrest.CoreMatchers.instanceOf;
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.hamcrest.CoreMatchers.is;
35  import static org.hamcrest.CoreMatchers.not;
36  import static org.hamcrest.CoreMatchers.notNullValue;
37  import static org.hamcrest.CoreMatchers.sameInstance;
38  import static org.mockito.Mockito.spy;
39  import static org.mockito.Mockito.times;
40  import static org.mockito.Mockito.verify;
41  
42  /**
43   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
44   * @see ConsoleLogger
45   * @see SurefireReflector
46   * @since 2.20
47   */
48  public class SurefireReflectorTest
49  {
50      private ConsoleLogger logger;
51      private SurefireReflector reflector;
52  
53      @Before
54      public void prepareData()
55      {
56          logger = spy( new PrintStreamLogger( System.out ) );
57          ClassLoader cl = new IsolatedClassLoader( Thread.currentThread().getContextClassLoader(), false, "role" );
58          reflector = new SurefireReflector( cl );
59      }
60  
61      @Test
62      public void shouldProxyConsoleLogger()
63      {
64          Object mirror = reflector.createConsoleLogger( logger );
65          assertThat( mirror, is( notNullValue() ) );
66          assertThat( mirror.getClass().getInterfaces()[0].getName(), is( ConsoleLogger.class.getName() ) );
67          assertThat( mirror, is( not( sameInstance( (Object) logger ) ) ) );
68          assertThat( mirror, is( instanceOf( ConsoleLoggerDecorator.class ) ) );
69          invokeMethodWithArray( mirror, getMethod( mirror, "info", String.class ), "Hi There!" );
70          verify( logger, times( 1 ) ).info( "Hi There!" );
71      }
72  }