View Javadoc
1   package org.apache.maven.surefire.testng.conf;
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 java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  
28  import junit.framework.TestCase;
29  import org.testng.ReporterConfig;
30  
31  /**
32   * @author Kristian Rosenvold
33   */
34  public class TestNGMapConfiguratorTest
35      extends TestCase
36  {
37      public static final String FIRST_LISTENER = "org.testng.TestListenerAdapter";
38      public static final String SECOND_LISTENER = "org.testng.reporters.ExitCodeListener";
39      public static final String LISTENER_PROP = "listener";
40  
41      public void testGetConvertedOptions()
42          throws Exception
43      {
44          Map convertedOptions = getConvertedOptions( "mixed", "true" );
45          boolean bool = (Boolean) convertedOptions.get( "-mixed" );
46          assertTrue( bool );
47      }
48  
49      public void testListenersOnSeparateLines()
50          throws Exception
51      {
52          String listenersOnSeveralLines = String.format( "%s , %n %s",
53                  FIRST_LISTENER, SECOND_LISTENER);
54          Map convertedOptions = getConvertedOptions(LISTENER_PROP, listenersOnSeveralLines);
55          List listeners = (List) convertedOptions.get( String.format("-%s", LISTENER_PROP));
56          assertEquals(2, listeners.size());
57      }
58  
59      public void testListenersOnTheSameLine()
60          throws Exception
61      {
62          String listenersOnSeveralLines = String.format( "%s,%s",
63                  FIRST_LISTENER, SECOND_LISTENER);
64          Map convertedOptions = getConvertedOptions( LISTENER_PROP, listenersOnSeveralLines);
65          List listeners = (List) convertedOptions.get( String.format("-%s", LISTENER_PROP));
66          assertEquals(2, listeners.size());
67      }
68  
69      public void testGroupByInstances()
70          throws Exception
71      {
72          Map convertedOptions = getConvertedOptions( "group-by-instances", "true" );
73          boolean bool = (Boolean) convertedOptions.get( "-group-by-instances" );
74          assertTrue( bool );
75      }
76  
77      public void testReporter()
78          throws Exception
79      {
80          Map<String, Object> convertedOptions = getConvertedOptions( "reporter", "classname" );
81          List<ReporterConfig> reporter = (List) convertedOptions.get( "-reporterslist" );
82          ReporterConfig reporterConfig = reporter.get( 0 );
83          assertEquals( "classname", reporterConfig.getClassName() );
84      }
85  
86      private Map getConvertedOptions( String key, String value )
87          throws TestSetFailedException
88      {
89          TestNGMapConfigurator testNGMapConfigurator = new TestNGMapConfigurator();
90          Map<String, String> raw = new HashMap<>();
91          raw.put( key, value );
92          return testNGMapConfigurator.getConvertedOptions( raw );
93      }
94  }