1 /*******************************************************************************
2  * Copyright (c) 2004, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.tests.dynamicplugins;
15 
16 import java.util.Iterator;
17 
18 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
19 import org.eclipse.ui.views.markers.internal.MarkerGroup;
20 import org.eclipse.ui.views.markers.internal.MarkerSupportRegistry;
21 import org.eclipse.ui.views.markers.internal.ProblemFilter;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /**
27  * Test the loading and unloading of the marker support components.
28  *
29  * @since 3.2
30  */
31 @RunWith(JUnit4.class)
32 public class MarkerSupportTests extends DynamicTestCase {
33 
34 	public static final String FILTER1 = "filter1";
35 
36 	public static final String FILTER2 = "filter2";
37 
38 	public static final String FILTER3 = "filter3";
39 
40 	public static final String DYNAMIC_CATEGORY = "dynamicCategory";
41 
42 	static final String DYNAMIC_PROBLEM_MARKER = "org.eclipse.ui.tests.dynamicTestMarker";
43 
44 	static final String PROBLEM_MARKER = "org.eclipse.core.resources.problemmarker";
45 
46 	/**
47 	 * @param testName
48 	 */
MarkerSupportTests()49 	public MarkerSupportTests() {
50 		super(MarkerSupportTests.class.getSimpleName());
51 	}
52 
53 	@Test
testFilters()54 	public void testFilters() {
55 		assertFalse(hasFilter(FILTER1));
56 		assertFalse(hasFilter(FILTER2));
57 		assertFalse(hasFilter(FILTER3));
58 		getBundle();
59 		assertTrue(hasFilter(FILTER1));
60 		assertTrue(hasFilter(FILTER2));
61 		assertTrue(hasFilter(FILTER3));
62 		removeBundle();
63 		assertFalse(hasFilter(FILTER1));
64 		assertFalse(hasFilter(FILTER2));
65 		assertFalse(hasFilter(FILTER3));
66 	}
67 
68 	@Test
testMarkerGroup()69 	public void testMarkerGroup() {
70 		assertFalse(hasMarkerGroup());
71 		getBundle();
72 		assertTrue(hasMarkerGroup());
73 		removeBundle();
74 		assertFalse(hasMarkerGroup());
75 	}
76 
77 	@Test
testCategories()78 	public void testCategories() {
79 		assertFalse(hasCategory());
80 		getBundle();
81 		assertTrue(hasCategory());
82 		removeBundle();
83 		assertFalse(hasCategory());
84 	}
85 
86 	@Test
testHierarchies()87 	public void testHierarchies() {
88 		assertFalse(hasHierarchy());
89 		getBundle();
90 		assertTrue(hasHierarchy());
91 		removeBundle();
92 		assertFalse(hasHierarchy());
93 	}
94 
95 	@Override
getMarkerClass()96 	protected String getMarkerClass() {
97 		return "org.eclipse.ui.dynamic.markerSupport.DynamicTestsSubCategoryProvider";
98 	}
99 
100 	/**
101 	 * Return whether or not there is a hierarchy for the dynamic type or if it
102 	 * is using the default.
103 	 *
104 	 * @return
105 	 */
hasHierarchy()106 	private boolean hasHierarchy() {
107 		return MarkerSupportRegistry.getInstance().getSorterFor(
108 				DYNAMIC_PROBLEM_MARKER) != MarkerSupportRegistry.getInstance()
109 				.getSorterFor(PROBLEM_MARKER);
110 	}
111 
hasMarkerGroup()112 	private boolean hasMarkerGroup() {
113 		Iterator<MarkerGroup> groups = MarkerSupportRegistry.getInstance()
114 		.getMarkerGroups().iterator();
115 
116 		while (groups.hasNext()) {
117 			MarkerGroup element = groups.next();
118 			if(element.getField().getDescription().equals("Dynamic Test Grouping")) {
119 				return true;
120 			}
121 		}
122 
123 		return false;
124 	}
125 
126 	/**
127 	 * Return whether or not there is a filter for the dynamic category
128 	 *
129 	 * @return
130 	 */
hasCategory()131 	private boolean hasCategory() {
132 		return MarkerSupportRegistry.getInstance().getCategory(
133 				DYNAMIC_PROBLEM_MARKER) != null;
134 	}
135 
136 	/**
137 	 * Return whether or not there is a filter for id.
138 	 *
139 	 * @param id
140 	 * @return
141 	 */
hasFilter(String id)142 	private boolean hasFilter(String id) {
143 		Iterator<ProblemFilter> filters = MarkerSupportRegistry.getInstance()
144 				.getRegisteredFilters().iterator();
145 		while (filters.hasNext()) {
146 			ProblemFilter filter = filters.next();
147 			if (id.equals(filter.getId())) {
148 				return true;
149 			}
150 		}
151 		return false;
152 	}
153 
154 	@Override
getExtensionId()155 	protected String getExtensionId() {
156 		return "newProblemFilter.testDynamicFilterAddition";
157 	}
158 
159 	@Override
getExtensionPoint()160 	protected String getExtensionPoint() {
161 		return MarkerSupportRegistry.MARKER_SUPPORT;
162 	}
163 
164 	@Override
getInstallLocation()165 	protected String getInstallLocation() {
166 		return "data/org.eclipse.newMarkerSupport";
167 	}
168 
169 	@Override
getDeclaringNamespace()170 	protected String getDeclaringNamespace() {
171 		return IDEWorkbenchPlugin.IDE_WORKBENCH;
172 	}
173 
174 }
175