Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d42dabab842090aa3180c6e01c5042e0e797ad3 (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
/*******************************************************************************
 * Copyright (c) 2009, 2015 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.help.remote;

import java.util.HashSet;
import junit.framework.TestCase;

import org.eclipse.help.AbstractTocProvider;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.remote.RemoteTocProvider;
import org.eclipse.help.internal.toc.TocContribution;
import org.eclipse.help.internal.toc.TocFileProvider;

public class TocManagerTest extends TestCase {
	
	private int mode;

	@Override
	protected void setUp() throws Exception {
		BaseHelpSystem.ensureWebappRunning();
		mode = BaseHelpSystem.getMode();
		RemotePreferenceStore.savePreferences();
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
	}
	
	@Override
	protected void tearDown() throws Exception {
		BaseHelpSystem.setMode(mode);
		RemotePreferenceStore.restorePreferences();
	}

	public void testDuplicatesOneRemote() throws Exception {
		
		RemotePreferenceStore.setMockRemoteServer();
		HelpPlugin.getTocManager().clearCache();
		boolean hasDuplicates=hasDuplicateContributions(HelpPlugin.getTocManager().getTocContributions("en"));
		assertFalse(hasDuplicates);
	}
	
	public void testDuplicatesTwoRemote() throws Exception {
		
		RemotePreferenceStore.setTwoMockRemoteServers();
		HelpPlugin.getTocManager().clearCache();
		boolean hasDuplicates=hasDuplicateContributions(HelpPlugin.getTocManager().getTocContributions("en"));
		assertFalse(hasDuplicates);
	}
	
	public void testLocalProviderPriority() throws Exception {
		
		int localPriority=0,remotePriority=0;
		RemotePreferenceStore.setMockRemoteServer();
		RemotePreferenceStore.setMockLocalPriority();
		HelpPlugin.getTocManager().clearCache();
		AbstractTocProvider [] tocProviders = HelpPlugin.getTocManager().getTocProviders();
		for (AbstractTocProvider tocProvider : tocProviders) {
			if(tocProvider instanceof TocFileProvider)
				localPriority = tocProvider.getPriority();
			
			if(tocProvider instanceof RemoteTocProvider)
				remotePriority = tocProvider.getPriority();
		}
		
		assertTrue(localPriority<remotePriority);
	}
	
	public void testRemoteProviderPriority() throws Exception {
		
		RemotePreferenceStore.setMockRemoteServer();
		RemotePreferenceStore.setMockRemotePriority();
		HelpPlugin.getTocManager().clearCache();
		int localPriority=0,remotePriority=0;
		
		AbstractTocProvider [] tocProviders = HelpPlugin.getTocManager().getTocProviders();
		for (AbstractTocProvider tocProvider : tocProviders) {
			if(tocProvider instanceof TocFileProvider)
				localPriority = tocProvider.getPriority();
			
			if(tocProvider instanceof RemoteTocProvider)
				remotePriority = tocProvider.getPriority();
		}
		
		assertTrue(remotePriority<localPriority);
	}
	
	public static boolean hasDuplicateContributions(TocContribution[] tocContributions)
	{
		HashSet<String> contributionsFound = new HashSet<String>();
		
		for (TocContribution tocContribution : tocContributions) {
			if(contributionsFound.contains(tocContribution.getId()))
				return true;
			else
				contributionsFound.add(tocContribution.getId());
		}
		
		return false;
	}
}

Back to the top