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          throws Exception
44      {
45          DefaultRepositoryEventDispatcher dispatcher = new DefaultRepositoryEventDispatcher();
46  
47          ListenerHandler handler = new ListenerHandler();
48  
49          RepositoryListener listener =
50              (RepositoryListener) Proxy.newProxyInstance( getClass().getClassLoader(),
51                                                           new Class<?>[] { RepositoryListener.class }, handler );
52  
53          DefaultRepositorySystemSession session = TestUtils.newSession();
54          session.setRepositoryListener( listener );
55  
56          for ( RepositoryEvent.EventType type : RepositoryEvent.EventType.values() )
57          {
58              RepositoryEvent event = new RepositoryEvent.Builder( session, type ).build();
59  
60              handler.methodName = null;
61  
62              dispatcher.dispatch( event );
63  
64              assertNotNull( "not handled: " + type, handler.methodName );
65  
66              assertEquals( "badly handled: " + type, type.name().replace( "_", "" ).toLowerCase( Locale.ENGLISH ),
67                            handler.methodName.toLowerCase( Locale.ENGLISH ) );
68          }
69      }
70  
71      static class ListenerHandler
72          implements InvocationHandler
73      {
74  
75          public String methodName;
76  
77          public Object invoke( Object proxy, Method method, Object[] args )
78              throws Throwable
79          {
80              if ( args.length == 1 && args[0] instanceof RepositoryEvent )
81              {
82                  methodName = method.getName();
83              }
84  
85              return null;
86          }
87  
88      }
89  
90  }