View Javadoc
1   package org.apache.maven.surefire.booter;
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 static org.apache.maven.surefire.util.internal.StringUtils.requireNonNull;
23  import static org.apache.maven.surefire.util.internal.StringUtils.isBlank;
24  import static org.apache.maven.surefire.booter.MasterProcessCommand.RUN_CLASS;
25  import static org.apache.maven.surefire.booter.MasterProcessCommand.SHUTDOWN;
26  import static org.apache.maven.surefire.booter.Shutdown.DEFAULT;
27  
28  /**
29   * Encapsulates data and command sent from master to forked process.
30   *
31   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
32   * @since 2.19
33   */
34  public final class Command
35  {
36      public static final Command TEST_SET_FINISHED = new Command( MasterProcessCommand.TEST_SET_FINISHED );
37      public static final Command SKIP_SINCE_NEXT_TEST = new Command( MasterProcessCommand.SKIP_SINCE_NEXT_TEST );
38      public static final Command NOOP = new Command( MasterProcessCommand.NOOP );
39  
40      private final MasterProcessCommand command;
41      private final String data;
42  
43      public Command( MasterProcessCommand command, String data )
44      {
45          this.command = requireNonNull( command );
46          this.data = data;
47      }
48  
49      public static Command toShutdown( Shutdown shutdownType )
50      {
51          return new Command( SHUTDOWN, shutdownType.name() );
52      }
53  
54      public static Command toRunClass( String runClass )
55      {
56          return new Command( RUN_CLASS, runClass );
57      }
58  
59      public Command( MasterProcessCommand command )
60      {
61          this( command, null );
62      }
63  
64      public MasterProcessCommand getCommandType()
65      {
66          return command;
67      }
68  
69      public String getData()
70      {
71          return data;
72      }
73  
74      /**
75       * @return {@link Shutdown} or {@link Shutdown#DEFAULT} if {@link #getData()} is null or blank string
76       * @throws IllegalArgumentException if string data {@link #getData()} is not applicable to enum {@link Shutdown}
77       */
78      public Shutdown toShutdownData()
79      {
80          if ( !isType( SHUTDOWN ) )
81          {
82              throw new IllegalStateException( "expected MasterProcessCommand.SHUTDOWN" );
83          }
84          return isBlank( data ) ? DEFAULT : Shutdown.valueOf( data );
85      }
86  
87      public boolean isType( MasterProcessCommand command )
88      {
89          return command == this.command;
90      }
91  
92      @Override
93      public boolean equals( Object o )
94      {
95          if ( this == o )
96          {
97              return true;
98          }
99  
100         if ( o == null || getClass() != o.getClass() )
101         {
102             return false;
103         }
104 
105         Command arg = (Command) o;
106 
107         return command == arg.command && ( data == null ? arg.data == null : data.equals( arg.data ) );
108     }
109 
110     @Override
111     public int hashCode()
112     {
113         int result = command.hashCode();
114         result = 31 * result + ( data != null ? data.hashCode() : 0 );
115         return result;
116     }
117 }