Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef970f92f07837b5d02b3a179872fc916bff57ec (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Kiril Mitov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.tests.internal.util;

import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import java.net.URI;

import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.platform.IDiagramEditor;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.TypeMapping;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IEclipseFacade;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPASolver;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings({"unused", "nls"})
public class JPASolverTest {

	private IEclipseFacade eclipseFacade;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		eclipseFacade = createEclipseFacade();
	}

	@Test
	public void testResourceListenerRegistered() {
		IWorkspace workspace = EasyMock.createMock(IWorkspace.class);
		workspace.addResourceChangeListener(isA(IResourceChangeListener.class), eq(IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_DELETE | IResourceChangeEvent.POST_BUILD));
		replay(workspace);
		IEclipseFacade facade = EasyMock.createMock(IEclipseFacade.class);
		expect(facade.getWorkspace()).andStubReturn(workspace);
		replay(facade);
		new JPASolver(facade, null);
		verify(workspace, facade);
	}

	private IFile replayFile() {
		IFile file = EasyMock.createMock(IFile.class);
		URI uri = URI.create("file://project//aaa");
		expect(file.getLocationURI()).andStubReturn(uri);
		expect(file.exists()).andStubReturn(true);
		IProject proj = EasyMock.createMock(IProject.class);
		expect(proj.getType()).andStubReturn(IResource.PROJECT);
		expect(file.getType()).andStubReturn(IResource.FILE);
		expect(file.getFullPath()).andStubReturn(new Path("C:\\project\\aaa"));
		IFile clsPath = EasyMock.createMock(IFile.class); 
		expect(proj.getFile(".classpath")).andStubReturn(clsPath);
		try {
			expect(proj.hasNature("org.eclipse.jdt.core.javanature")).andStubReturn(true);
		} catch (CoreException e) {}
		expect(file.getProject()).andStubReturn(proj);
		replay(file, proj);
		return file;
	}

	private PersistentType createJptForResource(IFile file, String name) {
		PersistentType jpt = EasyMock.createNiceMock(PersistentType.class);
		JpaProject jpaProject = EasyMock.createNiceMock(JpaProject.class);
		TypeMapping m = EasyMock.createNiceMock(TypeMapping.class);
		expect(jpt.getResource()).andStubReturn(file);
		expect(jpt.getJpaProject()).andStubReturn(jpaProject);
		expect(jpt.getMapping()).andStubReturn(m);
		if (name != null)
			expect(jpt.getName()).andStubReturn(name);
		replay(jpt, jpaProject);
		return jpt;
	}

	private IEclipseFacade createEclipseFacade() {
		IEclipseFacade facade = EasyMock.createMock(IEclipseFacade.class);
		return facade;
	}

	private void configureForWorkspace(IEclipseFacade facade) {
		IWorkspace workspace = EasyMock.createMock(IWorkspace.class);
		workspace.addResourceChangeListener(isA(IResourceChangeListener.class), eq(IResourceChangeEvent.POST_BUILD));
		replay(workspace);
		expect(facade.getWorkspace()).andStubReturn(workspace);
	}

	private IJPAEditorFeatureProvider configureRefreshEditorProvider() {
		IDiagramEditor editor = EasyMock.createMock(IDiagramEditor.class);
		editor.refresh();
		replay(editor);
		IDiagramTypeProvider diagramProvider = EasyMock.createMock(IDiagramTypeProvider.class);
		expect(diagramProvider.getDiagramEditor()).andStubReturn(editor);
		replay(diagramProvider);
		IJPAEditorFeatureProvider provider = EasyMock.createMock(IJPAEditorFeatureProvider.class);
		expect(provider.getDiagramTypeProvider()).andStubReturn(diagramProvider);
		return provider;
	}

	private IMarkerDelta replayDelta(IResource resource) {
		IMarkerDelta delta = EasyMock.createMock(IMarkerDelta.class);
		expect(delta.getResource()).andStubReturn(resource);
		replay(delta);
		return delta;
	}

	private JPASolver createSolver(IEclipseFacade facade, IJPAEditorUtil util) {
		return new JPASolver(facade, util);
	}

	public static IResourceChangeListener eqResourceListener(IArgumentMatcher matcher) {
		EasyMock.reportMatcher(matcher);
		return null;
	}

	private IResourceChangeEvent replayEvent(IResource resource) {
		IResourceChangeEvent event = EasyMock.createMock(IResourceChangeEvent.class);
		expect(event.findMarkerDeltas(null, true)).andStubReturn(new IMarkerDelta[] { replayDelta(resource) });
		replay(event);
		return event;
	}

	private IResourceChangeEvent replayEmptyEvent() {
		IResourceChangeEvent event = EasyMock.createMock(IResourceChangeEvent.class);
		expect(event.findMarkerDeltas(null, true)).andStubReturn(new IMarkerDelta[] {});
		replay(event);
		return event;
	}

}

Back to the top