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.internal.impl;
20  
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Method;
23  import java.lang.reflect.Proxy;
24  import java.util.Locale;
25  
26  import org.eclipse.aether.DefaultRepositorySystemSession;
27  import org.eclipse.aether.RepositoryEvent;
28  import org.eclipse.aether.RepositoryListener;
29  import org.eclipse.aether.internal.test.util.TestUtils;
30  import org.junit.Test;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   */
36  public class DefaultRepositoryEventDispatcherTest {
37  
38      @Test
39      public void testDispatchHandlesAllEventTypes() {
40          DefaultRepositoryEventDispatcher dispatcher = new DefaultRepositoryEventDispatcher();
41  
42          ListenerHandler handler = new ListenerHandler();
43  
44          RepositoryListener listener = (RepositoryListener)
45                  Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] {RepositoryListener.class}, handler);
46  
47          DefaultRepositorySystemSession session = TestUtils.newSession();
48          session.setRepositoryListener(listener);
49  
50          for (RepositoryEvent.EventType type : RepositoryEvent.EventType.values()) {
51              RepositoryEvent event = new RepositoryEvent.Builder(session, type).build();
52  
53              handler.methodName = null;
54  
55              dispatcher.dispatch(event);
56  
57              assertNotNull("not handled: " + type, handler.methodName);
58  
59              assertEquals(
60                      "badly handled: " + type,
61                      type.name().replace("_", "").toLowerCase(Locale.ENGLISH),
62                      handler.methodName.toLowerCase(Locale.ENGLISH));
63          }
64      }
65  
66      static class ListenerHandler implements InvocationHandler {
67  
68          public String methodName;
69  
70          public Object invoke(Object proxy, Method method, Object[] args) {
71              if (args.length == 1 && args[0] instanceof RepositoryEvent) {
72                  methodName = method.getName();
73              }
74  
75              return null;
76          }
77      }
78  }