1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mkstubs.sourcer;
18
19
20import org.junit.After;
21import org.junit.Assert;
22import org.junit.Before;
23import org.junit.Test;
24import org.objectweb.asm.signature.SignatureReader;
25
26import java.util.ArrayList;
27
28/**
29 *
30 */
31public class SignatureSourcerTest {
32
33    private SignatureSourcer mSourcer;
34
35    @Before
36    public void setUp() throws Exception {
37        mSourcer = new SignatureSourcer();
38    }
39
40    @After
41    public void tearDown() throws Exception {
42    }
43
44    @Test
45    public void testReturnMapNoArgs() {
46        SignatureReader reader = new SignatureReader(
47                "()Ljava/util/Map<Ljava/util/ArrayList<TT;>;Ljava/util/Map<Ljava/lang/String;Ljava/util/ArrayList<TU;>;>;>;");
48        reader.accept(mSourcer);
49        String result = mSourcer.getReturnType().toString();
50
51        Assert.assertEquals(
52                "java.util.Map<java.util.ArrayList<T>, java.util.Map<java.lang.String, java.util.ArrayList<U>>>",
53                result);
54    }
55
56    @Test
57    public void testReturnVoid() {
58        SignatureReader reader = new SignatureReader(
59                "(Ljava/util/List<+Lorg/w3c/dom/css/Rect;>;)V");
60        reader.accept(mSourcer);
61        String result = mSourcer.getReturnType().toString();
62
63        Assert.assertEquals(
64                "void",
65                result);
66    }
67
68    @Test
69    public void testSimpleArg() {
70        SignatureReader reader = new SignatureReader(
71                "(Ljava/util/List<+Lorg/w3c/dom/css/Rect;>;)V");
72        reader.accept(mSourcer);
73
74        ArrayList<SignatureSourcer> params = mSourcer.getParameters();
75        Assert.assertNotNull(params);
76
77        String[] array = toStringArray(params);
78
79        Assert.assertArrayEquals(
80                new String[] { "java.util.List<? extends org.w3c.dom.css.Rect>" },
81                array);
82    }
83
84    @Test
85    public void testFormalParameters1() {
86        SignatureReader reader = new SignatureReader("<X:TT;Y:Ljava/lang/Object;>()V");
87        reader.accept(mSourcer);
88
89        Assert.assertTrue(mSourcer.hasFormalsContent());
90
91        String result = mSourcer.formalsToString();
92        Assert.assertEquals(
93                "<X extends T, Y extends java.lang.Object>",
94                result);
95    }
96
97    @Test
98    public void testFormalParameters2() {
99        SignatureReader reader = new SignatureReader("<T::Ljava/lang/Comparable<-TT;>;>(Ljava/util/List<TT;>;)V");
100        reader.accept(mSourcer);
101
102        Assert.assertTrue(mSourcer.hasFormalsContent());
103
104        String result = mSourcer.formalsToString();
105        Assert.assertEquals(
106                "<T extends java.lang.Comparable<? super T>>",
107                result);
108    }
109
110    @Test
111    public void testManyArgs() {
112        SignatureReader reader = new SignatureReader(
113                "<X:TT;Y:Ljava/lang/Object;>(Ljava/util/List<TT;>;Ljava/util/Map<TT;TU;>;Ljava/util/Map<TX;Ljava/util/Set<-TY;>;>;)V");
114        reader.accept(mSourcer);
115
116        Assert.assertTrue(mSourcer.hasFormalsContent());
117        String formals = mSourcer.formalsToString();
118        Assert.assertEquals(
119                "<X extends T, Y extends java.lang.Object>",
120                formals);
121
122        String result = mSourcer.getReturnType().toString();
123        Assert.assertEquals(
124                "void",
125                result);
126
127        ArrayList<SignatureSourcer> params = mSourcer.getParameters();
128        Assert.assertNotNull(params);
129
130        String[] array = toStringArray(params);
131
132        Assert.assertArrayEquals(
133                new String[] { "java.util.List<T>",
134                               "java.util.Map<T, U>",
135                               "java.util.Map<X, java.util.Set<? super Y>>" },
136                array);
137    }
138
139    private String[] toStringArray(ArrayList<?> params) {
140        String[] array = new String[params.size()];
141        for (int i = 0; i < params.size(); i++) {
142            array[i] = params.get(i).toString();
143        }
144        return array;
145    }
146}
147