1 package org.apache.maven.cli;
2
3 import org.apache.maven.wagon.resource.Resource;
4 import org.apache.maven.wagon.WagonConstants;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.PrintWriter;
8 import java.io.PrintStream;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class ConsoleDownloadMonitorTest
36 extends AbstractConsoleDownloadMonitorTest
37 {
38 ByteArrayOutputStream bout;
39 protected void setUp()
40 throws Exception
41 {
42 super.setMonitor( new ConsoleDownloadMonitor() );
43 super.setUp();
44 bout = new ByteArrayOutputStream();
45 monitor.out = new PrintStream(bout);
46 }
47
48 public void testTransferProgress()
49 throws Exception
50 {
51 byte[] buffer = new byte[1024];
52 monitor.transferProgress( new TransferEventMock(new Resource(), 10000), buffer, 1024 );
53 assertEquals("1/9K\r", new String(bout.toByteArray()));
54 }
55
56 public void testTransferProgressTwoFiles()
57 throws Exception
58 {
59 byte[] buffer = new byte[2048];
60 monitor.transferProgress( new TransferEventMock(new Resource("foo"), 10000), buffer, 1024 );
61 assertEquals("1/9K\r", new String(bout.toByteArray()));
62 bout.reset();
63 monitor.transferProgress( new TransferEventMock(new Resource("bar"), 10000), buffer, 2048 );
64 assertEquals("1/9K 2/9K\r", new String(bout.toByteArray()));
65 bout.reset();
66 monitor.transferProgress( new TransferEventMock(new Resource("bar"), 10000), buffer, 2048 );
67 assertEquals("1/9K 4/9K\r", new String(bout.toByteArray()));
68 bout.reset();
69 monitor.transferProgress( new TransferEventMock(new Resource("foo"), 10000), buffer, 2048 );
70 assertEquals("3/9K 4/9K\r", new String(bout.toByteArray()));
71 }
72
73 public void testGetDownloadStatusForResource()
74 {
75 ConsoleDownloadMonitor cm = (ConsoleDownloadMonitor)monitor;
76 assertEquals("200/400b", cm.getDownloadStatusForResource(200, 400));
77 assertEquals("1/2K", cm.getDownloadStatusForResource(1024, 2048));
78 assertEquals("0/2K", cm.getDownloadStatusForResource(10, 2048));
79 assertEquals("10/?", cm.getDownloadStatusForResource(10, WagonConstants.UNKNOWN_LENGTH));
80 assertEquals("1024/?", cm.getDownloadStatusForResource(1024, WagonConstants.UNKNOWN_LENGTH));
81 }
82 }