source: trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtilsTest.java @ 1608

Last change on this file since 1608 was 1608, checked in by sherbold, 10 years ago
  • changed test case so that it is running for now; needs to be adapted as soon as we have another lock
  • Property svn:mime-type set to text/plain
File size: 8.7 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.uml;
16
17import java.io.File;
18import java.io.FileOutputStream;
19import java.io.IOException;
20import java.io.InputStream;
21import java.util.Collection;
22import java.util.Iterator;
23import java.util.LinkedList;
24import java.util.List;
25import java.util.Map;
26
27import org.apache.commons.math3.analysis.function.Rint;
28import org.eclipse.emf.common.util.URI;
29import org.eclipse.emf.ecore.EPackage;
30import org.eclipse.emf.ecore.EcorePackage;
31import org.eclipse.emf.ecore.resource.Resource;
32import org.eclipse.emf.ecore.resource.ResourceSet;
33import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
34import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
35import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
36import org.eclipse.emf.ecore.util.EcoreUtil;
37import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
38import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
39import org.eclipse.uml2.uml.Model;
40import org.eclipse.uml2.uml.StateMachine;
41import org.eclipse.uml2.uml.Transition;
42import org.eclipse.uml2.uml.UMLPackage;
43import org.eclipse.uml2.uml.resource.UMLResource;
44import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
45import org.junit.After;
46import org.junit.Test;
47
48import de.ugoe.cs.autoquest.eventcore.Event;
49import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
50import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
51import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
52
53/**
54 * <p>
55 * Tests for AutoQUESTs UMLUtils
56 * </p>
57 *
58 * @author Steffen Herbold
59 */
60public class UMLUtilsTest {
61   
62    private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
63   
64    /**
65    *
66    */
67   @After
68   public void tearDown() throws Exception {
69       deleteFiles(new File(OUTPUT_DIR));
70   }
71
72    @Test(expected=java.lang.RuntimeException.class)
73    public void createUMLTransitionSequence_1() throws Exception {
74        // parse log file
75        HTTPLogParser parser = new HTTPLogParser();
76        parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
77            .getFile()));
78        Collection<List<Event>> httpSequences = parser.getSequences();
79        Model model =
80            loadModelFromInputStream(ClassLoader
81                .getSystemResourceAsStream("createSequence_1_model.uml"));
82
83        StateMachine stateMachine =
84            (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
85                                                    UMLPackage.Literals.STATE_MACHINE, true);
86
87        Collection<List<Event>> umlSequences = new LinkedList<>();
88        for (List<Event> httpSequence : httpSequences) {
89            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
90                Event event = eventIter.next();
91                if (!(event.getType() instanceof SOAPEventType)) {
92                    eventIter.remove();
93                }
94                else {
95                    if (!event.getType().toString().contains("ixsmq")) {
96                        eventIter.remove();
97                    }
98                }
99            }
100
101            umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
102        }
103    }
104   
105    @Test
106    public void convertStateMachineToUsageProfile_1() throws Exception {
107        // parse log file
108        HTTPLogParser parser = new HTTPLogParser();
109        parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
110            .getFile()));
111        Collection<List<Event>> httpSequences = parser.getSequences();
112        Model model =
113            loadModelFromInputStream(ClassLoader
114                .getSystemResourceAsStream("createSequence_1_model.uml"));
115
116        StateMachine stateMachine =
117            (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
118                                                    UMLPackage.Literals.STATE_MACHINE, true);
119
120        Collection<List<Event>> umlSequences = new LinkedList<>();
121        for (List<Event> httpSequence : httpSequences) {
122            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
123                Event event = eventIter.next();
124                if (!(event.getType() instanceof SOAPEventType)) {
125                    eventIter.remove();
126                }
127                else {
128                    if (!event.getType().toString().contains("ixsmq")) {
129                        eventIter.remove();
130                    }
131                }
132            }
133
134            List<List<Transition>> matchingSequences = 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    private static void writeModelToFile(Model model, String filename) throws IOException {
149        final ResourceSet resourceSet = new ResourceSetImpl();
150        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
151            .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ResourceFactoryImpl() {
152                @Override
153                public Resource createResource(URI uri) {
154                    return new XMIResourceImpl(uri);
155                }
156            });
157
158        Resource resource = resourceSet.createResource(URI.createURI("binresource"));
159
160        resource.getContents().add(model);
161        FileOutputStream fos;
162        File file = new File(filename);
163        file.getParentFile().mkdirs();
164        fos = new FileOutputStream(file);
165        resource.save(fos, null);
166    }
167
168    private static Model loadModelFromInputStream(InputStream inputStream) {
169        ResourceSet resourceSet = new ResourceSetImpl();
170        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
171            .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
172        resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
173        resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
174        resourceSet.getResourceFactoryRegistry();
175        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
176            .put(Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
177        Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
178
179        UMLResourcesUtil.init(resourceSet);
180
181        uriMap.put(URI.createURI("pathmap://Papyrus.profile.uml"),
182                   URI.createURI("file:/D:/.../Papyrus.profile.uml/"));
183
184        EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
185
186        Resource resource = new XMIResourceImpl();
187        /*
188         * XMIResource.XMLMap xmlMap = new XMLMapImpl(); Map options = new HashMap();
189         * options.put(XMIResource.OPTION_XML_MAP, xmlMap);
190         */
191
192        try {
193            // resource.load(inputStream, options);
194            resource.load(inputStream, null);
195        }
196        catch (IOException e) {
197            // TODO Auto-generated catch block
198            e.printStackTrace();
199        }
200
201        return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
202    }
203
204   private void deleteFiles(File file) {
205       if (file.exists()) {
206           if (file.isDirectory()) {
207               for (File child : file.listFiles()) {
208                   deleteFiles(child);
209               }
210           }
211           
212           try {
213               file.delete();
214           }
215           catch (Exception e) {
216               // ignore and delete as much as possible
217           }
218       }
219   }
220
221}
Note: See TracBrowser for help on using the repository browser.