View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.Method;
26  import java.lang.reflect.Proxy;
27  import java.util.Locale;
28  
29  import org.eclipse.aether.DefaultRepositorySystemSession;
30  import org.eclipse.aether.RepositoryEvent;
31  import org.eclipse.aether.RepositoryListener;
32  import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
33  import org.eclipse.aether.internal.test.util.TestUtils;
34  import org.junit.Test;
35  
36  /**
37   */
38  public class DefaultRepositoryEventDispatcherTest
39  {
40  
41      @Test
42      public void testDispatchHandlesAllEventTypes()
43      {
44          DefaultRepositoryEventDispatcher dispatcher = new DefaultRepositoryEventDispatcher();
45  
46          ListenerHandler handler = new ListenerHandler();
47  
48          RepositoryListener listener =
49              (RepositoryListener) Proxy.newProxyInstance( getClass().getClassLoader(),
50                                                           new Class<?>[] { RepositoryListener.class }, handler );
51  
52          DefaultRepositorySystemSession session = TestUtils.newSession();
53          session.setRepositoryListener( listener );
54  
55          for ( RepositoryEvent.EventType type : RepositoryEvent.EventType.values() )
56          {
57              RepositoryEvent event = new RepositoryEvent.Builder( session, type ).build();
58  
59              handler.methodName = null;
60  
61              dispatcher.dispatch( event );
62  
63              assertNotNull( "not handled: " + type, handler.methodName );
64  
65              assertEquals( "badly handled: " + type, type.name().replace( "_", "" ).toLowerCase( Locale.ENGLISH ),
66                            handler.methodName.toLowerCase( Locale.ENGLISH ) );
67          }
68      }
69  
70      static class ListenerHandler
71          implements InvocationHandler
72      {
73  
74          public String methodName;
75  
76          public Object invoke( Object proxy, Method method, Object[] args )
77          {
78              if ( args.length == 1 && args[0] instanceof RepositoryEvent )
79              {
80                  methodName = method.getName();
81              }
82  
83              return null;
84          }
85  
86      }
87  
88  }