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   * Licensed to the Apache Software Foundation (ASF) under one
12   * or more contributor license agreements.  See the NOTICE file
13   * distributed with this work for additional information
14   * regarding copyright ownership.  The ASF licenses this file
15   * to you under the Apache License, Version 2.0 (the
16   * "License"); you may not use this file except in compliance
17   * with the License.  You may obtain a copy of the License at
18   *
19   *  http://www.apache.org/licenses/LICENSE-2.0
20   *
21   * Unless required by applicable law or agreed to in writing,
22   * software distributed under the License is distributed on an
23   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24   * KIND, either express or implied.  See the License for the
25   * specific language governing permissions and limitations
26   * under the License.
27   */
28  
29  /**
30   * Test for {@link ConsoleDownloadMonitor}
31   * 
32   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
33   * @version $Id: ConsoleDownloadMonitorTest.java 746136 2009-02-20 04:53:53Z brett $
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  }