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.apache.maven.cli.transfer;
20  
21  import java.io.File;
22  import java.io.PrintStream;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.CountDownLatch;
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  
29  import org.apache.maven.jline.JLineMessageBuilderFactory;
30  import org.eclipse.aether.DefaultRepositorySystemSession;
31  import org.eclipse.aether.transfer.TransferCancelledException;
32  import org.eclipse.aether.transfer.TransferEvent;
33  import org.eclipse.aether.transfer.TransferListener;
34  import org.eclipse.aether.transfer.TransferResource;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  class ConsoleMavenTransferListenerTest {
40  
41      private CountDownLatch startLatch;
42      private CountDownLatch endLatch;
43  
44      @Test
45      void testTransferProgressedWithPrintResourceNames() throws Exception {
46          int size = 1000;
47          ExecutorService service = Executors.newFixedThreadPool(size * 2);
48          startLatch = new CountDownLatch(size);
49          endLatch = new CountDownLatch(size);
50          Map<String, String> output = new ConcurrentHashMap<String, String>();
51  
52          TransferListener listener = new SimplexTransferListener(new ConsoleMavenTransferListener(
53                  new JLineMessageBuilderFactory(),
54                  new PrintStream(System.out) {
55  
56                      @Override
57                      public void print(Object o) {
58  
59                          String string = o.toString();
60                          int i = string.length() - 1;
61                          while (i >= 0) {
62                              char c = string.charAt(i);
63                              if (c == '\n' || c == '\r' || c == ' ') i--;
64                              else break;
65                          }
66  
67                          string = string.substring(0, i + 1).trim();
68                          output.put(string, string);
69                          System.out.print(o);
70                      }
71                  },
72                  true));
73          TransferResource resource =
74                  new TransferResource(null, null, "http://maven.org/test/test-resource", new File(""), null);
75          resource.setContentLength(size - 1);
76  
77          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false); // no close handle
78  
79          // warm up
80          test(listener, session, resource, 0);
81  
82          for (int i = 1; i < size; i++) {
83              final int bytes = i;
84  
85              service.execute(() -> {
86                  test(listener, session, resource, bytes);
87              });
88          }
89  
90          // start all threads at once
91          try {
92              startLatch.await();
93          } catch (InterruptedException e) {
94              e.printStackTrace();
95          }
96  
97          // wait for all thread to end
98          try {
99              endLatch.await();
100         } catch (InterruptedException e) {
101             e.printStackTrace();
102         }
103 
104         // despite all are back, we need to make sure all the events are processed (are async)
105         // this one should block until all processed
106         listener.transferSucceeded(new TransferEvent.Builder(session, resource)
107                 .setType(TransferEvent.EventType.SUCCEEDED)
108                 .build());
109 
110         StringBuilder message = new StringBuilder("Messages [");
111         boolean test = true;
112         for (int i = 0; i < 999; i++) {
113             boolean ok = output.containsKey("Progress (1): test-resource (" + i + "/999 B)");
114             if (!ok) {
115                 System.out.println("false : " + i);
116                 message.append(i + ",");
117             }
118             test = test & ok;
119         }
120         assertTrue(test, message + "] are missing in " + output);
121     }
122 
123     private void test(
124             TransferListener listener,
125             DefaultRepositorySystemSession session,
126             TransferResource resource,
127             final int bytes) {
128         TransferEvent event = new TransferEvent.Builder(session, resource)
129                 .setType(TransferEvent.EventType.PROGRESSED)
130                 .setTransferredBytes(bytes)
131                 .build();
132         startLatch.countDown();
133         try {
134             listener.transferProgressed(event);
135         } catch (TransferCancelledException e) {
136         }
137         endLatch.countDown();
138     }
139 }