|
Added
Link Here
|
| 1 |
/* |
| 2 |
* Copyright (c) 2016 Christian W. Damus and others. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available 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 |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* Christian W. Damus - initial API and implementation |
| 11 |
* |
| 12 |
*/ |
| 13 |
package org.eclipse.uml2.uml.bug.tests; |
| 14 |
|
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.emf.common.util.URI; |
| 18 |
import org.eclipse.emf.ecore.EClass; |
| 19 |
import org.eclipse.emf.ecore.EObject; |
| 20 |
import org.eclipse.emf.ecore.resource.Resource; |
| 21 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 22 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 23 |
import org.eclipse.uml2.common.util.CacheAdapter; |
| 24 |
import org.eclipse.uml2.common.util.UML2Util; |
| 25 |
import org.eclipse.uml2.uml.Artifact; |
| 26 |
import org.eclipse.uml2.uml.Class; |
| 27 |
import org.eclipse.uml2.uml.Element; |
| 28 |
import org.eclipse.uml2.uml.Package; |
| 29 |
import org.eclipse.uml2.uml.PackageableElement; |
| 30 |
import org.eclipse.uml2.uml.Profile; |
| 31 |
import org.eclipse.uml2.uml.Stereotype; |
| 32 |
import org.eclipse.uml2.uml.UMLFactory; |
| 33 |
import org.eclipse.uml2.uml.UMLPackage; |
| 34 |
import org.eclipse.uml2.uml.profile.standard.StandardPackage; |
| 35 |
import org.eclipse.uml2.uml.resource.UMLResource; |
| 36 |
import org.eclipse.uml2.uml.tests.util.StandaloneSupport; |
| 37 |
import org.eclipse.uml2.uml.tests.util.Stopwatch; |
| 38 |
import org.eclipse.uml2.uml.util.UMLUtil.StereotypeApplicationHelper; |
| 39 |
|
| 40 |
import junit.framework.Test; |
| 41 |
import junit.framework.TestCase; |
| 42 |
import junit.framework.TestSuite; |
| 43 |
|
| 44 |
/** |
| 45 |
* Tests the performance of operations on stereotype applications from static |
| 46 |
* profiles. |
| 47 |
* |
| 48 |
* @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=501740 |
| 49 |
*/ |
| 50 |
public class Bug501740Test |
| 51 |
extends TestCase { |
| 52 |
|
| 53 |
private static final int ITERATIONS = 10; |
| 54 |
|
| 55 |
private ResourceSet rset; |
| 56 |
|
| 57 |
private Package fixture; |
| 58 |
|
| 59 |
public Bug501740Test() { |
| 60 |
super(); |
| 61 |
} |
| 62 |
|
| 63 |
public Bug501740Test(String name) { |
| 64 |
super(name); |
| 65 |
} |
| 66 |
|
| 67 |
public static Test suite() { |
| 68 |
return new TestSuite(Bug501740Test.class, "Bug 501740 tests"); //$NON-NLS-1$ |
| 69 |
} |
| 70 |
|
| 71 |
public void testCreatingManyStereotypedElements() { |
| 72 |
Stopwatch watch = new Stopwatch(); |
| 73 |
|
| 74 |
for (int i = 0; i < ITERATIONS; i++) { |
| 75 |
watch.start(); |
| 76 |
createManyStereotypedElements(); |
| 77 |
watch.end(); |
| 78 |
|
| 79 |
// Clean up |
| 80 |
nextIteration(); |
| 81 |
} |
| 82 |
|
| 83 |
watch.log("Creation"); |
| 84 |
} |
| 85 |
|
| 86 |
private void createManyStereotypedElements() { |
| 87 |
for (int i = 0; i < 500; i++) { |
| 88 |
Class class_ = fixture.createOwnedClass(null, false); |
| 89 |
applyStereotype(class_, StandardPackage.Literals.AUXILIARY); |
| 90 |
|
| 91 |
Artifact artifact = (Artifact) fixture.createPackagedElement(null, |
| 92 |
UMLPackage.Literals.ARTIFACT); |
| 93 |
applyStereotype(artifact, StandardPackage.Literals.EXECUTABLE); |
| 94 |
|
| 95 |
Package nested = fixture.createNestedPackage(null); |
| 96 |
applyStereotype(nested, StandardPackage.Literals.MODEL_LIBRARY); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public void testReadingManyStereotypedElements() { |
| 101 |
Stopwatch watch = new Stopwatch(); |
| 102 |
|
| 103 |
// Create a bunch of elements to read |
| 104 |
createManyStereotypedElements(); |
| 105 |
|
| 106 |
for (int i = 0; i < ITERATIONS; i++) { |
| 107 |
watch.start(); |
| 108 |
readManyStereotypedElements(); |
| 109 |
watch.end(); |
| 110 |
|
| 111 |
// Clean up |
| 112 |
CacheAdapter.getCacheAdapter(fixture).clear(); |
| 113 |
} |
| 114 |
|
| 115 |
watch.log("Reading"); |
| 116 |
} |
| 117 |
|
| 118 |
private void readManyStereotypedElements() { |
| 119 |
for (PackageableElement next : fixture.getPackagedElements()) { |
| 120 |
List<Stereotype> stereos = next.getAppliedStereotypes(); |
| 121 |
assertTrue(stereos.size() == 1); |
| 122 |
assertEquals("StandardProfile", |
| 123 |
stereos.get(0).getProfile().getName()); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// |
| 128 |
// Test framework |
| 129 |
// |
| 130 |
|
| 131 |
@Override |
| 132 |
protected void setUp() |
| 133 |
throws Exception { |
| 134 |
|
| 135 |
rset = new ResourceSetImpl(); |
| 136 |
if (StandaloneSupport.isStandalone()) { |
| 137 |
StandaloneSupport.init(rset); |
| 138 |
} |
| 139 |
|
| 140 |
fixture = getTestModel(); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected void tearDown() |
| 145 |
throws Exception { |
| 146 |
|
| 147 |
fixture = null; |
| 148 |
|
| 149 |
// clean up the CacheAdapter as well as we can |
| 150 |
for (Resource next : rset.getResources()) { |
| 151 |
next.unload(); |
| 152 |
next.eAdapters().clear(); |
| 153 |
} |
| 154 |
|
| 155 |
rset.getResources().clear(); |
| 156 |
rset.eAdapters().clear(); |
| 157 |
} |
| 158 |
|
| 159 |
void nextIteration() { |
| 160 |
try { |
| 161 |
tearDown(); |
| 162 |
setUp(); |
| 163 |
} catch (Exception e) { |
| 164 |
e.printStackTrace(); |
| 165 |
fail("Failed to reset for next iteration: " + e.getMessage()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Package getTestModel() { |
| 170 |
// Just create a small model locally |
| 171 |
Resource resource = UMLResource.Factory.INSTANCE |
| 172 |
.createResource(URI.createURI("test:bug501740")); |
| 173 |
Package result = UMLFactory.eINSTANCE.createPackage(); |
| 174 |
result.setName("bug501740"); |
| 175 |
resource.getContents().add(result); |
| 176 |
rset.getResources().add(resource); |
| 177 |
|
| 178 |
// Apply the standard profile, which is statically generated |
| 179 |
Profile std = UML2Util.load(rset, |
| 180 |
URI.createURI(UMLResource.STANDARD_PROFILE_URI), |
| 181 |
UMLPackage.Literals.PROFILE); |
| 182 |
result.applyProfile(std); |
| 183 |
|
| 184 |
// Apply a stereotype to the package |
| 185 |
applyStereotype(result, StandardPackage.Literals.MODEL_LIBRARY); |
| 186 |
|
| 187 |
return result; |
| 188 |
} |
| 189 |
|
| 190 |
protected static EObject applyStereotype(Element element, |
| 191 |
EClass definition) { |
| 192 |
return StereotypeApplicationHelper.getInstance(element) |
| 193 |
.applyStereotype(element, definition); |
| 194 |
} |
| 195 |
|
| 196 |
} |