1
/* *******************************************************************
2
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
4
* This program and the accompanying materials are made available
5
* under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
10
* Xerox/PARC initial implementation
11
* AMC 21.01.2003 fixed for new source location in eclipse.org
12
* ******************************************************************/
14
package org.aspectj.ajde;
17
import java.util.Iterator;
19
import junit.framework.TestSuite;
21
import org.aspectj.asm.*;
26
public class StructureModelTest extends AjdeTestCase {
29
private final String CONFIG_FILE_PATH = "../examples/figures-coverage/all.lst";
31
public StructureModelTest(String name) {
35
public static void main(String[] args) {
36
junit.swingui.TestRunner.run(StructureModelTest.class);
39
public static TestSuite suite() {
40
TestSuite result = new TestSuite();
41
result.addTestSuite(StructureModelTest.class);
45
// XXX this should work
46
// public void testFieldInitializerCorrespondence() throws IOException {
47
// File testFile = createFile("testdata/examples/figures-coverage/figures/Figure.java");
48
// IProgramElement node = Ajde.getDefault().getStructureModelManager().getStructureModel().findNodeForSourceLine(
49
// testFile.getCanonicalPath(), 28);
50
// assertTrue("find result", node != null) ;
51
// ProgramElementNode pNode = (ProgramElementNode)node;
52
// ProgramElementNode foundNode = null;
53
// final List list = pNode.getRelations();
54
// assertNotNull("pNode.getRelations()", list);
55
// for (Iterator it = list.iterator(); it.hasNext(); ) {
56
// RelationNode relation = (RelationNode)it.next();
58
// if (relation.getRelation().equals(AdviceAssociation.FIELD_ACCESS_RELATION)) {
59
// for (Iterator it2 = relation.getChildren().iterator(); it2.hasNext(); ) {
60
// LinkNode linkNode = (LinkNode)it2.next();
61
// if (linkNode.getProgramElementNode().getName().equals("this.currVal = 0")) {
62
// foundNode = linkNode.getProgramElementNode();
68
// assertTrue("find associated node", foundNode != null) ;
70
// File pointFile = createFile("testdata/examples/figures-coverage/figures/primitives/planar/Point.java");
71
// IProgramElement fieldNode = Ajde.getDefault().getStructureModelManager().getStructureModel().findNodeForSourceLine(
72
// pointFile.getCanonicalPath(), 12);
73
// assertTrue("find result", fieldNode != null);
75
// assertTrue("matches", foundNode.getParent() == fieldNode.getParent());
79
public void testRootForSourceFile() throws IOException {
80
File testFile = openFile("figures-coverage/figures/Figure.java");
81
IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceFile(
82
testFile.getAbsolutePath());
83
assertTrue("find result", node != null) ;
84
IProgramElement pNode = (IProgramElement)node;
85
String child = ((IProgramElement)pNode.getChildren().get(1)).getName();
86
assertTrue("expected Figure got child " + child, child.equals("Figure"));
89
public void testPointcutName() throws IOException {
90
File testFile = openFile("figures-coverage/figures/Main.java");
91
IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceFile(
92
testFile.getAbsolutePath());
93
assertTrue("find result", node != null) ;
94
IProgramElement pNode = (IProgramElement)((IProgramElement)node).getChildren().get(2);
95
IProgramElement pointcut = (IProgramElement)pNode.getChildren().get(0);
96
assertTrue("kind", pointcut.getKind().equals(IProgramElement.Kind.POINTCUT));
97
assertTrue("found node: " + pointcut.getName(), pointcut.toLabelString().equals("testptct()"));
100
public void testFileNodeFind() throws IOException {
101
File testFile = openFile("figures-coverage/figures/Main.java");
103
// System.err.println(((IProgramElement)((IProgramElement)Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().getChildren().get(0)).getChildren().get(3)).getSourceLocation().getSourceFile().getAbsolutePath());
104
// System.err.println(testFile.getAbsolutePath());
106
IProgramElement node = Ajde.getDefault().getStructureModelManager().getHierarchy().findElementForSourceLine(
107
testFile.getAbsolutePath(), 1);
108
assertTrue("find result", node != null) ;
109
assertEquals("find result has children", 3, node.getChildren().size()) ;
110
IProgramElement pNode = (IProgramElement)node;
111
assertTrue("found node: " + pNode.getName(), pNode.getKind().equals(IProgramElement.Kind.FILE_JAVA));
115
* @todo add negative test to make sure things that aren't runnable aren't annotated
117
public void testMainClassNodeInfo() throws IOException {
118
IHierarchy model = Ajde.getDefault().getStructureModelManager().getHierarchy();
119
assertTrue("model exists", model != null);
120
assertTrue("root exists", model.getRoot() != null);
121
File testFile = openFile("figures-coverage/figures/Main.java");
122
IProgramElement node = model.findElementForSourceLine(testFile.getAbsolutePath(), 11);
123
assertTrue("find result", node != null);
124
IProgramElement pNode = (IProgramElement)((IProgramElement)node).getParent();
126
assertTrue("null parent of " + node, false);
128
assertTrue("found node: " + pNode.getName(), pNode.isRunnable());
132
* Integrity could be checked somewhere in the API.
134
public void testModelIntegrity() {
135
IProgramElement modelRoot = Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot();
136
assertTrue("root exists", modelRoot != null);
139
testModelIntegrityHelper(modelRoot);
140
} catch (Exception e) {
141
assertTrue(e.toString(), false);
145
private void testModelIntegrityHelper(IProgramElement node) throws Exception {
146
for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
147
IProgramElement child = (IProgramElement)it.next();
148
if (node == child.getParent()) {
149
testModelIntegrityHelper(child);
151
throw new Exception("parent-child check failed for child: " + child.toString());
156
public void testNoChildIsNull() {
157
HierarchyWalker walker = new HierarchyWalker() {
158
public void preProcess(IProgramElement node) {
159
if (node.getChildren() == null) return;
160
for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
161
if (it.next() == null) throw new NullPointerException("null child on node: " + node.getName());
165
Ajde.getDefault().getStructureModelManager().getHierarchy().getRoot().walk(walker);
168
protected void setUp() throws Exception {
169
super.setUp("examples");
170
doSynchronousBuild(CONFIG_FILE_PATH);
173
protected void tearDown() throws Exception {