View Javadoc
1   package org.apache.maven.surefire.api.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 org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Segment;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import static java.nio.charset.StandardCharsets.US_ASCII;
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Commands which are sent from plugin to the forked jvm.
32   *
33   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
34   * @since 2.19
35   */
36  public enum MasterProcessCommand
37  {
38      RUN_CLASS( "run-testclass", String.class ),
39      TEST_SET_FINISHED( "testset-finished", Void.class ),
40      SKIP_SINCE_NEXT_TEST( "skip-since-next-test", Void.class ),
41      SHUTDOWN( "shutdown", String.class ),
42  
43      /** To tell a forked process that the master process is still alive. Repeated after 10 seconds. */
44      NOOP( "noop", Void.class ),
45      BYE_ACK( "bye-ack", Void.class );
46  
47      // due to have fast and thread-safe Map
48      public static final Map<Segment, MasterProcessCommand> COMMAND_TYPES = segmentsToCmds();
49  
50      private final String opcode;
51      private final byte[] opcodeBinary;
52      private final Class<?> dataType;
53  
54      MasterProcessCommand( String opcode, Class<?> dataType )
55      {
56          this.opcode = requireNonNull( opcode, "value cannot be null" );
57          opcodeBinary = opcode.getBytes( US_ASCII );
58          this.dataType = requireNonNull( dataType, "dataType cannot be null" );
59      }
60  
61      public byte[] getOpcodeBinary()
62      {
63          return opcodeBinary;
64      }
65  
66      public int getOpcodeLength()
67      {
68          return opcodeBinary.length;
69      }
70  
71      public Class<?> getDataType()
72      {
73          return dataType;
74      }
75  
76      public boolean hasDataType()
77      {
78          return dataType != Void.class;
79      }
80  
81      @Override
82      public String toString()
83      {
84          return opcode;
85      }
86  
87      private static Map<Segment, MasterProcessCommand> segmentsToCmds()
88      {
89          Map<Segment, MasterProcessCommand> commands = new HashMap<>();
90          for ( MasterProcessCommand command : MasterProcessCommand.values() )
91          {
92              byte[] array = command.toString().getBytes( US_ASCII );
93              commands.put( new Segment( array, 0, array.length ), command );
94          }
95          return commands;
96      }
97  }