1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.uml;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.io.FileOutputStream;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.InputStream;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.LinkedList;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Map;
|
---|
26 |
|
---|
27 | import org.eclipse.emf.common.util.URI;
|
---|
28 | import org.eclipse.emf.ecore.EPackage;
|
---|
29 | import org.eclipse.emf.ecore.EcorePackage;
|
---|
30 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
31 | import org.eclipse.emf.ecore.resource.ResourceSet;
|
---|
32 | import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
|
---|
33 | import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
|
---|
34 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
---|
35 | import org.eclipse.emf.ecore.util.EcoreUtil;
|
---|
36 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
|
---|
37 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
|
---|
38 | import org.eclipse.uml2.uml.Model;
|
---|
39 | import org.eclipse.uml2.uml.StateMachine;
|
---|
40 | import org.eclipse.uml2.uml.Transition;
|
---|
41 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
42 | import org.eclipse.uml2.uml.resource.UMLResource;
|
---|
43 | import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
|
---|
44 | import org.junit.After;
|
---|
45 | import org.junit.Test;
|
---|
46 |
|
---|
47 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
48 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
49 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
50 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * Tests for AutoQUESTs UMLUtils
|
---|
55 | * </p>
|
---|
56 | *
|
---|
57 | * @author Steffen Herbold
|
---|
58 | */
|
---|
59 | public class UMLUtilsTest {
|
---|
60 |
|
---|
61 | private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
|
---|
62 |
|
---|
63 | /**
|
---|
64 | *
|
---|
65 | */
|
---|
66 | @After
|
---|
67 | public void tearDown() throws Exception {
|
---|
68 | deleteFiles(new File(OUTPUT_DIR));
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test(expected = java.lang.RuntimeException.class)
|
---|
72 | public void testCreateUMLTransitionSequence_1() throws Exception {
|
---|
73 | // parse log file
|
---|
74 | HTTPLogParser parser = new HTTPLogParser();
|
---|
75 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
76 | .getFile()));
|
---|
77 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
78 | Model model =
|
---|
79 | loadModelFromInputStream(ClassLoader
|
---|
80 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
81 |
|
---|
82 | StateMachine stateMachine =
|
---|
83 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
84 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
85 |
|
---|
86 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
87 | for (List<Event> httpSequence : httpSequences) {
|
---|
88 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
89 | Event event = eventIter.next();
|
---|
90 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
91 | eventIter.remove();
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
95 | eventIter.remove();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void testConvertStateMachineToUsageProfile_1() throws Exception {
|
---|
106 | // parse log file
|
---|
107 | HTTPLogParser parser = new HTTPLogParser();
|
---|
108 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
109 | .getFile()));
|
---|
110 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
111 | Model model =
|
---|
112 | loadModelFromInputStream(ClassLoader
|
---|
113 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
114 |
|
---|
115 | StateMachine stateMachine =
|
---|
116 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
117 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
118 |
|
---|
119 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
120 | for (List<Event> httpSequence : httpSequences) {
|
---|
121 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
122 | Event event = eventIter.next();
|
---|
123 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
124 | eventIter.remove();
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
128 | eventIter.remove();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | List<List<Transition>> matchingSequences =
|
---|
134 | UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
135 | if (matchingSequences.size() >= 1) {
|
---|
136 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
137 | for (Transition transition : matchingSequences.get(0)) {
|
---|
138 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
139 | }
|
---|
140 | umlSequences.add(umlEventSequence);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
|
---|
144 |
|
---|
145 | writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Test
|
---|
149 | public void testCreateInteractionFromEventSequence_1() throws Exception {
|
---|
150 | // parse log file
|
---|
151 | HTTPLogParser parser =
|
---|
152 | new HTTPLogParser(new File(ClassLoader
|
---|
153 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt").getFile()));
|
---|
154 | parser.parseFile(new File(ClassLoader
|
---|
155 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log").getFile()));
|
---|
156 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
157 | Model model =
|
---|
158 | loadModelFromInputStream(ClassLoader
|
---|
159 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
160 |
|
---|
161 | for (List<Event> httpSequence : httpSequences) {
|
---|
162 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
163 | Event event = eventIter.next();
|
---|
164 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
165 | eventIter.remove();
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence");
|
---|
170 | }
|
---|
171 | //writeModelToFile(model, "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
172 | }
|
---|
173 |
|
---|
174 | private static void writeModelToFile(Model model, String filename) throws IOException {
|
---|
175 | final ResourceSet resourceSet = new ResourceSetImpl();
|
---|
176 | UMLResourcesUtil.init(resourceSet);
|
---|
177 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
178 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ResourceFactoryImpl() {
|
---|
179 | @Override
|
---|
180 | public Resource createResource(URI uri) {
|
---|
181 | return new XMIResourceImpl(uri) {
|
---|
182 | @Override
|
---|
183 | public boolean useUUIDs()
|
---|
184 | {
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 | };
|
---|
188 | }
|
---|
189 | });
|
---|
190 |
|
---|
191 | Resource resource = resourceSet.createResource(URI.createURI("binresource"));
|
---|
192 |
|
---|
193 | resource.getContents().add(model);
|
---|
194 | FileOutputStream fos;
|
---|
195 | File file = new File(filename);
|
---|
196 | fos = new FileOutputStream(file);
|
---|
197 | resource.save(fos, null);
|
---|
198 | }
|
---|
199 |
|
---|
200 | private static Model loadModelFromInputStream(InputStream inputStream) {
|
---|
201 | ResourceSet resourceSet = new ResourceSetImpl();
|
---|
202 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
203 | .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
|
---|
204 | resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
205 | resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
|
---|
206 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
207 | .put(Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
|
---|
208 | Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
|
---|
209 |
|
---|
210 | UMLResourcesUtil.init(resourceSet);
|
---|
211 |
|
---|
212 | uriMap.put(URI.createURI("pathmap://Papyrus.profile.uml"),
|
---|
213 | URI.createURI("file:/D:/.../Papyrus.profile.uml/"));
|
---|
214 |
|
---|
215 | EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
216 |
|
---|
217 | Resource resource = new XMIResourceImpl();
|
---|
218 |
|
---|
219 | try {
|
---|
220 | // resource.load(inputStream, options);
|
---|
221 | resource.load(inputStream, null);
|
---|
222 | }
|
---|
223 | catch (IOException e) {
|
---|
224 | // TODO Auto-generated catch block
|
---|
225 | e.printStackTrace();
|
---|
226 | }
|
---|
227 |
|
---|
228 | return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void deleteFiles(File file) {
|
---|
232 | if (file.exists()) {
|
---|
233 | if (file.isDirectory()) {
|
---|
234 | for (File child : file.listFiles()) {
|
---|
235 | deleteFiles(child);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | try {
|
---|
240 | file.delete();
|
---|
241 | }
|
---|
242 | catch (Exception e) {
|
---|
243 | // ignore and delete as much as possible
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | }
|
---|