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.commons.weaver.utils;
20  
21  import static org.junit.Assert.assertThat;
22  
23  import java.util.Arrays;
24  
25  import org.apache.commons.weaver.Consumes;
26  import org.apache.commons.weaver.Produces;
27  import org.apache.commons.weaver.lifecycle.WeaveLifecycleToken;
28  import org.apache.commons.weaver.spi.WeaveLifecycleProvider;
29  import org.apache.commons.weaver.spi.Weaver;
30  import org.hamcrest.collection.IsIterableContainingInOrder;
31  import org.junit.Test;
32  
33  public class ProvidersTest {
34  
35      @Test(expected = NullPointerException.class)
36      public void testSortNull() {
37          Providers.sort(null);
38      }
39  
40      @Test(expected = IllegalArgumentException.class)
41      public void testSortNullElement() {
42          Providers.sort(Arrays.asList((Weaver) null));
43      }
44  
45      public interface FauxWeaveProvider extends WeaveLifecycleProvider<WeaveLifecycleToken.Weave> {
46      }
47  
48      public class A implements FauxWeaveProvider {
49      }
50  
51      @Consumes(A.class)
52      @Produces(C.class)
53      public class B implements FauxWeaveProvider {
54      }
55  
56      public class C implements FauxWeaveProvider {
57      }
58  
59      @Consumes(X.class)
60      public class W implements FauxWeaveProvider {
61      }
62  
63      @Consumes(Y.class)
64      public class X implements FauxWeaveProvider {
65      }
66  
67      public class Y implements FauxWeaveProvider {
68      }
69  
70      @Produces(Y.class)
71      public class Z implements FauxWeaveProvider {
72      }
73  
74      @Consumes(Y.class)
75      @Produces(Z.class)
76      public class Monkeywrench implements FauxWeaveProvider {
77      }
78  
79      private FauxWeaveProvider a = new A(), b = new B(), c = new C(), w = new W(), x = new X(), y = new Y(), z = new Z(),
80                      monkeywrench = new Monkeywrench();
81  
82      @Test
83      public void testSort() {
84          assertThat(Providers.sort(Arrays.asList(b, a, c)), IsIterableContainingInOrder.contains(a, b, c));
85          assertThat(Providers.sort(Arrays.asList(y, w, x, z)), IsIterableContainingInOrder.contains(z, y, x, w));
86      }
87  
88      @Test(expected = IllegalStateException.class)
89      public void testCircularSort() {
90          Providers.sort(Arrays.asList(y, z, monkeywrench));
91      }
92  
93  }