aboutsummaryrefslogtreecommitdiffstats
path: root/api/src/test/java/org/osjava/jardiff/ToolsTest.java
blob: 7af6d45e9d98f2669c628cc8c5fd6a69d084ffd5 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Copyright 2012-2014 Julien Eluard and contributors
 * This project includes software developed by Julien Eluard: https://github.com/jeluard/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.osjava.jardiff;

import org.junit.Test;
import org.objectweb.asm.Opcodes;
import static org.junit.Assert.*;

public class ToolsTest {

    static class lala {
        int lala;
    }

	@Test
	public void isClassAccessChange() {
		// A class can't become final.
		assertTrue(Tools.isClassAccessChange(0, Opcodes.ACC_FINAL));
		assertTrue(Tools.isClassAccessChange(0, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));
		// ... but can become non-final.
		assertFalse(Tools.isClassAccessChange(Opcodes.ACC_FINAL, 0));
		assertFalse(Tools.isClassAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

		// No matter the final access, can't become protected or private or
		// package if it was public.
		assertTrue(Tools.isClassAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, 0));
		assertTrue(Tools.isClassAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED));
		// A class can become concrete.
		assertFalse(Tools.isClassAccessChange(Opcodes.ACC_ABSTRACT, 0));
		assertFalse(Tools.isClassAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));
		assertFalse(Tools.isClassAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED));
		// ...but can't become abstract
		assertTrue(Tools.isClassAccessChange(0, Opcodes.ACC_ABSTRACT));
		assertTrue(Tools.isClassAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT));
		assertTrue(Tools.isClassAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED + Opcodes.ACC_ABSTRACT));
	}

    @Test
    public void isFieldAccessChange() {
        // A field can't become final.
        assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_FINAL));
        assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));
        // ... but can become non-final.
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_FINAL, 0));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

        // A field can become more accessible
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isFieldAccessChange(0, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isFieldAccessChange(0, Opcodes.ACC_PROTECTED));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PROTECTED));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, 0));
        // ...but can't become less accessible
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, 0));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PRIVATE));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, 0));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PRIVATE));
        assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_PRIVATE));

        // A field can't change static
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_STATIC, 0));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

        // A field can't change volatile
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_VOLATILE, 0));
        assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_VOLATILE + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

        // A field can change transient
        assertFalse(Tools.isFieldAccessChange(0,
                                              Opcodes.ACC_TRANSIENT));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC,
                                              Opcodes.ACC_PUBLIC + Opcodes.ACC_TRANSIENT));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_TRANSIENT,
                                              0));
        assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_TRANSIENT,
                                              Opcodes.ACC_PUBLIC));
    }

    @Test
    public void isMethodAccessChange() {
        // A non-static method can't become final.
        assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_FINAL));
        assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));
        // ... but can become non-final.
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_FINAL, 0));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));
        // ... but a static method can become final!
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_STATIC,
                                               Opcodes.ACC_STATIC + Opcodes.ACC_FINAL));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC,
                                               Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));

        // A method can become more accessible
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_PROTECTED));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PROTECTED));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, 0));
        // ...but can't become less accessible
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, 0));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PRIVATE));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, 0));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PRIVATE));
        assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_PRIVATE));

        // A class or method can become concrete.
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT, 0));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED));
        // ...but can't become abstract
        assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_ABSTRACT));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED + Opcodes.ACC_ABSTRACT));

        // A method can't change static
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_STATIC, 0));
        assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

        // A method can change synchronized
        assertFalse(Tools.isMethodAccessChange(0,
                                               Opcodes.ACC_SYNCHRONIZED));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC,
                                               Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_SYNCHRONIZED,
                                               0));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED,
                                               Opcodes.ACC_PUBLIC));

        // A method can change native
        assertFalse(Tools.isMethodAccessChange(0,
                                               Opcodes.ACC_NATIVE));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC,
                                               Opcodes.ACC_PUBLIC + Opcodes.ACC_NATIVE));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_NATIVE,
                                               0));
        assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_NATIVE,
                                               Opcodes.ACC_PUBLIC));
    }

}