1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.Test;
26
27 import static org.junit.Assert.*;
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 public 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 public 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 }