Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83d6e9b7e3521b15d0cf114e9e0617f28665b61f (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
/*******************************************************************************
 * Copyright (c) 2013 Florian Thienel 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:
 * 		Florian Thienel - initial API and implementation
 * 		Carsten Hiesserich - additional tests 
 *******************************************************************************/
package org.eclipse.vex.ui.internal.config.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.vex.ui.internal.config.ConfigEvent;
import org.eclipse.vex.ui.internal.config.ConfigLoaderJob;
import org.eclipse.vex.ui.internal.config.ConfigSource;
import org.eclipse.vex.ui.internal.config.ConfigurationLoader;
import org.eclipse.vex.ui.internal.config.ConfigurationRegistry;
import org.eclipse.vex.ui.internal.config.ConfigurationRegistryImpl;
import org.eclipse.vex.ui.internal.config.DocumentType;
import org.eclipse.vex.ui.internal.config.IConfigListener;
import org.eclipse.vex.ui.internal.config.PluginProject;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

/**
 * @author Florian Thienel
 */
public class ConfigurationRegistryTest {

	private ConfigurationRegistry registry;

	@Rule
	public TestName name = new TestName();

	@After
	public void disposeRegistry() {
		if (registry != null) {
			registry.dispose();
		}
		registry = null;
	}

	@Test
	public void notAutomaticallyLoaded() throws Exception {
		registry = new ConfigurationRegistryImpl(new MockConfigurationLoader());
		assertFalse(registry.isLoaded());
	}

	@Test
	public void fireLoadedEventOnLoad() throws Exception {
		registry = new ConfigurationRegistryImpl(new MockConfigurationLoader());
		final MockConfigListener configListener = new MockConfigListener();
		registry.addConfigListener(configListener);
		registry.loadConfigurations();
		assertTrue(registry.isLoaded());
		assertTrue(configListener.loaded);
		assertFalse(configListener.changed);
	}

	@Test
	public void loadNewPluginProjectAndFireChangedEvent() throws Exception {
		registry = new ConfigurationRegistryImpl(new MockConfigurationLoader());
		final MockConfigListener configListener = new MockConfigListener();
		registry.addConfigListener(configListener);
		registry.loadConfigurations();
		configListener.reset();
		final IProject project = PluginProjectTest.createVexPluginProject(name.getMethodName());
		assertFalse(configListener.loaded);
		assertTrue(configListener.changed);
		assertNotNull(registry.getPluginProject(project));
	}

	@Test
	public void reloadModifiedPluginProjectAndFireConfigChangedEvent() throws Exception {
		registry = new ConfigurationRegistryImpl(new MockConfigurationLoader());
		registry.loadConfigurations();
		final IProject project = PluginProjectTest.createVexPluginProject(name.getMethodName());
		final MockConfigListener configListener = new MockConfigListener();
		project.getFile("plugintest2.css").create(new ByteArrayInputStream(new byte[0]), true, null);
		final String fileContent = PluginProjectTest.createVexPluginFileContent(project, "plugintest.dtd", "plugintest.css", "plugintest2.css");
		registry.addConfigListener(configListener);
		project.getFile(PluginProject.PLUGIN_XML).setContents(new ByteArrayInputStream(fileContent.getBytes()), true, true, null);
		assertFalse(configListener.loaded);
		assertTrue(configListener.changed);
		assertNotNull(registry.getPluginProject(project).getItemForResource(project.getFile("plugintest2.css")));
	}

	@Ignore("I don't understand why it fails...WTF???")
	@Test
	public void removeDeletedPluginProjectAndFireConfigChangedEvent() throws Exception {
		registry = new ConfigurationRegistryImpl(new MockConfigurationLoader());
		registry.loadConfigurations();
		final IProject project = PluginProjectTest.createVexPluginProject(name.getMethodName());
		final MockConfigListener configListener = new MockConfigListener();
		registry.addConfigListener(configListener);
		project.getFile("plugintest.css").delete(true, null);
		assertTrue(configListener.changed);
		assertNotNull(registry.getPluginProject(project));
		configListener.reset();
		project.getFile("plugintest.dtd").delete(true, null);
		assertTrue(configListener.changed);
		assertNotNull(registry.getPluginProject(project));
		configListener.reset();
		project.getFile(PluginProject.PLUGIN_XML).delete(true, null);
		assertTrue(configListener.changed);
		assertNotNull(registry.getPluginProject(project));
	}

	@Test
	public void testPluginDoctypeDefinition() throws Exception {
		final ConfigurationRegistry configurationRegistry = new ConfigurationRegistryImpl(new ConfigLoaderJob());
		configurationRegistry.loadConfigurations();

		final DocumentType doctype = configurationRegistry.getDocumentType("-//Vex//DTD Test//EN", null);
		assertNotNull(doctype);
		assertEquals("test.dtd", doctype.getSystemId());
	}

	@Test
	public void testPluginNamespaceDefinition() throws Exception {
		final ConfigurationRegistry configurationRegistry = new ConfigurationRegistryImpl(new ConfigLoaderJob());
		configurationRegistry.loadConfigurations();

		final DocumentType doctype = configurationRegistry.getDocumentType("http://org.eclipse.vex/namespace", null);
		assertNotNull(doctype);
		assertEquals("test schema doctype", doctype.getName());
	}

	@Test
	public void testGetDocumentTypesWithStyles() throws Exception {
		final ConfigurationRegistry configurationRegistry = new ConfigurationRegistryImpl(new ConfigLoaderJob());
		configurationRegistry.loadConfigurations();

		final DocumentType[] doctypes = configurationRegistry.getDocumentTypesWithStyles();
		boolean dtdFound = false;
		boolean schemaFound = false;
		for (final DocumentType doctype : doctypes) {
			if ("test doctype".equals(doctype.getName())) {
				dtdFound = true;
			}
			if ("test schema doctype".equals(doctype.getName())) {
				schemaFound = true;
			}
		}
		assertTrue("DoctypeWithStyles should return Doctype with DTD ", dtdFound);
		assertTrue("DoctypeWithStyles should return Docype with namespace ", schemaFound);
	}

	private static class MockConfigurationLoader implements ConfigurationLoader {
		private final List<ConfigSource> loadedConfigSources;

		public MockConfigurationLoader() {
			this(Collections.<ConfigSource> emptyList());
		}

		public MockConfigurationLoader(final List<ConfigSource> loadedConfigSources) {
			this.loadedConfigSources = loadedConfigSources;
		}

		public void load(final Runnable whenDone) {
			whenDone.run();
		}

		public boolean isLoading() {
			return false;
		}

		public List<ConfigSource> getLoadedConfigSources() {
			return loadedConfigSources;
		}

		public void join() throws InterruptedException {
			return;
		}
	}

	private static class MockConfigListener implements IConfigListener {
		public boolean changed = false;
		public boolean loaded = false;

		public void configChanged(final ConfigEvent e) {
			changed = true;
		}

		public void configLoaded(final ConfigEvent e) {
			loaded = true;
		}

		public void reset() {
			changed = false;
			loaded = false;
		}
	}
}

Back to the top