Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 687f1fd7884db1b350f9dce1a09a195f09087cd1 (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
/*******************************************************************************
 * Copyright (c) 2016 COSEDA Technologies GmbH
 * 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:
 *     Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation 
 *
 *******************************************************************************/
package org.eclipse.cdt.codan.checkers.ui.quickfix;

import static org.junit.Assert.*;

import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateNewClass;
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;

import java.util.function.Function;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;

@RunWith(MockitoJUnitRunner.class)
public class QuickFixCreateNewClassTest {

	QuickFixCreateNewClass qut;

	@Mock
	IMarker marker;

	/**
	 * TranslationUnit of the marker's resource which is received from the
	 * workspace.
	 */
	@Mock
	ITranslationUnit translationUnitViaWorkspace;

	/**
	 * TranslationUnit of the marker's resource if there is an open editor.
	 */
	@Mock
	ITranslationUnit translationUnitWorkingCopy;

	/**
	 * Function to get the working copy of translationUnitViaWorkspace
	 */
	@Mock
	Function<ITranslationUnit, ITranslationUnit> toWorkingCopy;

	/**
	 * Language of the TranslationUnit
	 */
	@Mock
	ILanguage translationUnitLanguage;

	@Before
	public void setUp() throws Exception {
		qut = new QuickFixCreateNewClass(toWorkingCopy) {
			@Override
			protected ITranslationUnit getTranslationUnitViaWorkspace(IMarker marker) {
				if (QuickFixCreateNewClassTest.this.marker == marker) {
					return translationUnitViaWorkspace;
				}
				throw new RuntimeException("Invalid marker");
			};
		};

		when(toWorkingCopy.apply(translationUnitViaWorkspace)).thenReturn(translationUnitWorkingCopy);
		when(translationUnitWorkingCopy.getLanguage()).thenReturn(translationUnitLanguage);
	}

	/**
	 * Test if the marker is applicable if
	 * {@link AbstractCodanCMarkerResolution#getTranslationUnitViaWorkspace}
	 * returns null.
	 */
	@Test
	public void isApplicableForUnresolvableMarker() {
		translationUnitViaWorkspace = null;
		assertTrue("Unresolvable marker is not applicable", qut.isApplicable(marker));
	}

	/**
	 * Test if the marker is applicable if
	 * {@link AbstractCodanCMarkerResolution#getTranslationUnitViaWorkspace}
	 * returns a marker valid marker.
	 */
	@Test
	public void isApplicableForResolvableMarker() {
		assertTrue("Resolvable marker is not applicable", qut.isApplicable(marker));
	}

	/**
	 * Test if the marker is applicable if
	 * <code>tu.getLanguage().getLinkageID() == ILinkage.C_LINKAGE_ID</code>
	 */
	@Test
	public void isNotApplicableForResolvableMarkerWithCLinkage() {
		when(translationUnitLanguage.getLinkageID()).thenReturn(ILinkage.C_LINKAGE_ID);
		assertFalse("Resolvable marker with ILinkage.C_LINKAGE_ID must not be applicable", qut.isApplicable(marker));
	}

	/**
	 * Test if the marker is applicable if <code>tu.getLanguage()</code> throws
	 * a {@link CoreException}.
	 * 
	 * @throws CoreException
	 */
	@Test
	public void isApplicableForResolvableMarkerWithCLinkageResolutionCoreException() throws CoreException {
		CoreException coreException = new CoreException(mock(IStatus.class));
		doThrow(coreException).when(translationUnitWorkingCopy).getLanguage();
		assertTrue("Resolvable marker with invalid language must be applicable", qut.isApplicable(marker));
	}
}

Back to the top