Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2011 IBM Corporation and others. |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Technical University Berlin - adapted for Object Teams |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.jdt.core.tests.dom; |
| 14 | |
| 15 | import java.lang.reflect.Method; |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.HashSet; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import junit.framework.Test; |
| 23 | |
| 24 | import org.eclipse.jdt.core.dom.*; |
| 25 | |
| 26 | public class ASTStructuralPropertyTest extends org.eclipse.jdt.core.tests.junit.extension.TestCase { |
| 27 | |
| 28 | /** @deprecated using deprecated code */ |
| 29 | public static Test suite() { |
| 30 | // TODO (frederic) use buildList + setAstLevel(init) instead... |
| 31 | junit.framework.TestSuite suite = new junit.framework.TestSuite(ASTStructuralPropertyTest.class.getName()); |
| 32 | |
| 33 | Class c = ASTStructuralPropertyTest.class; |
| 34 | Method[] methods = c.getMethods(); |
| 35 | for (int i = 0, max = methods.length; i < max; i++) { |
| 36 | if (methods[i].getName().startsWith("test")) { //$NON-NLS-1$ |
| 37 | suite.addTest(new ASTStructuralPropertyTest(methods[i].getName(), AST.JLS2)); |
| 38 | suite.addTest(new ASTStructuralPropertyTest(methods[i].getName(), AST.JLS3)); |
| 39 | } |
| 40 | } |
| 41 | return suite; |
| 42 | } |
| 43 | |
| 44 | AST ast; |
| 45 | ASTParser parser; |
| 46 | int API_LEVEL; |
| 47 | |
| 48 | public ASTStructuralPropertyTest(String name, int apiLevel) { |
| 49 | super(name); |
| 50 | this.API_LEVEL = apiLevel; |
| 51 | } |
| 52 | |
| 53 | protected void setUp() throws Exception { |
| 54 | super.setUp(); |
| 55 | this.ast = AST.newAST(this.API_LEVEL); |
| 56 | this.parser = ASTParser.newParser(this.API_LEVEL); |
| 57 | } |
| 58 | |
| 59 | protected void tearDown() throws Exception { |
| 60 | this.ast = null; |
| 61 | super.tearDown(); |
| 62 | } |
| 63 | |
| 64 | /** @deprecated using deprecated code */ |
| 65 | public String getName() { |
| 66 | String name = super.getName(); |
| 67 | switch (this.API_LEVEL) { |
| 68 | case AST.JLS2: |
| 69 | name = "JLS2 - " + name; |
| 70 | break; |
| 71 | case AST.JLS3: |
| 72 | name = "JLS3 - " + name; |
| 73 | break; |
| 74 | } |
| 75 | return name; |
| 76 | } |
| 77 | |
| 78 | public void testLocationInParent() { |
| 79 | final ASTNode root = SampleASTs.oneOfEach(this.ast); |
| 80 | ASTVisitor v = new ASTVisitor(true) { |
| 81 | public void postVisit(ASTNode node) { |
| 82 | StructuralPropertyDescriptor me = node.getLocationInParent(); |
| 83 | assertTrue(me != null || (node == root)); |
| 84 | ASTNode p = node.getParent(); |
| 85 | if (p != null) { |
| 86 | List parentProperties = p.structuralPropertiesForType(); |
| 87 | boolean foundMe = false; |
| 88 | for (Iterator it = parentProperties.iterator(); it |
| 89 | .hasNext();) { |
| 90 | StructuralPropertyDescriptor prop = |
| 91 | (StructuralPropertyDescriptor) it.next(); |
| 92 | if (me == prop || prop.getId().equals(me.getId())) { |
| 93 | foundMe = true; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | assertTrue(foundMe); |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | root.accept(v); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @deprecated since using deprecated constant |
| 106 | */ |
| 107 | public void testStructuralProperties() { |
| 108 | final ASTNode root = SampleASTs.oneOfEach(this.ast); |
| 109 | |
| 110 | final Set simpleProperties = new HashSet(400); |
| 111 | final Set childProperties = new HashSet(400); |
| 112 | final Set childListProperties = new HashSet(400); |
| 113 | final Set visitedProperties = new HashSet(400); |
| 114 | final Set nodeClasses = new HashSet(100); |
| 115 | |
| 116 | ASTVisitor v = new ASTVisitor(true) { |
| 117 | public void postVisit(ASTNode node) { |
| 118 | StructuralPropertyDescriptor me = node.getLocationInParent(); |
| 119 | if (me != null) { |
| 120 | visitedProperties.add(me); |
| 121 | } |
| 122 | visitedProperties.add(me); |
| 123 | nodeClasses.add(node.getClass()); |
| 124 | List ps = node.structuralPropertiesForType(); |
| 125 | for (Iterator it = ps.iterator(); it.hasNext(); ) { |
| 126 | StructuralPropertyDescriptor p = (StructuralPropertyDescriptor) it.next(); |
| 127 | Object o = node.getStructuralProperty(p); |
| 128 | if (p.isSimpleProperty()) { |
| 129 | simpleProperties.add(p); |
| 130 | // slam simple properties |
| 131 | node.setStructuralProperty(p, o); |
| 132 | } else if (p.isChildProperty()) { |
| 133 | childProperties.add(p); |
| 134 | // replace child with a copy |
| 135 | ASTNode copy = ASTNode.copySubtree(ASTStructuralPropertyTest.this.ast, (ASTNode) o); |
| 136 | node.setStructuralProperty(p, copy); |
| 137 | } else if (p.isChildListProperty()) { |
| 138 | childListProperties.add(p); |
| 139 | // replace child list with copies |
| 140 | List list = (List) o; |
| 141 | List copy = ASTNode.copySubtrees(ASTStructuralPropertyTest.this.ast, list); |
| 142 | list.clear(); |
| 143 | list.addAll(copy); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | }; |
| 148 | root.accept(v); |
| 149 | switch(this.API_LEVEL) { |
| 150 | case AST.JLS2 : |
| 151 | assertEquals("Wrong number of visited node classes", 67, nodeClasses.size()); |
| 152 | assertEquals("Wrong number of visited properties", 82, visitedProperties.size()); |
| 153 | //{ObjectTeams: 2 new simple properties in TypeDeclaration: TEAM_PROPERTY and ROLE_PROPERTY: |
| 154 | /* orig: |
| 155 | assertEquals("Wrong number of simple properties", 26, simpleProperties.size()); |
| 156 | :giro */ |
| 157 | assertEquals("Wrong number of simple properties", 28, simpleProperties.size()); |
| 158 | // 2 new child properties: GUARD_PROPERTY (in TypeDeclaration and MethodDeclaration) |
| 159 | /* orig: |
| 160 | assertEquals("Wrong number of child properties", 90, childProperties.size()); |
| 161 | :giro */ |
| 162 | assertEquals("Wrong number of child properties", 92, childProperties.size()); |
| 163 | |
| 164 | // 1 new child list property: PRECEDENCES_PROPERTY |
| 165 | /* orig: |
| 166 | assertEquals("Wrong number of child list properties", 26, childListProperties.size()); |
| 167 | :giro */ |
| 168 | assertEquals("Wrong number of child list properties", 27, childListProperties.size()); |
| 169 | // SH} |
| 170 | break; |
| 171 | case AST.JLS3 : |
| 172 | assertEquals("Wrong number of visited node classes", 80, nodeClasses.size()); |
| 173 | assertEquals("Wrong number of visited properties", 104, visitedProperties.size()); |
| 174 | //{ObjectTeams: 2 new simple properties in TypeDeclaration: TEAM_PROPERTY and ROLE_PROPERTY: |
| 175 | // 1 new property in ImportDeclaration: BASE_PROPERTY |
| 176 | /* orig: |
| 177 | assertEquals("Wrong number of simple properties", 23, simpleProperties.size()); |
| 178 | :giro */ |
| 179 | assertEquals("Wrong number of simple properties", 26, simpleProperties.size()); |
| 180 | // 2 new child properties: GUARD_PROPERTY (in TypeDeclaration and MethodDeclaration) |
| 181 | /* orig: |
| 182 | assertEquals("Wrong number of child properties", 115, childProperties.size()); |
| 183 | :giro */ |
| 184 | assertEquals("Wrong number of child properties", 117, childProperties.size()); |
| 185 | |
| 186 | // 1 new child list property: PRECEDENCES_PROPERTY |
| 187 | /* orig: |
| 188 | assertEquals("Wrong number of child list properties", 52, childListProperties.size()); |
| 189 | :giro */ |
| 190 | assertEquals("Wrong number of child list properties", 53, childListProperties.size()); |
| 191 | // SH} |
| 192 | } |
| 193 | // visit should rebuild tree |
| 194 | ASTNode newRoot = SampleASTs.oneOfEach(this.ast); |
| 195 | assertTrue(root.subtreeMatch(new ASTMatcher(), newRoot)); |
| 196 | } |
| 197 | |
| 198 | public void testProtect() { |
| 199 | final ASTNode root = SampleASTs.oneOfEach(this.ast); |
| 200 | |
| 201 | // check that all properties are again modifiable |
| 202 | class Slammer extends ASTVisitor { |
| 203 | boolean shouldBeProtected; |
| 204 | Slammer(boolean shouldBeProtected){ |
| 205 | super(true); // visit doc |
| 206 | this.shouldBeProtected = shouldBeProtected; |
| 207 | } |
| 208 | public void postVisit(ASTNode node) { |
| 209 | try { |
| 210 | node.setSourceRange(1, 1); |
| 211 | assertTrue(!this.shouldBeProtected); |
| 212 | } catch (RuntimeException e) { |
| 213 | assertTrue(this.shouldBeProtected); |
| 214 | } |
| 215 | List ps = node.structuralPropertiesForType(); |
| 216 | for (Iterator it = ps.iterator(); it.hasNext(); ) { |
| 217 | StructuralPropertyDescriptor p = (StructuralPropertyDescriptor) it.next(); |
| 218 | Object o = node.getStructuralProperty(p); |
| 219 | if (p.isSimpleProperty()) { |
| 220 | // slam simple properties |
| 221 | try { |
| 222 | node.setStructuralProperty(p, o); |
| 223 | assertTrue(!this.shouldBeProtected); |
| 224 | } catch (RuntimeException e) { |
| 225 | assertTrue(this.shouldBeProtected); |
| 226 | } |
| 227 | } else if (p.isChildProperty()) { |
| 228 | // replace child with a copy |
| 229 | ASTNode copy = ASTNode.copySubtree(ASTStructuralPropertyTest.this.ast, (ASTNode) o); |
| 230 | try { |
| 231 | node.setStructuralProperty(p, copy); |
| 232 | assertTrue(!this.shouldBeProtected); |
| 233 | } catch (RuntimeException e) { |
| 234 | assertTrue(this.shouldBeProtected); |
| 235 | } |
| 236 | } else if (p.isChildListProperty()) { |
| 237 | // replace child list with copies |
| 238 | List list = (List) o; |
| 239 | List copy = ASTNode.copySubtrees(ASTStructuralPropertyTest.this.ast, list); |
| 240 | if (!list.isEmpty()) { |
| 241 | try { |
| 242 | list.clear(); |
| 243 | assertTrue(!this.shouldBeProtected); |
| 244 | } catch (RuntimeException e) { |
| 245 | assertTrue(this.shouldBeProtected); |
| 246 | } |
| 247 | try { |
| 248 | list.addAll(copy); |
| 249 | assertTrue(!this.shouldBeProtected); |
| 250 | } catch (RuntimeException e) { |
| 251 | assertTrue(this.shouldBeProtected); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | class Protector extends ASTVisitor { |
| 260 | boolean shouldBeProtected; |
| 261 | Protector(boolean shouldBeProtected){ |
| 262 | super(true); // visit doc |
| 263 | this.shouldBeProtected = shouldBeProtected; |
| 264 | } |
| 265 | public void preVisit(ASTNode node) { |
| 266 | int f = node.getFlags(); |
| 267 | if (this.shouldBeProtected) { |
| 268 | f |= ASTNode.PROTECT; |
| 269 | } else { |
| 270 | f &= ~ASTNode.PROTECT; |
| 271 | } |
| 272 | node.setFlags(f); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
| 277 | // mark all nodes as protected |
| 278 | root.accept(new Protector(true)); |
| 279 | root.accept(new Slammer(true)); |
| 280 | |
| 281 | // mark all nodes as unprotected |
| 282 | root.accept(new Protector(false)); |
| 283 | root.accept(new Slammer(false)); |
| 284 | } |
| 285 | |
| 286 | public void testDelete() { |
| 287 | final ASTNode root = SampleASTs.oneOfEach(this.ast); |
| 288 | |
| 289 | // check that nodes can be deleted unless mandatory |
| 290 | root.accept(new ASTVisitor(true) { |
| 291 | public void postVisit(ASTNode node) { |
| 292 | List ps = node.structuralPropertiesForType(); |
| 293 | for (Iterator it = ps.iterator(); it.hasNext(); ) { |
| 294 | StructuralPropertyDescriptor p = (StructuralPropertyDescriptor) it.next(); |
| 295 | if (p.isChildProperty()) { |
| 296 | ChildPropertyDescriptor c = (ChildPropertyDescriptor) p; |
| 297 | ASTNode child = (ASTNode) node.getStructuralProperty(c); |
| 298 | if (!c.isMandatory() && child != null) { |
| 299 | try { |
| 300 | child.delete(); |
| 301 | assertTrue(node.getStructuralProperty(c) == null); |
| 302 | } catch (RuntimeException e) { |
| 303 | assertTrue(false); |
| 304 | } |
| 305 | } |
| 306 | } else if (p.isChildListProperty()) { |
| 307 | // replace child list with copies |
| 308 | List list = (List) node.getStructuralProperty(p); |
| 309 | // iterate over a copy and try removing all members |
| 310 | List copy = new ArrayList(); |
| 311 | copy.addAll(list); |
| 312 | for (Iterator it2 = copy.iterator(); it2.hasNext(); ) { |
| 313 | ASTNode n = (ASTNode) it2.next(); |
| 314 | try { |
| 315 | n.delete(); |
| 316 | assertTrue(!list.contains(n)); |
| 317 | } catch (RuntimeException e) { |
| 318 | assertTrue(false); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | }); |
| 325 | } |
| 326 | |
| 327 | /** @deprecated using deprecated code */ |
| 328 | public void testCreateInstance() { |
| 329 | for (int nodeType = 0; nodeType < 100; nodeType++) { |
| 330 | Class nodeClass = null; |
| 331 | try { |
| 332 | nodeClass = ASTNode.nodeClassForType(nodeType); |
Stephan Herrmann | 9242e59 | 2011-09-17 19:42:31 +0000 | [diff] [blame] | 333 | } catch (IllegalArgumentException e) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 334 | // oops - guess that's not valid |
| 335 | } |
| 336 | if (nodeClass != null) { |
| 337 | try { |
| 338 | ASTNode node = this.ast.createInstance(nodeClass); |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 339 | //{ObjectTeams: adpated for new OT-specific ASTNodes |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 340 | if ((nodeType >= 85) && (nodeType <= 100)) { |
| 341 | // good: OT node |
| 342 | } else |
| 343 | // SH} |
| 344 | if (this.ast.apiLevel() == AST.JLS2) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 345 | assertTrue((nodeType >= 1) && (nodeType <= 69)); |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 346 | } else if (this.ast.apiLevel() == AST.JLS3) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 347 | assertTrue((nodeType >= 1) && (nodeType <= 83)); |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 348 | } else if (this.ast.apiLevel() == AST.JLS4) { |
| 349 | assertTrue((nodeType >= 1) && (nodeType <= 84)); |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 350 | } |
| 351 | assertTrue(node.getNodeType() == nodeType); |
| 352 | //ASTNode node2 = ast.createInstance(nodeType); |
| 353 | //assertTrue(node2.getNodeType() == nodeType); |
| 354 | } catch (RuntimeException e) { |
| 355 | if (this.ast.apiLevel() == AST.JLS2) { |
| 356 | assertTrue((nodeType < 1) || (nodeType > 69)); |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 357 | } else if (this.ast.apiLevel() == AST.JLS3) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 358 | assertTrue((nodeType < 1) || (nodeType > 83)); |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 359 | } else if (this.ast.apiLevel() == AST.JLS4) { |
| 360 | assertTrue((nodeType < 1) || (nodeType > 84)); |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | public void testNodeClassForType() { |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 368 | //{ObjectTeams: larger range: |
| 369 | /* orig: |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 370 | Set classes = new HashSet(100); |
| 371 | // make sure node types are contiguous starting at 0 |
| 372 | int hi = 0; |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 373 | for (int nodeType = 1; nodeType < 100; nodeType++) { |
| 374 | :giro */ |
| 375 | Set classes = new HashSet(120); |
| 376 | // make sure node types are contiguous starting at 0 |
| 377 | int hi = 0; |
| 378 | for (int nodeType = 1; nodeType < 120; nodeType++) { |
| 379 | // SH} |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 380 | try { |
| 381 | Class nodeClass = ASTNode.nodeClassForType(nodeType); |
| 382 | assertTrue(ASTNode.class.isAssignableFrom(nodeClass)); |
| 383 | classes.add(nodeClass); |
| 384 | if (nodeType > 1) { |
| 385 | assertTrue(hi == nodeType - 1); |
| 386 | } |
| 387 | hi = nodeType; |
Stephan Herrmann | 9242e59 | 2011-09-17 19:42:31 +0000 | [diff] [blame] | 388 | } catch (IllegalArgumentException e) { |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 389 | // oops - guess that's not valid |
| 390 | } |
| 391 | } |
| 392 | // {ObjectTeams: adapted for OT specific ASTNodes |
| 393 | /* orig: |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 394 | assertTrue(hi == 84); // last known one |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 395 | :giro */ |
Stephan Herrmann | 4683caf | 2011-08-05 06:54:51 +0000 | [diff] [blame] | 396 | assertTrue(hi == 100); // last known one |
Stephan Herrmann | be6c0ea | 2010-04-01 21:59:30 +0000 | [diff] [blame] | 397 | // jwl} |
| 398 | assertTrue(classes.size() == hi); // all classes are distinct |
| 399 | } |
| 400 | } |