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.name());
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       * @throws IllegalArgumentException if string data {@link #getData()} is not applicable to enum {@link Shutdown}
69       */
70      public Shutdown toShutdownData() {
71          if (command != MasterProcessCommand.SHUTDOWN) {
72              throw new IllegalStateException("expected MasterProcessCommand.SHUTDOWN");
73          }
74          return isBlank(data) ? Shutdown.DEFAULT : Shutdown.valueOf(data);
75      }
76  
77      @Override
78      public boolean equals(Object o) {
79          if (this == o) {
80              return true;
81          }
82  
83          if (o == null || getClass() != o.getClass()) {
84              return false;
85          }
86  
87          Command arg = (Command) o;
88  
89          return command == arg.command && Objects.equals(data, arg.data);
90      }
91  
92      @Override
93      public int hashCode() {
94          int result = command.hashCode();
95          result = 31 * result + (data != null ? data.hashCode() : 0);
96          return result;
97      }
98  }