Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ac2156751f798e493089b37a731368e979ce92f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2017 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.tests.ui.properties.internal.preprocessor;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.eclipse.eef.EefPackage;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.match.DefaultComparisonFactory;
import org.eclipse.emf.compare.match.DefaultEqualityHelperFactory;
import org.eclipse.emf.compare.match.DefaultMatchEngine;
import org.eclipse.emf.compare.match.IComparisonFactory;
import org.eclipse.emf.compare.match.IMatchEngine;
import org.eclipse.emf.compare.match.eobject.IEObjectMatcher;
import org.eclipse.emf.compare.match.impl.MatchEngineFactoryImpl;
import org.eclipse.emf.compare.match.impl.MatchEngineFactoryRegistryImpl;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.compare.utils.UseIdentifiers;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.sirius.common.interpreter.api.IVariableManager;
import org.eclipse.sirius.common.interpreter.api.VariableManagerFactory;
import org.eclipse.sirius.properties.PropertiesPackage;
import org.eclipse.sirius.properties.ViewExtensionDescription;
import org.eclipse.sirius.properties.core.api.OverridesProvider;
import org.eclipse.sirius.properties.core.api.ViewDescriptionPreprocessor;
import org.junit.Test;

/**
 * Test suite for the
 * {@link org.eclipse.sirius.properties.core.api.IDescriptionPreprocessor}.
 * 
 * @author flatombe
 * @author mbats
 */
public final class PreprocessorExtendsTest {
    /**
     * The path of the input model in the bundle.
     */
    private static final String INPUT_MODEL_PATH = "/data/preprocessor/extends/input.odesign";

    /**
     * This test is used to ensure the proper transformation of a Sirius
     * Properties model with extends and overrides definitions into a resolved
     * "flat" Sirius Properties model.
     */
    @Test
    public void testPreprocessorTest() {
        EObject processedEObject = this.preprocess(this.load(INPUT_MODEL_PATH).getContents().get(0).eContents().get(0));
        EObject expectedEObject = this.load("/data/preprocessor/extends/expected.odesign").getContents().get(0);

        // For debug purpose only
        print(processedEObject);

        IComparisonScope scope = new DefaultComparisonScope(processedEObject, expectedEObject, null);
        IEObjectMatcher matcher = DefaultMatchEngine.createDefaultEObjectMatcher(UseIdentifiers.NEVER);
        IComparisonFactory comparisonFactory = new DefaultComparisonFactory(new DefaultEqualityHelperFactory());
        IMatchEngine.Factory matchEngineFactory = new MatchEngineFactoryImpl(matcher, comparisonFactory);
        matchEngineFactory.setRanking(20);
        IMatchEngine.Factory.Registry matchEngineRegistry = new MatchEngineFactoryRegistryImpl();
        matchEngineRegistry.add(matchEngineFactory);

        EMFCompare comparator = EMFCompare.builder().setMatchEngineFactoryRegistry(matchEngineRegistry).build();
        Comparison comparison = comparator.compare(scope);
        List<Diff> differences = comparison.getDifferences();

        assertEquals(0, differences.size());
    }

    /**
     * For debug use only, used to save the processed odesign into a file to
     * compare easily the processed result with the expected one.
     * 
     * @param resource
     */
    private void print(EObject eObject) {
        try {
            eObject.eResource().save(System.out, new HashMap<>());
        } catch (IOException e) {
            // Nothing
        }
    }

    /**
     * Transforms the given Sirius EObject into an EEF EObject.
     * 
     * @param eObject
     *            The Sirius EObject
     * @return The EEF EObject created form the Sirius one
     */
    private EObject preprocess(EObject eObject) {
        if (eObject instanceof ViewExtensionDescription) {
            ViewExtensionDescription viewExtensionDescription = (ViewExtensionDescription) eObject;
            ViewDescriptionPreprocessor preprocessor = new ViewDescriptionPreprocessor(viewExtensionDescription);
            IVariableManager variableManager = new VariableManagerFactory().createVariableManager();
            variableManager.put("self", viewExtensionDescription);
            Optional<ViewExtensionDescription> processedOptional = preprocessor.convert(new AQLInterpreter(), variableManager, new OverridesProvider(null));
            if (processedOptional.isPresent()) {
                EObject processed = processedOptional.get();
                ResourceSet resourceSet = new ResourceSetImpl();
                resourceSet.getPackageRegistry().put(EefPackage.eNS_URI, EefPackage.eINSTANCE);
                resourceSet.getPackageRegistry().put(PropertiesPackage.eNS_URI, PropertiesPackage.eINSTANCE);
                resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", //$NON-NLS-1$
                        new XMIResourceFactoryImpl());
                Resource resource = resourceSet.createResource(URI.createURI(INPUT_MODEL_PATH));
                resource.getContents().clear();
                resource.getContents().add(processed);

                return processed;
            }
        }
        return null;
    }

    /**
     * Loads the resource with the given URI in a new resource set and return
     * it.
     * 
     * @param uri
     *            The URI of the resource to load
     * @return The resource loaded
     */
    private Resource load(String uri) {
        ResourceSet resourceSet = new ResourceSetImpl();
        resourceSet.getPackageRegistry().put(PropertiesPackage.eNS_URI, PropertiesPackage.eINSTANCE);
        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl()); //$NON-NLS-1$
        Resource resource = resourceSet.getResource(URI.createFileURI(System.getProperty("user.dir") + uri), true);
        resource.setURI(URI.createURI(INPUT_MODEL_PATH));
        return resource;
    }

}

Back to the top