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.ObjectUtils.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      public static final Command BYE_ACK = new Command( MasterProcessCommand.BYE_ACK );
40  
41      private final MasterProcessCommand command;
42      private final String data;
43  
44      public Command( MasterProcessCommand command, String data )
45      {
46          this.command = requireNonNull( command );
47          this.data = data;
48      }
49  
50      public static Command toShutdown( Shutdown shutdownType )
51      {
52          return new Command( SHUTDOWN, shutdownType.name() );
53      }
54  
55      public static Command toRunClass( String runClass )
56      {
57          return new Command( RUN_CLASS, runClass );
58      }
59  
60      public Command( MasterProcessCommand command )
61      {
62          this( command, null );
63      }
64  
65      public MasterProcessCommand getCommandType()
66      {
67          return command;
68      }
69  
70      public String getData()
71      {
72          return data;
73      }
74  
75      /**
76       * @return {@link Shutdown} or {@link Shutdown#DEFAULT} if {@link #getData()} is null or blank string
77       * @throws IllegalArgumentException if string data {@link #getData()} is not applicable to enum {@link Shutdown}
78       */
79      public Shutdown toShutdownData()
80      {
81          if ( !isType( SHUTDOWN ) )
82          {
83              throw new IllegalStateException( "expected MasterProcessCommand.SHUTDOWN" );
84          }
85          return isBlank( data ) ? DEFAULT : Shutdown.valueOf( data );
86      }
87  
88      public boolean isType( MasterProcessCommand command )
89      {
90          return command == this.command;
91      }
92  
93      @Override
94      public boolean equals( Object o )
95      {
96          if ( this == o )
97          {
98              return true;
99          }
100 
101         if ( o == null || getClass() != o.getClass() )
102         {
103             return false;
104         }
105 
106         Command arg = (Command) o;
107 
108         return command == arg.command && ( data == null ? arg.data == null : data.equals( arg.data ) );
109     }
110 
111     @Override
112     public int hashCode()
113     {
114         int result = command.hashCode();
115         result = 31 * result + ( data != null ? data.hashCode() : 0 );
116         return result;
117     }
118 }