Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73a95af7434f9abb15b999f0388ab2bd20aaa364 (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
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.tests.base;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.graphiti.mm.algorithms.styles.Color;
import org.eclipse.graphiti.util.IColorConstant;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;

public abstract class TestBase {

	private String basePath;
	private LinkedList<RoomModel> models = new LinkedList<RoomModel>();

	protected void loadModelFile() {
		try {
			URL modelsDir = getModelsDirectoy();
			URL fileURL = FileLocator.toFileURL(modelsDir);
			basePath = fileURL.getFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		loadModels(getModelFileName());
	}

	protected void removeDiagramsDirectory() {
		File diagDir = new File(basePath+File.separator+"diagrams");
		if (diagDir.isDirectory()) {
			File[] files = diagDir.listFiles();
			if (files!=null)
				for (int i = 0; i < files.length; i++) {
					files[i].delete();
				}
			diagDir.delete();
		}
	}
	
	private void loadModels(String modelName) {
		XtextResourceSet rs = new XtextResourceSet();
		rs.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		String path = basePath + modelName;
		URI uri = URI.createFileURI(path);
		Resource resource = rs.getResource(uri, true);
		Object root = resource.getContents().get(0);
		if (root instanceof RoomModel)
			models.add((RoomModel) root);
		else
			fail("File contains no RoomModel as root object");
	}

	public LinkedList<RoomModel> getModels() {
		return models;
	}

	protected abstract String getModelFileName();
	protected abstract URL getModelsDirectoy();

	protected boolean isEqual(Color c, IColorConstant cc) {
		if (c.getRed()!=cc.getRed())
			return false;
		if (c.getGreen()!=cc.getGreen())
			return false;
		if (c.getBlue()!=cc.getBlue())
			return false;
		return true;
	}
}

Back to the top