Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 05f2056962828717f424a5a3d73f55ef4a9478a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.jsr356.utils;

import static org.hamcrest.Matchers.nullValue;

import org.eclipse.jetty.websocket.common.util.ReflectUtils;
import org.junit.Assert;
import org.junit.Test;

public class ReflectUtilsTest
{
    public static interface Fruit<T>
    {
    }

    public static interface Color<T>
    {
    }

    public static interface Food<T> extends Fruit<T>
    {
    }

    public static abstract class Apple<T extends Object> implements Fruit<T>, Color<String>
    {
    }

    public static abstract class Cherry<A extends Object, B extends Number> implements Fruit<A>, Color<B>
    {
    }

    public static abstract class Banana implements Fruit<String>, Color<String>
    {
    }

    public static class Washington<Z extends Number, X extends Object> extends Cherry<X, Z>
    {
    }

    public static class Rainier extends Washington<Float, Short>
    {
    }

    public static class Pizza implements Food<Integer>
    {
    }

    public static class Cavendish extends Banana
    {
    }

    public static class GrannySmith extends Apple<Long>
    {
    }

    public static class Pear implements Fruit<String>, Color<Double>
    {
    }

    public static class Kiwi implements Fruit<Character>
    {
    }

    @Test
    public void testFindGeneric_PearFruit()
    {
        assertFindGenericClass(Pear.class,Fruit.class,String.class);
    }

    @Test
    public void testFindGeneric_PizzaFruit()
    {
        assertFindGenericClass(Pizza.class,Fruit.class,Integer.class);
    }

    @Test
    public void testFindGeneric_KiwiFruit()
    {
        assertFindGenericClass(Kiwi.class,Fruit.class,Character.class);
    }

    @Test
    public void testFindGeneric_PearColor()
    {
        assertFindGenericClass(Pear.class,Color.class,Double.class);
    }

    @Test
    public void testFindGeneric_GrannySmithFruit()
    {
        assertFindGenericClass(GrannySmith.class,Fruit.class,Long.class);
    }

    @Test
    public void testFindGeneric_CavendishFruit()
    {
        assertFindGenericClass(Cavendish.class,Fruit.class,String.class);
    }

    @Test
    public void testFindGeneric_RainierFruit()
    {
        assertFindGenericClass(Rainier.class,Fruit.class,Short.class);
    }

    @Test
    public void testFindGeneric_WashingtonFruit()
    {
        // Washington does not have a concrete implementation
        // of the Fruit interface, this should return null
        Class<?> impl = ReflectUtils.findGenericClassFor(Washington.class,Fruit.class);
        Assert.assertThat("Washington -> Fruit implementation",impl,nullValue());
    }

    private void assertFindGenericClass(Class<?> baseClass, Class<?> ifaceClass, Class<?> expectedClass)
    {
        Class<?> foundClass = ReflectUtils.findGenericClassFor(baseClass,ifaceClass);
        String msg = String.format("Expecting %s<%s> found on %s",ifaceClass.getName(),expectedClass.getName(),baseClass.getName());
        Assert.assertEquals(msg,expectedClass,foundClass);
    }
}

Back to the top