summaryrefslogtreecommitdiff
path: root/src/test/java/cuchaz/enigma/TestJarIndexConstructorReferences.java
blob: edb859a7d6b9f41b3e43de0ee5229e65c8f9433f (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
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/

package cuchaz.enigma;

import cuchaz.enigma.analysis.EntryReference;
import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.ClassEntry;
import org.junit.Test;

import java.io.File;
import java.util.Collection;
import java.util.jar.JarFile;

import static cuchaz.enigma.TestEntryFactory.newBehaviorReferenceByConstructor;
import static cuchaz.enigma.TestEntryFactory.newBehaviorReferenceByMethod;
import static cuchaz.enigma.TestEntryFactory.newClass;
import static cuchaz.enigma.TestEntryFactory.newConstructor;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;

public class TestJarIndexConstructorReferences {

	private JarIndex index;

	private ClassEntry baseClass = newClass("a");
	private ClassEntry subClass = newClass("d");
	private ClassEntry subsubClass = newClass("e");
	private ClassEntry defaultClass = newClass("c");
	private ClassEntry callerClass = newClass("b");

	public TestJarIndexConstructorReferences()
		throws Exception {
		File jarFile = new File("build/test-obf/constructors.jar");
		index = new JarIndex();
		index.indexJar(new JarFile(jarFile), false);
	}

	@Test
	public void obfEntries() {
		assertThat(index.getObfClassEntries(), containsInAnyOrder(newClass("cuchaz/enigma/inputs/Keep"), baseClass,
			subClass, subsubClass, defaultClass, callerClass));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void baseDefault() {
		BehaviorEntry source = newConstructor(baseClass, "()V");
		Collection<EntryReference<BehaviorEntry, BehaviorEntry>> references = index.getBehaviorReferences(source);
		assertThat(references, containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "a", "()V"),
			newBehaviorReferenceByConstructor(source, subClass.getName(), "()V"),
			newBehaviorReferenceByConstructor(source, subClass.getName(), "(III)V")
		));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void baseInt() {
		BehaviorEntry source = newConstructor(baseClass, "(I)V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "b", "()V")
		));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void subDefault() {
		BehaviorEntry source = newConstructor(subClass, "()V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "c", "()V"),
			newBehaviorReferenceByConstructor(source, subClass.getName(), "(I)V")
		));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void subInt() {
		BehaviorEntry source = newConstructor(subClass, "(I)V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "d", "()V"),
			newBehaviorReferenceByConstructor(source, subClass.getName(), "(II)V"),
			newBehaviorReferenceByConstructor(source, subsubClass.getName(), "(I)V")
		));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void subIntInt() {
		BehaviorEntry source = newConstructor(subClass, "(II)V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "e", "()V")
		));
	}

	@Test
	public void subIntIntInt() {
		BehaviorEntry source = newConstructor(subClass, "(III)V");
		assertThat(index.getBehaviorReferences(source), is(empty()));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void subsubInt() {
		BehaviorEntry source = newConstructor(subsubClass, "(I)V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "f", "()V")
		));
	}

	@Test
	@SuppressWarnings("unchecked")
	public void defaultConstructable() {
		BehaviorEntry source = newConstructor(defaultClass, "()V");
		assertThat(index.getBehaviorReferences(source), containsInAnyOrder(
			newBehaviorReferenceByMethod(source, callerClass.getName(), "g", "()V")
		));
	}
}