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.stream;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  import java.nio.channels.WritableByteChannel;
26  import java.nio.charset.Charset;
27  import java.nio.charset.CharsetEncoder;
28  
29  import org.apache.maven.surefire.api.booter.MasterProcessCommand;
30  import org.apache.maven.surefire.api.stream.AbstractStreamEncoder;
31  
32  import static org.apache.maven.surefire.api.booter.Constants.DEFAULT_STREAM_ENCODING;
33  import static org.apache.maven.surefire.api.booter.Constants.DEFAULT_STREAM_ENCODING_BYTES;
34  import static org.apache.maven.surefire.api.booter.Constants.MAGIC_NUMBER_FOR_COMMANDS_BYTES;
35  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.BYE_ACK;
36  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.NOOP;
37  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.RUN_CLASS;
38  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.SHUTDOWN;
39  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.SKIP_SINCE_NEXT_TEST;
40  import static org.apache.maven.surefire.api.booter.MasterProcessCommand.TEST_SET_FINISHED;
41  
42  public class CommandEncoder extends AbstractStreamEncoder<MasterProcessCommand> implements AutoCloseable {
43      private final WritableByteChannel out;
44  
45      public CommandEncoder(WritableByteChannel out) {
46          super(out);
47          this.out = out;
48      }
49  
50      public void sendRunClass(String testClassName) throws IOException {
51          CharsetEncoder encoder = newCharsetEncoder();
52          int bufferMaxLength = estimateBufferLength(RUN_CLASS.getOpcodeLength(), null, encoder, 0, 0, testClassName);
53          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
54          encode(encoder, result, RUN_CLASS, testClassName);
55          write(result, true);
56      }
57  
58      public void sendTestSetFinished() throws IOException {
59          int bufferMaxLength = estimateBufferLength(TEST_SET_FINISHED.getOpcodeLength(), null, null, 0, 0);
60          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
61          encodeHeader(result, TEST_SET_FINISHED);
62          write(result, true);
63      }
64  
65      public void sendSkipSinceNextTest() throws IOException {
66          int bufferMaxLength = estimateBufferLength(SKIP_SINCE_NEXT_TEST.getOpcodeLength(), null, null, 0, 0);
67          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
68          encodeHeader(result, SKIP_SINCE_NEXT_TEST);
69          write(result, true);
70      }
71  
72      public void sendShutdown(String shutdownData) throws IOException {
73          CharsetEncoder encoder = newCharsetEncoder();
74          int bufferMaxLength = estimateBufferLength(SHUTDOWN.getOpcodeLength(), null, encoder, 0, 0, shutdownData);
75          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
76          encode(encoder, result, SHUTDOWN, shutdownData);
77          write(result, true);
78      }
79  
80      public void sendNoop() throws IOException {
81          int bufferMaxLength = estimateBufferLength(NOOP.getOpcodeLength(), null, null, 0, 0);
82          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
83          encodeHeader(result, NOOP);
84          write(result, true);
85      }
86  
87      public void sendByeAck() throws IOException {
88          int bufferMaxLength = estimateBufferLength(BYE_ACK.getOpcodeLength(), null, null, 0, 0);
89          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
90          encodeHeader(result, BYE_ACK);
91          write(result, true);
92      }
93  
94      @Nonnull
95      @Override
96      protected final byte[] getEncodedMagicNumber() {
97          return MAGIC_NUMBER_FOR_COMMANDS_BYTES;
98      }
99  
100     @Nonnull
101     @Override
102     protected final byte[] enumToByteArray(MasterProcessCommand masterProcessCommand) {
103         return masterProcessCommand.getOpcodeBinary();
104     }
105 
106     @Nonnull
107     @Override
108     protected final byte[] getEncodedCharsetName() {
109         return DEFAULT_STREAM_ENCODING_BYTES;
110     }
111 
112     @Nonnull
113     @Override
114     protected final Charset getCharset() {
115         return DEFAULT_STREAM_ENCODING;
116     }
117 
118     @Nonnull
119     @Override
120     protected final CharsetEncoder newCharsetEncoder() {
121         return getCharset().newEncoder();
122     }
123 
124     @Override
125     public void close() throws IOException {
126         out.close();
127     }
128 }