Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 479f6d12ff8cac37e55e6431198989da69a6953c (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.bundles.tests;

import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

import org.eclipse.osgi.framework.internal.core.BundleFragment;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.internal.core.feature.Feature;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.Bundle;

public class BundlesTests {

	//Transform the version number to the regex format
	//Adds .* (Valid version numbers are e.g. 0.10.1.qualifier)
	private static final String REGEX_VERSION_NUMBER = BundleTestsUtils.PAPYRUS_VERSION.replaceAll("\\.", "\\\\.") + "\\..*"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

	private static final String REGEX_INCUBATION = ".*\\(Incubation\\)"; //$NON-NLS-1$

	@Test
	public void featureVersionNumberTest() {
		String message = null;
		int nbProblem = 0;
		final List<Feature> features = BundleTestsUtils.getPapyrusFeature();
		for(final Feature feature : features) {
			String version = feature.getVersion();
			if(!version.matches(REGEX_VERSION_NUMBER)) {
				if(message == null) {
					message = "Wrong version number for the features:";
				}
				message += "\n" + feature.getId();
				nbProblem++;
			}
		}
		Assert.assertNull(nbProblem + "problems!\n" + message, message);
	}

	/**
	 * Tests that all Papyrus Bundle name are finished by {@link #INCUBATION}
	 */
	@Test
	public void incubationTest() {
		testManifestProperty(BundleTestsUtils.BUNDLE_NAME, REGEX_INCUBATION, false, false);
	}

	/**
	 * Tests the provider name (should be EL
	 */
	@Test
	public void vendorTest() {
		testManifestProperty(BundleTestsUtils.BUNDLE_VENDOR, BundleTestsUtils.VENDOR_NAME, false, false);
	}

	/**
	 * Tests that each papyrus plugins have the correct version
	 */
	@Test
	public void versionTest() {
		testManifestProperty(BundleTestsUtils.BUNDLE_VERSION, REGEX_VERSION_NUMBER, false, false);
	}

	/**
	 * Tests if the file about.html is included to the plugin
	 */
	@Test
	public void aboutTest() {
		fileTest("/about.html"); //$NON-NLS-1$
	}

	/**
	 * Tests the java version
	 */
	@Test
	public void javaVersionTest() {
		testManifestProperty(BundleTestsUtils.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, BundleTestsUtils.JAVA_VERSION_REGEX, false, true);
	}

	/**
	 * Tests that we don't use import package
	 */
	@Test
	public void importPackage() {
		testManifestProperty(BundleTestsUtils.BUNDLE_IMPORT_PACKAGE, "", true, false); //$NON-NLS-1$
	}

	/**
	 * This test verify that we doesn't re-export dependencies
	 */
	@Test
	public void reexportDependencies() {
		String message = null;
		int nb = 0;
		for(final Bundle current : BundleTestsUtils.getPapyrusBundles()) {
			final String value = current.getHeaders().get(BundleTestsUtils.REQUIRE_BUNDLE);
			if(value == null) {
				continue;
			}
			final String[] bundles = value.split(","); //$NON-NLS-1$
			String localMessage = null;
			for(final String bundle : bundles) {
				if(bundle.contains("visibility:=reexport")) { //$NON-NLS-1$
					nb++;
					if(localMessage == null) {
						localMessage = NLS.bind("{0} re-exports:", current.getSymbolicName()); //$NON-NLS-1$
					}
					if(bundle.contains(";")) { //$NON-NLS-1$
						localMessage += NLS.bind("\n  - {0}", bundle.substring(0, bundle.indexOf(";"))); //$NON-NLS-1$ //$NON-NLS-2$
					} else {
						localMessage += NLS.bind("\n  - {0}", bundle); //$NON-NLS-1$ 
					}
				}
			}
			if(localMessage != null) {
				if(message == null) {
					message = ""; //$NON-NLS-1$
				}
				message += localMessage + "\n"; //$NON-NLS-1$
			}
		}
		Assert.assertNull(nb + " problems!", message); //$NON-NLS-1$
	}

	/**
	 * Tests if a the value of a property in the Manifest is correct
	 * 
	 * @param property
	 *        the property to test
	 * @param regex
	 *        the regular expression to test the property
	 * @param mustBeNull
	 *        indicates that the value for the property must be <code>null</code>
	 * @param onlyOnJavaProject
	 *        boolean indicating if the tests should only be done on JavaProject
	 */
	private void testManifestProperty(final String property, final String regex, final boolean mustBeNull, final boolean onlyOnJavaProject) {
		String message = null;
		int nb = 0;
		for(final Bundle current : BundleTestsUtils.getPapyrusBundles()) {
			if(onlyOnJavaProject && !BundleTestsUtils.isJavaProject(current)) {
				continue; //useful for oep.infra.gmfdiag.css.theme for example
			}
			final String value = current.getHeaders().get(property);
			boolean result = false;
			if(mustBeNull) {
				result = (value == null);
			} else if(value != null) {
				result = value.matches(regex);
			}
			if(!result) {
				if(message == null) {
					message = "Wrong " + property + " for :"; //$NON-NLS-1$ //$NON-NLS-2$
				}
				message += "\n "; //$NON-NLS-1$
				message += current.getSymbolicName();
				nb++;
			}
		}
		Assert.assertNull(nb + " problems!", message); //$NON-NLS-1$
	}

	/**
	 * Tests if the file is owned by the bundle
	 * 
	 * @param filepath
	 *        the file path
	 */
	private void fileTest(final String filepath) {
		StringBuffer buffer = new StringBuffer();
		int nb = 0;
		for(final Bundle current : BundleTestsUtils.getPapyrusBundles()) {
			URL url = current.getEntry(filepath);
			if(url == null) {
				if(buffer.length() == 0) {
					buffer.append(NLS.bind("The following bundles don't have the file {0}.", filepath)); //$NON-NLS-1$
				}
				buffer.append("\n");//$NON-NLS-1$
				buffer.append(current.getSymbolicName());
				nb++;
			}
		}
		StringBuffer errorMessage = new StringBuffer();
		errorMessage.append(nb);
		errorMessage.append(" problems!\n"); //$NON-NLS-1$
		errorMessage.append(buffer.toString());
		Assert.assertTrue(errorMessage.toString(), buffer.toString().isEmpty());
	}

	/**
	 * We want that all Papyrus dependencies in the Papyrus plugin will be define
	 */
	@Test
	public void papyrusDependencyVersionTest() {
		String message = null;
		int nb = 0;
		for(final Bundle current : BundleTestsUtils.getPapyrusBundles()) {
			final String value = current.getHeaders().get(BundleTestsUtils.REQUIRE_BUNDLE);
			if(value == null) {
				continue;
			}
			final String[] bundles = value.split(","); //$NON-NLS-1$
			String localMessage = null;
			for(final String bundle : bundles) {
				if(bundle.contains("org.eclipse.papyrus")) { //$NON-NLS-1$
					if(!bundle.contains("bundle-version=" + '"' + BundleTestsUtils.PAPYRUS_VERSION + '"')) { //$NON-NLS-1$ 
						nb++;
						if(localMessage == null) {
							localMessage = NLS.bind("{0} incorrect required bundle-version:", current.getSymbolicName()); //$NON-NLS-1$
						}
						if(bundle.contains(";")) { //$NON-NLS-1$
							localMessage += NLS.bind("\n  - {0}", bundle.substring(0, bundle.indexOf(";"))); //$NON-NLS-1$ //$NON-NLS-2$
						} else {
							localMessage += NLS.bind("\n  - {0}", bundle); //$NON-NLS-1$ 
						}
					}
				}
			}
			if(localMessage != null) {
				if(message == null) {
					message = ""; //$NON-NLS-1$
				}
				message += localMessage + "\n"; //$NON-NLS-1$
			}
		}
		Assert.assertNull(nb + " problems!", message); //$NON-NLS-1$
	}

	/**
	 * This test verify that the plugin contains pdoc file
	 */
	@Test
	public void documentationFileTest() {
		String message = null;
		int nb = 0;
		for(final Bundle bundle : BundleTestsUtils.getPapyrusBundles()) {
			final URL res = bundle.getResource("plugin.pdoc"); //$NON-NLS-1$
			if(res == null) {
				nb++;
				if(message == null) {
					message = "No file plugin.pdoc found for:"; //$NON-NLS-1$
				}
				message += NLS.bind("\n  - {0}", bundle.getSymbolicName()); //$NON-NLS-1$
			}
		}
		Assert.assertNull(nb + " problems! " + message, message); //$NON-NLS-1$
	}

	/**
	 * verify that the field PLUGIN_ID is equals to the plugin name
	 */
	@Test
	public void pluginIDTest() {
		String errorMessage = ""; //$NON-NLS-1$.
		String warningMessage = "";//$NON-NLS-1$ 
		final Collection<String> possibleIds = new ArrayList<String>();
		possibleIds.add("ID");//$NON-NLS-1$ 
		possibleIds.add("PLUGIN_ID");//$NON-NLS-1$ 
		int nbError = 0;
		int nbWarning = 0;
		for(final Bundle current : BundleTestsUtils.getPapyrusBundles()) {
			if(!BundleTestsUtils.isJavaProject(current)) {
				continue; //useful for oep.infra.gmfdiag.css.theme for example
			}
			final String activator = current.getHeaders().get("Bundle-Activator"); //$NON-NLS-1$
			if(activator != null) {
				try {
					final Class<?> activatorClass = current.loadClass(activator);
					Field plugin_id_field = null;
					for(final Field currentField : activatorClass.getFields()) {
						final String fieldName = currentField.getName();
						if(possibleIds.contains(fieldName)) {
							plugin_id_field = currentField;
							break;
						}
					}
					if(plugin_id_field != null) {
						final String plugin_id = (String)plugin_id_field.get(activatorClass);
						if(!plugin_id.equals(current.getSymbolicName())) {
							nbError++;
							errorMessage += NLS.bind("The field PLUGIN_ID of the plugin {0} is not equals to the plugin name.\n", current.getSymbolicName()); //$NON-NLS-1$
						}
					} else {
						//Never happens. An exception is thrown.
						nbWarning++;
						warningMessage += NLS.bind("The activator of {0} has no field named PLUGIN_ID.\n", current.getSymbolicName()); //$NON-NLS-1$
					}
				} catch (final Exception e) {
					errorMessage += NLS.bind("Exception occured with the plugin {0} \n {1} \n", new Object[]{ current.getSymbolicName(), e }); //$NON-NLS-1$
				}
			}

		}
		Assert.assertTrue(nbError + " problems! " + errorMessage, nbError == 0); //$NON-NLS-1$

		//Do not fail on warnings
		//Assert.assertTrue(nbWarning + "warning!" + warningMessage, nbWarning == 0);//$NON-NLS-1$ 
	}


}

Back to the top