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