1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }