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