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  /**
43   *
44   */
45  public class CommandEncoder extends AbstractStreamEncoder<MasterProcessCommand> implements AutoCloseable {
46      private final WritableByteChannel out;
47  
48      public CommandEncoder(WritableByteChannel out) {
49          super(out);
50          this.out = out;
51      }
52  
53      public void sendRunClass(String testClassName) throws IOException {
54          CharsetEncoder encoder = newCharsetEncoder();
55          int bufferMaxLength = estimateBufferLength(RUN_CLASS.getOpcodeLength(), null, encoder, 0, 0, testClassName);
56          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
57          encode(encoder, result, RUN_CLASS, testClassName);
58          write(result, true);
59      }
60  
61      public void sendTestSetFinished() throws IOException {
62          int bufferMaxLength = estimateBufferLength(TEST_SET_FINISHED.getOpcodeLength(), null, null, 0, 0);
63          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
64          encodeHeader(result, TEST_SET_FINISHED);
65          write(result, true);
66      }
67  
68      public void sendSkipSinceNextTest() throws IOException {
69          int bufferMaxLength = estimateBufferLength(SKIP_SINCE_NEXT_TEST.getOpcodeLength(), null, null, 0, 0);
70          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
71          encodeHeader(result, SKIP_SINCE_NEXT_TEST);
72          write(result, true);
73      }
74  
75      public void sendShutdown(String shutdownData) throws IOException {
76          CharsetEncoder encoder = newCharsetEncoder();
77          int bufferMaxLength = estimateBufferLength(SHUTDOWN.getOpcodeLength(), null, encoder, 0, 0, shutdownData);
78          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
79          encode(encoder, result, SHUTDOWN, shutdownData);
80          write(result, true);
81      }
82  
83      public void sendNoop() throws IOException {
84          int bufferMaxLength = estimateBufferLength(NOOP.getOpcodeLength(), null, null, 0, 0);
85          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
86          encodeHeader(result, NOOP);
87          write(result, true);
88      }
89  
90      public void sendByeAck() throws IOException {
91          int bufferMaxLength = estimateBufferLength(BYE_ACK.getOpcodeLength(), null, null, 0, 0);
92          ByteBuffer result = ByteBuffer.allocate(bufferMaxLength);
93          encodeHeader(result, BYE_ACK);
94          write(result, true);
95      }
96  
97      @Nonnull
98      @Override
99      protected final byte[] getEncodedMagicNumber() {
100         return MAGIC_NUMBER_FOR_COMMANDS_BYTES;
101     }
102 
103     @Nonnull
104     @Override
105     protected final byte[] enumToByteArray(MasterProcessCommand masterProcessCommand) {
106         return masterProcessCommand.getOpcodeBinary();
107     }
108 
109     @Nonnull
110     @Override
111     protected final byte[] getEncodedCharsetName() {
112         return DEFAULT_STREAM_ENCODING_BYTES;
113     }
114 
115     @Nonnull
116     @Override
117     protected final Charset getCharset() {
118         return DEFAULT_STREAM_ENCODING;
119     }
120 
121     @Nonnull
122     @Override
123     protected final CharsetEncoder newCharsetEncoder() {
124         return getCharset().newEncoder();
125     }
126 
127     @Override
128     public void close() throws IOException {
129         out.close();
130     }
131 }