Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71b0263bacca45067107543200566eb78ed6dc42 (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
/*******************************************************************************
 * Copyright (c) 2012 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.tests.internal.resource.java;

import java.io.File;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.jpt.common.core.internal.resource.java.binary.BinaryTypeCache;
import org.eclipse.jpt.common.core.resource.java.AnnotationDefinition;
import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
import org.eclipse.jpt.common.core.resource.java.JavaResourceMethod;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.resource.java.NestableAnnotationDefinition;

@SuppressWarnings("nls")
public class BinaryTypeTests
		extends JavaResourceModelTestCase {
	
	private static URL JAR_URL = BinaryTypeTests.class.getResource("binmodel.jar");
	
	private static String SUPERCLASS_CLASS_NAME = BinaryTypeTests.class.getPackage().getName() + ".binmodel.GenericSuperclass";
	
	private static String SUBCLASS_CLASS_NAME = BinaryTypeTests.class.getPackage().getName() + ".binmodel.GenericSubclass";
	
	
	public BinaryTypeTests(String name) {
		super(name);
	}
	
	
	@Override
	protected AnnotationDefinition[] annotationDefinitions() {
		return new AnnotationDefinition[] {
				DeprecatedAnnotationDefinition.instance() };
	}
	
	@Override
	protected NestableAnnotationDefinition[] nestableAnnotationDefinitions() {
		return new NestableAnnotationDefinition[0];
	}
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		getJavaProjectTestHarness().addJar(new File(FileLocator.resolve(JAR_URL).toURI()).getAbsolutePath());
	}
	
	public void testInheritedAttributeTypes() throws Exception {
		BinaryTypeCache typeCache = new BinaryTypeCache(buildAnnotationProvider());
		JavaResourceType superclassType = 
				(JavaResourceType) typeCache.addType(this.getJavaProject().findType(SUPERCLASS_CLASS_NAME));
		JavaResourceType subclassType = 
				(JavaResourceType) typeCache.addType(this.getJavaProject().findType(SUBCLASS_CLASS_NAME));
		
		// generic field 1 -> Object, String
		JavaResourceField genericField1 = superclassType.getField("genericField1");
		assertEquals("java.lang.Object", superclassType.getAttributeTypeBinding(genericField1).getQualifiedName());
		assertEquals("java.lang.String", subclassType.getAttributeTypeBinding(genericField1).getQualifiedName());
		
		// generic field 2 -> Object, Number
		JavaResourceField genericField2 = superclassType.getField("genericField2");
		assertEquals("java.lang.Object", superclassType.getAttributeTypeBinding(genericField2).getQualifiedName());
		assertEquals("java.lang.Number", subclassType.getAttributeTypeBinding(genericField2).getQualifiedName());
		
		// generic method 1 -> Object, String
		JavaResourceMethod genericMethod1 = superclassType.getMethod("genericMethod1");
		assertEquals("java.lang.Object", superclassType.getAttributeTypeBinding(genericMethod1).getQualifiedName());
		assertEquals("java.lang.String", subclassType.getAttributeTypeBinding(genericMethod1).getQualifiedName());
		
		// generic method 1 -> Object, Number
		JavaResourceMethod genericMethod2 = superclassType.getMethod("genericMethod2");
		assertEquals("java.lang.Object", superclassType.getAttributeTypeBinding(genericMethod2).getQualifiedName());
		assertEquals("java.lang.Number", subclassType.getAttributeTypeBinding(genericMethod2).getQualifiedName());
	}
	
	public void testGeneralTypeAPI() throws Exception {
		BinaryTypeCache typeCache = new BinaryTypeCache(buildAnnotationProvider());
		JavaResourceType superclassType = 
				(JavaResourceType) typeCache.addType(this.getJavaProject().findType(SUPERCLASS_CLASS_NAME));
		JavaResourceType subclassType = 
				(JavaResourceType) typeCache.addType(this.getJavaProject().findType(SUBCLASS_CLASS_NAME));
		
		// superclass qualified name
		assertEquals("java.lang.Object", superclassType.getSuperclassQualifiedName());
		assertEquals(SUPERCLASS_CLASS_NAME, subclassType.getSuperclassQualifiedName());
		
		// has any annotated fields/methods
		assertFalse(superclassType.hasAnyAnnotatedFields());
		assertFalse(superclassType.hasAnyAnnotatedMethods());
		assertTrue(subclassType.hasAnyAnnotatedFields());
		assertTrue(subclassType.hasAnyAnnotatedMethods());
		
		// has equals/hashcode method
		assertFalse(superclassType.hasEqualsMethod());
		assertFalse(superclassType.hasHashCodeMethod());
		assertTrue(subclassType.hasEqualsMethod());
		assertTrue(subclassType.hasHashCodeMethod());
		
		// has xxx no arg constructor
		assertTrue(superclassType.hasNoArgConstructor());
		assertFalse(superclassType.hasPrivateNoArgConstructor());
		assertTrue(superclassType.hasPublicNoArgConstructor());
		assertTrue(superclassType.hasPublicOrProtectedNoArgConstructor());
		assertTrue(subclassType.hasNoArgConstructor());
		assertTrue(subclassType.hasPrivateNoArgConstructor());
		assertFalse(subclassType.hasPublicNoArgConstructor());
		assertFalse(subclassType.hasPublicOrProtectedNoArgConstructor());
		
		// is abstract
		assertTrue(superclassType.isAbstract());
		assertFalse(subclassType.isAbstract());
	}
}

Back to the top