View Javadoc
1   package org.apache.maven.surefire.booter;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import junit.framework.TestCase;
22  import org.junit.Rule;
23  import org.junit.rules.ExpectedException;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.DataInputStream;
27  import java.io.IOException;
28  
29  import static org.apache.maven.surefire.booter.MasterProcessCommand.*;
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.hamcrest.Matchers.*;
32  
33  /**
34   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
35   * @since 2.19
36   */
37  public class MasterProcessCommandTest
38      extends TestCase
39  {
40      @Rule
41      public final ExpectedException exception = ExpectedException.none();
42  
43      public void testEncodedStreamSequence()
44      {
45          byte[] streamSequence = new byte[10];
46          streamSequence[8] = (byte) 'T';
47          streamSequence[9] = (byte) 'e';
48          setCommandAndDataLength( 256, 2, streamSequence );
49          assertEquals( streamSequence[0], (byte) 0 );
50          assertEquals( streamSequence[1], (byte) 0 );
51          assertEquals( streamSequence[2], (byte) 1 );
52          assertEquals( streamSequence[3], (byte) 0 );
53          assertEquals( streamSequence[4], (byte) 0 );
54          assertEquals( streamSequence[5], (byte) 0 );
55          assertEquals( streamSequence[6], (byte) 0 );
56          assertEquals( streamSequence[7], (byte) 2 );
57          // remain unchanged
58          assertEquals( streamSequence[8], (byte) 'T' );
59          assertEquals( streamSequence[9], (byte) 'e' );
60      }
61  
62      public void testResolved()
63      {
64          for ( MasterProcessCommand command : MasterProcessCommand.values() )
65          {
66              assertThat( command, is( resolve( command.getId() ) ) );
67          }
68      }
69  
70      public void testDataToByteArrayAndBack()
71      {
72          String dummyData = "pkg.Test";
73          for ( MasterProcessCommand command : MasterProcessCommand.values() )
74          {
75              switch ( command )
76              {
77                  case RUN_CLASS:
78                      assertEquals( String.class, command.getDataType() );
79                      byte[] encoded = command.fromDataType( dummyData );
80                      assertThat( encoded.length, is( 8 ) );
81                      assertThat( encoded[0], is( (byte) 'p' ) );
82                      assertThat( encoded[1], is( (byte) 'k' ) );
83                      assertThat( encoded[2], is( (byte) 'g' ) );
84                      assertThat( encoded[3], is( (byte) '.' ) );
85                      assertThat( encoded[4], is( (byte) 'T' ) );
86                      assertThat( encoded[5], is( (byte) 'e' ) );
87                      assertThat( encoded[6], is( (byte) 's' ) );
88                      assertThat( encoded[7], is( (byte) 't' ) );
89                      String decoded = command.toDataTypeAsString( encoded );
90                      assertThat( decoded, is( dummyData ) );
91                      break;
92                  case TEST_SET_FINISHED:
93                      assertEquals( Void.class, command.getDataType() );
94                      encoded = command.fromDataType( dummyData );
95                      assertThat( encoded.length, is( 0 ) );
96                      decoded = command.toDataTypeAsString( encoded );
97                      assertNull( decoded );
98                      break;
99                  case SKIP_SINCE_NEXT_TEST:
100                     assertEquals( Void.class, command.getDataType() );
101                     encoded = command.fromDataType( dummyData );
102                     assertThat( encoded.length, is( 0 ) );
103                     decoded = command.toDataTypeAsString( encoded );
104                     assertNull( decoded );
105                     break;
106                 case SHUTDOWN:
107                     assertEquals( String.class, command.getDataType() );
108                     encoded = command.fromDataType( Shutdown.EXIT.name() );
109                     assertThat( encoded.length, is( 4 ) );
110                     decoded = command.toDataTypeAsString( encoded );
111                     assertThat( decoded, is( Shutdown.EXIT.name() ) );
112                     break;
113                 case NOOP:
114                     assertEquals( Void.class, command.getDataType() );
115                     encoded = command.fromDataType( dummyData );
116                     assertThat( encoded.length, is( 0 ) );
117                     decoded = command.toDataTypeAsString( encoded );
118                     assertNull( decoded );
119                     break;
120                 case  BYE_ACK:
121                     assertEquals( Void.class, command.getDataType() );
122                     encoded = command.fromDataType( dummyData );
123                     assertThat( encoded.length, is( 0 ) );
124                     decoded = command.toDataTypeAsString( encoded );
125                     assertNull( decoded );
126                     break;
127                 default:
128                     fail();
129             }
130             assertThat( command, is( resolve( command.getId() ) ) );
131         }
132     }
133 
134     public void testEncodedDecodedIsSameForRunClass()
135         throws IOException
136     {
137         byte[] encoded = RUN_CLASS.encode( "pkg.Test" );
138         assertThat( encoded.length, is( 16 ) );
139         assertThat( encoded[0], is( (byte) 0 ) );
140         assertThat( encoded[1], is( (byte) 0 ) );
141         assertThat( encoded[2], is( (byte) 0 ) );
142         assertThat( encoded[3], is( (byte) 0 ) );
143         assertThat( encoded[4], is( (byte) 0 ) );
144         assertThat( encoded[5], is( (byte) 0 ) );
145         assertThat( encoded[6], is( (byte) 0 ) );
146         assertThat( encoded[7], is( (byte) 8 ) );
147         assertThat( encoded[8], is( (byte) 'p' ) );
148         assertThat( encoded[9], is( (byte) 'k' ) );
149         assertThat( encoded[10], is( (byte) 'g' ) );
150         assertThat( encoded[11], is( (byte) '.' ) );
151         assertThat( encoded[12], is( (byte) 'T' ) );
152         assertThat( encoded[13], is( (byte) 'e' ) );
153         assertThat( encoded[14], is( (byte) 's' ) );
154         assertThat( encoded[15], is( (byte) 't' ) );
155         Command command = decode( new DataInputStream( new ByteArrayInputStream( encoded ) ) );
156         assertNotNull( command );
157         assertThat( command.getCommandType(), is( RUN_CLASS ) );
158         assertThat( command.getData(), is( "pkg.Test" ) );
159     }
160 
161     public void testShouldDecodeTwoCommands() throws IOException
162     {
163         byte[] cmd1 = BYE_ACK.encode();
164         byte[] cmd2 = NOOP.encode();
165         byte[] stream = new byte[cmd1.length + cmd2.length];
166         System.arraycopy( cmd1, 0, stream, 0, cmd1.length );
167         System.arraycopy( cmd2, 0, stream, cmd1.length, cmd2.length );
168         DataInputStream is = new DataInputStream( new ByteArrayInputStream( stream ) );
169         Command bye = decode( is );
170         assertNotNull( bye );
171         assertThat( bye.getCommandType(), is( BYE_ACK ) );
172         Command noop = decode( is );
173         assertNotNull( noop );
174         assertThat( noop.getCommandType(), is( NOOP ) );
175     }
176 }