|
Added
Link Here
|
| 1 |
/** |
| 2 |
* <copyright> |
| 3 |
* |
| 4 |
* Copyright (c) 2008 Zeligsoft Inc. and others. |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Zeligsoft - Initial API and implementation |
| 12 |
* |
| 13 |
* </copyright> |
| 14 |
* |
| 15 |
* $Id$ |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.eclipse.ocl.ecore.tests; |
| 19 |
|
| 20 |
import java.util.Arrays; |
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
import junit.framework.Test; |
| 24 |
import junit.framework.TestSuite; |
| 25 |
|
| 26 |
import org.eclipse.emf.common.util.BasicEList; |
| 27 |
import org.eclipse.emf.common.util.EList; |
| 28 |
import org.eclipse.emf.common.util.Enumerator; |
| 29 |
import org.eclipse.emf.common.util.URI; |
| 30 |
import org.eclipse.emf.ecore.EAttribute; |
| 31 |
import org.eclipse.emf.ecore.EClass; |
| 32 |
import org.eclipse.emf.ecore.EEnum; |
| 33 |
import org.eclipse.emf.ecore.EFactory; |
| 34 |
import org.eclipse.emf.ecore.EObject; |
| 35 |
import org.eclipse.emf.ecore.EPackage; |
| 36 |
import org.eclipse.emf.ecore.EReference; |
| 37 |
import org.eclipse.emf.ecore.EStructuralFeature; |
| 38 |
import org.eclipse.emf.ecore.resource.Resource; |
| 39 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 40 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 41 |
import org.eclipse.ocl.ecore.dynamic.OCLSettingDelegateFactory; |
| 42 |
|
| 43 |
/** |
| 44 |
* Tests for the OCL setting delegate implementation. |
| 45 |
* |
| 46 |
* @author Christian W. Damus (cdamus) |
| 47 |
*/ |
| 48 |
@SuppressWarnings("nls") |
| 49 |
public class OCLSettingDelegateTest |
| 50 |
extends AbstractTestSuite { |
| 51 |
|
| 52 |
private ResourceSet rset; |
| 53 |
|
| 54 |
private Resource testResource; |
| 55 |
|
| 56 |
private EPackage companyPackage; |
| 57 |
|
| 58 |
private EFactory companyFactory; |
| 59 |
|
| 60 |
private EClass companyClass; |
| 61 |
|
| 62 |
private EAttribute companyName; |
| 63 |
|
| 64 |
private EReference companyEmployees; |
| 65 |
|
| 66 |
private EAttribute companySize; |
| 67 |
|
| 68 |
private EClass employeeClass; |
| 69 |
|
| 70 |
private EAttribute employeeName; |
| 71 |
|
| 72 |
private EReference employeeManager; |
| 73 |
|
| 74 |
private EReference employeeDirectReports; |
| 75 |
|
| 76 |
private EReference employeeAllReports; |
| 77 |
|
| 78 |
private EEnum sizeKind; |
| 79 |
|
| 80 |
private Enumerator sizeSmall; |
| 81 |
|
| 82 |
private Enumerator sizeMedium; |
| 83 |
|
| 84 |
private Enumerator sizeLarge; |
| 85 |
|
| 86 |
private EObject acme; |
| 87 |
|
| 88 |
private Map<String, EObject> employees; |
| 89 |
|
| 90 |
public OCLSettingDelegateTest(String name) { |
| 91 |
super(name); |
| 92 |
} |
| 93 |
|
| 94 |
public static Test suite() { |
| 95 |
return new TestSuite(OCLSettingDelegateTest.class, |
| 96 |
"OCL Setting Delegate Tests"); |
| 97 |
} |
| 98 |
|
| 99 |
public void test_eattributeDerivation() { |
| 100 |
assertSame(sizeSmall, size(acme)); |
| 101 |
|
| 102 |
// add a load of employees |
| 103 |
EList<EObject> emps = employees(acme); |
| 104 |
for (int i = 0; i < 60; i++) { |
| 105 |
emps.add(companyFactory.create(employeeClass)); |
| 106 |
} |
| 107 |
|
| 108 |
assertSame(sizeMedium, size(acme)); |
| 109 |
|
| 110 |
// and another bunch |
| 111 |
for (int i = 0; i < 1000; i++) { |
| 112 |
emps.add(companyFactory.create(employeeClass)); |
| 113 |
} |
| 114 |
|
| 115 |
assertSame(sizeLarge, size(acme)); |
| 116 |
} |
| 117 |
|
| 118 |
public void test_ereferenceDerivation() { |
| 119 |
EList<EObject> amyReports = directReports(employee("Amy")); |
| 120 |
assertEquals(3, amyReports.size()); |
| 121 |
assertTrue(amyReports.contains(employee("Bob"))); |
| 122 |
assertTrue(amyReports.contains(employee("Jane"))); |
| 123 |
assertTrue(amyReports.contains(employee("Fred"))); |
| 124 |
|
| 125 |
EList<EObject> bobReports = directReports(employee("Bob")); |
| 126 |
assertEquals(2, bobReports.size()); |
| 127 |
assertTrue(bobReports.contains(employee("Norbert"))); |
| 128 |
assertTrue(bobReports.contains(employee("Sally"))); |
| 129 |
|
| 130 |
EList<EObject> sallyReports = directReports(employee("Sally")); |
| 131 |
assertEquals(0, sallyReports.size()); |
| 132 |
} |
| 133 |
|
| 134 |
public void test_allInstances() { |
| 135 |
EList<EObject> amyAllReports = allReports(employee("Amy")); |
| 136 |
assertEquals(5, amyAllReports.size()); |
| 137 |
assertTrue(amyAllReports.contains(employee("Bob"))); |
| 138 |
assertTrue(amyAllReports.contains(employee("Jane"))); |
| 139 |
assertTrue(amyAllReports.contains(employee("Fred"))); |
| 140 |
assertTrue(amyAllReports.contains(employee("Norbert"))); |
| 141 |
assertTrue(amyAllReports.contains(employee("Sally"))); |
| 142 |
|
| 143 |
// change the set of all instances of Employee |
| 144 |
set(create(acme, companyEmployees, employeeClass, "Manuel"), |
| 145 |
employeeManager, employee("Bob")); |
| 146 |
|
| 147 |
amyAllReports = allReports(employee("Amy")); |
| 148 |
assertEquals(6, amyAllReports.size()); |
| 149 |
assertTrue(amyAllReports.contains(employee("Manuel"))); |
| 150 |
} |
| 151 |
|
| 152 |
// |
| 153 |
// Test framework |
| 154 |
// |
| 155 |
|
| 156 |
@Override |
| 157 |
protected void setUp() |
| 158 |
throws Exception { |
| 159 |
|
| 160 |
super.setUp(); |
| 161 |
|
| 162 |
((EStructuralFeature.Internal.SettingDelegate.Factory.Registry.Impl) EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE) |
| 163 |
.put("org.eclipse.ocl.ecore.OCL", new OCLSettingDelegateFactory()); |
| 164 |
|
| 165 |
rset = new ResourceSetImpl(); |
| 166 |
testResource = rset.getResource(URI.createPlatformPluginURI( |
| 167 |
"/org.eclipse.ocl.ecore.tests/model/TestCompany.xmi", true), true); |
| 168 |
|
| 169 |
acme = testResource.getContents().get(0); |
| 170 |
|
| 171 |
companyClass = acme.eClass(); |
| 172 |
companyPackage = companyClass.getEPackage(); |
| 173 |
companyFactory = companyPackage.getEFactoryInstance(); |
| 174 |
|
| 175 |
companyName = (EAttribute) companyClass.getEStructuralFeature("name"); |
| 176 |
companyEmployees = (EReference) companyClass |
| 177 |
.getEStructuralFeature("employees"); |
| 178 |
companySize = (EAttribute) companyClass.getEStructuralFeature("size"); |
| 179 |
|
| 180 |
employeeClass = companyEmployees.getEReferenceType(); |
| 181 |
employeeName = (EAttribute) employeeClass.getEStructuralFeature("name"); |
| 182 |
employeeManager = (EReference) employeeClass |
| 183 |
.getEStructuralFeature("manager"); |
| 184 |
employeeDirectReports = (EReference) employeeClass |
| 185 |
.getEStructuralFeature("directReports"); |
| 186 |
employeeAllReports = (EReference) employeeClass |
| 187 |
.getEStructuralFeature("allReports"); |
| 188 |
|
| 189 |
sizeKind = (EEnum) companySize.getEAttributeType(); |
| 190 |
sizeSmall = sizeKind.getEEnumLiteral("small"); |
| 191 |
sizeMedium = sizeKind.getEEnumLiteral("medium"); |
| 192 |
sizeLarge = sizeKind.getEEnumLiteral("large"); |
| 193 |
|
| 194 |
employees = new java.util.HashMap<String, EObject>(); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
protected void tearDown() |
| 199 |
throws Exception { |
| 200 |
|
| 201 |
for (Resource next : rset.getResources()) { |
| 202 |
next.unload(); |
| 203 |
} |
| 204 |
rset.getResources().clear(); |
| 205 |
rset = null; |
| 206 |
testResource = null; |
| 207 |
companyPackage = null; |
| 208 |
companyFactory = null; |
| 209 |
employees = null; |
| 210 |
|
| 211 |
((EStructuralFeature.Internal.SettingDelegate.Factory.Registry.Impl) EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE) |
| 212 |
.remove("org.eclipse.ocl.ecore.OCL"); |
| 213 |
|
| 214 |
super.tearDown(); |
| 215 |
} |
| 216 |
|
| 217 |
EObject employee(String name) { |
| 218 |
EObject result = employees.get(name); |
| 219 |
if (result == null) { |
| 220 |
EList<EObject> emps = get(acme, companyEmployees); |
| 221 |
for (EObject next : emps) { |
| 222 |
if (name.equals(name(next))) { |
| 223 |
result = next; |
| 224 |
employees.put(name, result); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return result; |
| 231 |
} |
| 232 |
|
| 233 |
String name(EObject employeeOrCompany) { |
| 234 |
EAttribute name = employeeClass.isInstance(employeeOrCompany) |
| 235 |
? employeeName |
| 236 |
: companyName; |
| 237 |
return get(employeeOrCompany, name); |
| 238 |
} |
| 239 |
|
| 240 |
EObject manager(EObject employee) { |
| 241 |
return get(employee, employeeManager); |
| 242 |
} |
| 243 |
|
| 244 |
EList<EObject> directReports(EObject employee) { |
| 245 |
return get(employee, employeeDirectReports); |
| 246 |
} |
| 247 |
|
| 248 |
EList<EObject> allReports(EObject employee) { |
| 249 |
return get(employee, employeeAllReports); |
| 250 |
} |
| 251 |
|
| 252 |
EList<EObject> employees(EObject company) { |
| 253 |
return get(company, companyEmployees); |
| 254 |
} |
| 255 |
|
| 256 |
Enumerator size(EObject company) { |
| 257 |
return get(company, companySize); |
| 258 |
} |
| 259 |
|
| 260 |
@SuppressWarnings("unchecked") |
| 261 |
<T> T get(EObject owner, EStructuralFeature feature) { |
| 262 |
return (T) owner.eGet(feature); |
| 263 |
} |
| 264 |
|
| 265 |
void set(EObject owner, EStructuralFeature feature, Object value) { |
| 266 |
owner.eSet(feature, value); |
| 267 |
} |
| 268 |
|
| 269 |
void add(EObject owner, EStructuralFeature feature, Object value) { |
| 270 |
this.<EList<Object>> get(owner, feature).add(value); |
| 271 |
} |
| 272 |
|
| 273 |
EObject create(EObject owner, EReference containment, EClass type, |
| 274 |
String name) { |
| 275 |
EObject result = companyFactory.create(type); |
| 276 |
|
| 277 |
if (containment.isMany()) { |
| 278 |
add(owner, containment, result); |
| 279 |
} else { |
| 280 |
set(owner, containment, result); |
| 281 |
} |
| 282 |
|
| 283 |
if (name != null) { |
| 284 |
set(result, type.getEStructuralFeature("name"), name); |
| 285 |
} |
| 286 |
|
| 287 |
return result; |
| 288 |
} |
| 289 |
|
| 290 |
<T> EList<T> list(T... element) { |
| 291 |
return new BasicEList<T>(Arrays.asList(element)); |
| 292 |
} |
| 293 |
} |