Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 406d336ade48f4e54f2bc0a98a386a059b4717ab (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
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.metatype.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.eclipse.equinox.compendium.tests.Activator;
import org.eclipse.equinox.metatype.EquinoxMetaTypeService;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.junit.*;
import org.osgi.framework.ServiceReference;
import org.osgi.service.metatype.*;

public abstract class AbstractTest {
	protected BundleInstaller bundleInstaller;
	protected EquinoxMetaTypeService metatype;
	protected ServiceReference<EquinoxMetaTypeService> metaTypeReference;

	protected void assertAttributeDefinition(AttributeDefinition ad, int cardinality, String[] defaultValue, String description, String id, String name, String[] optionLabels, String[] optionValues, int type) {
		Assert.assertEquals("Wrong cardinality", cardinality, ad.getCardinality()); //$NON-NLS-1$
		assertEquals("Wrong default value", defaultValue, ad.getDefaultValue()); //$NON-NLS-1$
		Assert.assertEquals("Wrong description", description, ad.getDescription()); //$NON-NLS-1$
		Assert.assertEquals("Wrong id", id, ad.getID()); //$NON-NLS-1$
		Assert.assertEquals("Wrong name", name, ad.getName()); //$NON-NLS-1$
		assertEquals("Wrong option labels", optionLabels, ad.getOptionLabels()); //$NON-NLS-1$
		assertEquals("Wrong option values", optionValues, ad.getOptionValues()); //$NON-NLS-1$
		Assert.assertEquals("Wrong type", type, ad.getType()); //$NON-NLS-1$
	}

	protected void assertAttributeDefinitions(AttributeDefinition[] ads, int size) {
		Assert.assertNotNull("Null attribute definitions", ads); //$NON-NLS-1$
		Assert.assertEquals("Wrong attribute definitions size", size, ads.length); //$NON-NLS-1$
	}

	protected void assertEquals(String message, String[] s1, String[] s2) {
		if (s1 == s2)
			return;
		// We know that at least one is not null from the above check.
		if (s1 == null || s2 == null)
			fail(message + " (one of the arrays was null)"); //$NON-NLS-1$
		if (s1.length != s2.length)
			fail(message + " (array lengths weren't equal)"); //$NON-NLS-1$
		for (int i = 0; i < s1.length; i++)
			Assert.assertEquals(message, s1[i], s2[i]);
	}

	protected void assertIcon(InputStream icon, int size) throws IOException {
		Assert.assertNotNull("Icon was null", icon); //$NON-NLS-1$
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			byte[] bytes = new byte[2048];
			int read;
			try {
				while ((read = icon.read(bytes)) != -1) {
					baos.write(bytes, 0, read);
				}
			} finally {
				icon.close();
			}
			Icon i = new ImageIcon(baos.toByteArray());
			Assert.assertEquals("Wrong icon size", size, i.getIconHeight() * i.getIconWidth()); //$NON-NLS-1$
		} finally {
			baos.close();
		}
	}

	protected void assertNotNull(MetaTypeInformation mti) {
		Assert.assertNotNull("Metatype information was null", mti); //$NON-NLS-1$
	}

	protected void assertNotNull(ObjectClassDefinition ocd) {
		Assert.assertNotNull("Object class definition was null", ocd); //$NON-NLS-1$
	}

	protected void assertObjectClassDefinition(ObjectClassDefinition ocd, String id, String name, String description) {
		assertNotNull(ocd);
		Assert.assertEquals("Wrong object class definition ID", id, ocd.getID()); //$NON-NLS-1$
		Assert.assertEquals("Wrong object class definition name", name, ocd.getName()); //$NON-NLS-1$
		Assert.assertEquals("Wrong object class definition description", description, ocd.getDescription()); //$NON-NLS-1$
	}

	protected void assertValidationFail(String value, AttributeDefinition ad) {
		String result = assertValidationPresent(value, ad);
		assertTrue("Validation passed", result.length() > 0); //$NON-NLS-1$
	}

	protected void assertValidationPass(String value, AttributeDefinition ad) {
		String result = assertValidationPresent(value, ad);
		Assert.assertEquals("Validation failed", 0, result.length()); //$NON-NLS-1$
	}

	protected String assertValidationPresent(String value, AttributeDefinition ad) {
		String result = ad.validate(value);
		Assert.assertNotNull("No validation was present", result); //$NON-NLS-1$
		return result;
	}

	protected String escape(String value) {
		if (value == null || value.length() == 0) {
			return value;
		}
		StringBuffer result = new StringBuffer(value.length() + 20);
		for (int i = 0; i < value.length(); i++) {
			char c = value.charAt(i);
			if (c == ',' || c == '\\' || Character.isWhitespace(c)) {
				result.append('\\');
			}
			result.append(c);
		}
		return result.toString();
	}

	protected AttributeDefinition findAttributeDefinitionById(String id, AttributeDefinition[] ads) {
		if (id == null || ads == null)
			return null;
		for (int i = 0; i < ads.length; i++) {
			if (ads[i] == null)
				continue;
			if (id.equals(ads[i].getID())) {
				return ads[i];
			}
		}
		return null;
	}

	protected String getFirstDefaultValue(String[] defaultValue) {
		if (defaultValue == null || defaultValue.length == 0)
			return null;
		return defaultValue[0];
	}

	@Before
	public void setUp() throws Exception {
		Activator.getBundle(Activator.BUNDLE_METATYPE).start();
		metaTypeReference = Activator.getBundleContext().getServiceReference(EquinoxMetaTypeService.class);
		Assert.assertNotNull("Metatype service reference not found", metaTypeReference); //$NON-NLS-1$
		metatype = Activator.getBundleContext().getService(metaTypeReference);
		Assert.assertNotNull("Metatype service not found", metatype); //$NON-NLS-1$
		bundleInstaller = new BundleInstaller("bundle_tests/metatype", Activator.getBundleContext()); //$NON-NLS-1$
	}

	@After
	public void tearDown() throws Exception {
		bundleInstaller.shutdown();
		Activator.getBundleContext().ungetService(metaTypeReference);
		Activator.getBundle(Activator.BUNDLE_METATYPE).stop();
	}
}

Back to the top