Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e178c31ca01837dc7295d18243c7d0290de49ef8 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ua.tests.intro.parser;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ua.tests.intro.util.IntroModelSerializer;
import org.eclipse.ua.tests.intro.util.IntroModelSerializerTest;
import org.eclipse.ua.tests.plugin.UserAssistanceTestPlugin;
import org.eclipse.ua.tests.util.FileUtil;
import org.eclipse.ua.tests.util.ResourceFinder;
import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager;
import org.osgi.framework.Bundle;

/*
 * Tests the intro parser on valid intro content.
 */
public class PlatformTest extends TestCase {
	
	private static final String SERIALIZED_PATH = "data/intro/platform/serialized.txt";
	
	/*
	 * Returns an instance of this Test.
	 */
	public static Test suite() {
		return new TestSuite(PlatformTest.class);
	}
	
	/*
	 * Test the platform's parsed intro content.
	 */
	public void testModel() {
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.config");
		Assert.assertEquals("The platform did not have the expected number of \"org.eclipse.ui.intro.config\" extensions.", 1, elements.length);
		
		IConfigurationElement element = elements[0];
		
		String pluginRoot = ResourceFinder.findFile(UserAssistanceTestPlugin.getDefault(), "/").toString().substring("file:".length());
		String content = element.getAttribute("content");
		String id = element.getAttribute("id");
		String resultFile = pluginRoot + SERIALIZED_PATH;

		IntroModelRoot model = ExtensionPointManager.getInst().getModel(id);
		IntroModelSerializer serializer = new IntroModelSerializer(model);
		
		try {
			String expected = FileUtil.getContents(resultFile);
			String actual = serializer.toString();
			StringTokenizer tok1 = new StringTokenizer(expected, "\n");
			StringTokenizer tok2 = new StringTokenizer(actual, "\n");
			
			/*
			 * Report the line number and line text where it didn't match,
			 * as well as the extension id and expected results file.
			 */
			int tokenNumber = 0;
			while (tok1.hasMoreTokens() && tok2.hasMoreTokens()) {
				String a = tok1.nextToken();
				String b = tok2.nextToken();
				Assert.assertEquals("Serialized intro content model text for \"" + id + "\" did not match expected result (" + IntroModelSerializerTest.getResultFile(content) + "). First difference occured on token " + tokenNumber + ".", a, b);
				++tokenNumber;
			}
		}
		catch(Exception e) {
			Assert.fail("An error occured while loading expected result file for intro at: " + resultFile);
		}
	}
	
	/*
	 * Some extensions run samples that involve executing code. Check to make sure
	 * that the classes exist and can be instantiated.
	 */
	public void testClasses() {
		String pluginRoot = ResourceFinder.findFile(UserAssistanceTestPlugin.getDefault(), "/").toString().substring("file:".length());
		String resultFile = pluginRoot + SERIALIZED_PATH;
		
		try {
			String contents = FileUtil.getContents(resultFile);
			StringTokenizer tok = new StringTokenizer(contents);
			while (tok.hasMoreTokens()) {
				String next = tok.nextToken();
				if (next.startsWith("http://org.eclipse.ui.intro/runAction?")) {
					Map map = createMap(next.substring("http://org.eclipse.ui.intro/runAction?".length()));
					Assert.assertTrue("The runAction was missing the class attribute: " + next, map.containsKey("class"));
					Assert.assertTrue("The runAction was missing the pluginId attribute: " + next, map.containsKey("pluginId"));
					
					String clazz = (String)map.get("class");
					String pluginId = (String)map.get("pluginId");
					
					Bundle bundle = Platform.getBundle(pluginId);
					Assert.assertNotNull("The plugin referenced in one of the platform's intro runAction URLs (" + next + ") was not found: " + pluginId, bundle);
					
					try {
						Class c = bundle.loadClass(clazz);
						c.newInstance();
					}
					catch (ClassNotFoundException e) {
						Assert.fail("One of the classes in the platform's intro runActions URLs was not found: " + clazz + " in plugin: " + pluginId);
					}
					catch (InstantiationException e) {
						Assert.fail("One of the classes in the platform's intro runActions URLs could not be instantiated: " + clazz + " in plugin: " + pluginId);
					}
					catch (IllegalAccessException e) {
						Assert.fail("One of the classes in the platform's intro runActions URLs could not be accessed (is it public?): " + clazz + " in plugin: " + pluginId);
					}
				}
			}
		}
		catch (IOException e) {
			Assert.fail("An IOException occured while reading platform's serialized.txt file");
		}
	}
	
	/*
	 * Generates a map from the given string of the form:
	 * "key1=value1&key2=value2&..."  (i.e. URL parameters)
	 */
	private static Map createMap(String args) {
		Map map = new HashMap();
		StringTokenizer tok2 = new StringTokenizer(args, "&");
		while (tok2.hasMoreTokens()) {
			String arg = tok2.nextToken();
			int separator = arg.indexOf('=');
			map.put(arg.substring(0, separator), arg.substring(separator + 1));
		}
		return map;
	}
}

Back to the top