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
|
---|
72 | public void createUMLTransitionSequence_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 convertStateMachineToUsageProfile_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 = UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
134 | if (matchingSequences.size() >= 1) {
|
---|
135 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
136 | for (Transition transition : matchingSequences.get(0)) {
|
---|
137 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
138 | }
|
---|
139 | umlSequences.add(umlEventSequence);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
|
---|
143 |
|
---|
144 | writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
145 | }
|
---|
146 |
|
---|
147 | private static void writeModelToFile(Model model, String filename) throws IOException {
|
---|
148 | final ResourceSet resourceSet = new ResourceSetImpl();
|
---|
149 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
150 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ResourceFactoryImpl() {
|
---|
151 | @Override
|
---|
152 | public Resource createResource(URI uri) {
|
---|
153 | return new XMIResourceImpl(uri);
|
---|
154 | }
|
---|
155 | });
|
---|
156 |
|
---|
157 | Resource resource = resourceSet.createResource(URI.createURI("binresource"));
|
---|
158 |
|
---|
159 | resource.getContents().add(model);
|
---|
160 | FileOutputStream fos;
|
---|
161 | File file = new File(filename);
|
---|
162 | file.getParentFile().mkdirs();
|
---|
163 | fos = new FileOutputStream(file);
|
---|
164 | resource.save(fos, null);
|
---|
165 | }
|
---|
166 |
|
---|
167 | private static Model loadModelFromInputStream(InputStream inputStream) {
|
---|
168 | ResourceSet resourceSet = new ResourceSetImpl();
|
---|
169 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
170 | .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
|
---|
171 | resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
172 | resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
|
---|
173 | resourceSet.getResourceFactoryRegistry();
|
---|
174 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
175 | .put(Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
|
---|
176 | Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
|
---|
177 |
|
---|
178 | UMLResourcesUtil.init(resourceSet);
|
---|
179 |
|
---|
180 | uriMap.put(URI.createURI("pathmap://Papyrus.profile.uml"),
|
---|
181 | URI.createURI("file:/D:/.../Papyrus.profile.uml/"));
|
---|
182 |
|
---|
183 | EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
184 |
|
---|
185 | Resource resource = new XMIResourceImpl();
|
---|
186 | /*
|
---|
187 | * XMIResource.XMLMap xmlMap = new XMLMapImpl(); Map options = new HashMap();
|
---|
188 | * options.put(XMIResource.OPTION_XML_MAP, xmlMap);
|
---|
189 | */
|
---|
190 |
|
---|
191 | try {
|
---|
192 | // resource.load(inputStream, options);
|
---|
193 | resource.load(inputStream, null);
|
---|
194 | }
|
---|
195 | catch (IOException e) {
|
---|
196 | // TODO Auto-generated catch block
|
---|
197 | e.printStackTrace();
|
---|
198 | }
|
---|
199 |
|
---|
200 | return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void deleteFiles(File file) {
|
---|
204 | if (file.exists()) {
|
---|
205 | if (file.isDirectory()) {
|
---|
206 | for (File child : file.listFiles()) {
|
---|
207 | deleteFiles(child);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | try {
|
---|
212 | file.delete();
|
---|
213 | }
|
---|
214 | catch (Exception e) {
|
---|
215 | // ignore and delete as much as possible
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|