View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.booter;
20  
21  import java.util.Objects;
22  
23  import static java.util.Objects.requireNonNull;
24  import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
25  
26  /**
27   * Encapsulates data and command sent from master to forked process.
28   *
29   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
30   * @since 2.19
31   */
32  public final class Command {
33      public static final Command TEST_SET_FINISHED = new Command(MasterProcessCommand.TEST_SET_FINISHED);
34      public static final Command SKIP_SINCE_NEXT_TEST = new Command(MasterProcessCommand.SKIP_SINCE_NEXT_TEST);
35      public static final Command NOOP = new Command(MasterProcessCommand.NOOP);
36      public static final Command BYE_ACK = new Command(MasterProcessCommand.BYE_ACK);
37  
38      private final MasterProcessCommand command;
39      private final String data;
40  
41      public Command(MasterProcessCommand command, String data) {
42          this.command = requireNonNull(command);
43          this.data = data;
44      }
45  
46      public Command(MasterProcessCommand command) {
47          this(command, null);
48      }
49  
50      public static Command toShutdown(Shutdown shutdownType) {
51          return new Command(MasterProcessCommand.SHUTDOWN, shutdownType.getParam());
52      }
53  
54      public static Command toRunClass(String runClass) {
55          return new Command(MasterProcessCommand.RUN_CLASS, runClass);
56      }
57  
58      public MasterProcessCommand getCommandType() {
59          return command;
60      }
61  
62      public String getData() {
63          return data;
64      }
65  
66      /**
67       * @return {@link Shutdown} or {@link Shutdown#DEFAULT} if {@link #getData()} is null or blank string
68       */
69      public Shutdown toShutdownData() {
70          if (command != MasterProcessCommand.SHUTDOWN) {
71              throw new IllegalStateException("expected MasterProcessCommand.SHUTDOWN");
72          }
73          return isBlank(data) ? Shutdown.DEFAULT : Shutdown.parameterOf(data);
74      }
75  
76      @Override
77      public boolean equals(Object o) {
78          if (this == o) {
79              return true;
80          }
81  
82          if (o == null || getClass() != o.getClass()) {
83              return false;
84          }
85  
86          Command arg = (Command) o;
87  
88          return command == arg.command && Objects.equals(data, arg.data);
89      }
90  
91      @Override
92      public int hashCode() {
93          int result = command.hashCode();
94          result = 31 * result + (data != null ? data.hashCode() : 0);
95          return result;
96      }
97  }