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

Last change on this file since 1603 was 1603, checked in by sherbold, 10 years ago
  • Property svn:mime-type set to text/plain
File size: 8.5 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.eclipse.emf.common.util.URI;
28import org.eclipse.emf.ecore.EPackage;
29import org.eclipse.emf.ecore.EcorePackage;
30import org.eclipse.emf.ecore.resource.Resource;
31import org.eclipse.emf.ecore.resource.ResourceSet;
32import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
33import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
34import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
35import org.eclipse.emf.ecore.util.EcoreUtil;
36import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
37import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
38import org.eclipse.uml2.uml.Model;
39import org.eclipse.uml2.uml.StateMachine;
40import org.eclipse.uml2.uml.Transition;
41import org.eclipse.uml2.uml.UMLPackage;
42import org.eclipse.uml2.uml.resource.UMLResource;
43import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
44import org.junit.After;
45import org.junit.Test;
46
47import de.ugoe.cs.autoquest.eventcore.Event;
48import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
49import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
50import 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 */
59public class UMLUtilsTest {
60   
61    private final static String OUTPUT_DIR = "target/tmp/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        fos = new FileOutputStream(filename);
162        resource.save(fos, null);
163    }
164
165    private static Model loadModelFromInputStream(InputStream inputStream) {
166        ResourceSet resourceSet = new ResourceSetImpl();
167        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
168            .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
169        resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
170        resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
171        resourceSet.getResourceFactoryRegistry();
172        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
173            .put(Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
174        Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
175
176        UMLResourcesUtil.init(resourceSet);
177
178        uriMap.put(URI.createURI("pathmap://Papyrus.profile.uml"),
179                   URI.createURI("file:/D:/.../Papyrus.profile.uml/"));
180
181        EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
182
183        Resource resource = new XMIResourceImpl();
184        /*
185         * XMIResource.XMLMap xmlMap = new XMLMapImpl(); Map options = new HashMap();
186         * options.put(XMIResource.OPTION_XML_MAP, xmlMap);
187         */
188
189        try {
190            // resource.load(inputStream, options);
191            resource.load(inputStream, null);
192        }
193        catch (IOException e) {
194            // TODO Auto-generated catch block
195            e.printStackTrace();
196        }
197
198        return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
199    }
200
201   private void deleteFiles(File file) {
202       if (file.exists()) {
203           if (file.isDirectory()) {
204               for (File child : file.listFiles()) {
205                   deleteFiles(child);
206               }
207           }
208           
209           try {
210               file.delete();
211           }
212           catch (Exception e) {
213               // ignore and delete as much as possible
214           }
215       }
216   }
217
218}
Note: See TracBrowser for help on using the repository browser.