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.eclipse.aether.transfer;
20  
21  import java.nio.ByteBuffer;
22  import java.nio.file.Path;
23  
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.*;
28  import static org.mockito.Mockito.mock;
29  
30  /**
31   */
32  public class TransferEventTest {
33  
34      private static final TransferResource res = new TransferResource("none", "file://nil", "void", (Path) null, null);
35  
36      private static final RepositorySystemSession session = mock(RepositorySystemSession.class);
37  
38      @Test
39      void testByteArrayConversion() {
40          byte[] buffer = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
41          int length = buffer.length - 2;
42          int offset = 1;
43  
44          TransferEvent event = new TransferEvent.Builder(session, res)
45                  .setDataBuffer(buffer, offset, length)
46                  .build();
47  
48          ByteBuffer bb = event.getDataBuffer();
49          byte[] dst = new byte[bb.remaining()];
50          bb.get(dst);
51  
52          byte[] expected = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
53          assertArrayEquals(expected, dst);
54      }
55  
56      @Test
57      void testRepeatableReadingOfDataBuffer() {
58          byte[] data = {0, 1, 2, 3, 4, 5, 6, 7};
59          ByteBuffer buffer = ByteBuffer.wrap(data);
60  
61          TransferEvent event =
62                  new TransferEvent.Builder(session, res).setDataBuffer(buffer).build();
63  
64          assertEquals(8, event.getDataLength());
65  
66          ByteBuffer eventBuffer = event.getDataBuffer();
67          assertNotNull(eventBuffer);
68          assertEquals(8, eventBuffer.remaining());
69  
70          byte[] eventData = new byte[8];
71          eventBuffer.get(eventData);
72          assertArrayEquals(data, eventData);
73          assertEquals(0, eventBuffer.remaining());
74          assertEquals(8, event.getDataLength());
75  
76          eventBuffer = event.getDataBuffer();
77          assertNotNull(eventBuffer);
78          assertEquals(8, eventBuffer.remaining());
79          eventBuffer.get(eventData);
80          assertArrayEquals(data, eventData);
81      }
82  }