Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2260aa56577c258c08a87a576a5ddf082c99ca2 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package org.eclipse.cdt.core.model.tests;


/**********************************************************************
 * Copyright (c) 2002,2003 Rational Software Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: 
 * Rational Software - Initial API and implementation
***********************************************************************/

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Vector;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IElementChangedListener;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.cdt.internal.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.model.TranslationUnit;
import org.eclipse.cdt.testplugin.CProjectHelper;
import org.eclipse.cdt.testplugin.TestPluginLauncher;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;

/**
 * Class for testing the C Element Delta Builder. 
 */
public class ElementDeltaTests extends TestCase implements IElementChangedListener {
	private ICProject fCProject;
	private IFile headerFile;
	private NullProgressMonitor monitor;
	private Vector addedElements;
	private Vector removedElements;
	private Vector changedElements;
	
	public static void main(String[] args) {
		TestPluginLauncher.run(TestPluginLauncher.getLocationFromProperties(), WorkingCopyTests.class, args);
	}
	
	public static Test suite() {
		TestSuite suite= new TestSuite(ElementDeltaTests.class.getName());
		suite.addTest(new ElementDeltaTests("testElementDeltas"));
		return suite;
	}		
	
	public ElementDeltaTests(String name) {
		super(name);
	}
	
	protected void setUp() throws Exception {
		monitor = new NullProgressMonitor();
		String pluginRoot=org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.core.tests").find(new Path("/")).getFile();

		fCProject= CProjectHelper.createCProject("TestProject1", "bin");
		//Path filePath = new Path(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()+ fCProject.getPath().toString()+ "/WorkingCopyTest.h");
		headerFile = fCProject.getProject().getFile("WorkingCopyTest.h");
		if (!headerFile.exists()) {
			try{
				FileInputStream fileIn = new FileInputStream(pluginRoot+ "resources/cfiles/WorkingCopyTestStart.h"); 
				headerFile.create(fileIn,false, monitor);        
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		if (!fCProject.getProject().hasNature(CCProjectNature.CC_NATURE_ID)) {
			addNatureToProject(fCProject.getProject(), CCProjectNature.CC_NATURE_ID, null);
		}
		
		// register with the model manager to listen to delta changes
		CModelManager.getDefault().addElementChangedListener(this);
		addedElements = new Vector(10);
		removedElements = new Vector(10);
		changedElements = new Vector(20);
		CCorePlugin.getDefault().setUseNewParser(true);
	}
	private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException {
		IProjectDescription description = proj.getDescription();
		String[] prevNatures= description.getNatureIds();
		String[] newNatures= new String[prevNatures.length + 1];
		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
		newNatures[prevNatures.length]= natureId;
		description.setNatureIds(newNatures);
		proj.setDescription(description, monitor);
	}

	protected void tearDown() throws Exception {
		CProjectHelper.delete(fCProject);
	}	
		
		
	public void testElementDeltas() throws Exception {
		ITranslationUnit tu = new TranslationUnit(fCProject, headerFile);		
		assertNotNull (tu);
		IWorkingCopy wc = tu.getWorkingCopy();
		assertNotNull (wc);
		assertNotNull (wc.getBuffer());
		assertTrue (wc.exists());
		
		// add the class Hello
		IBuffer wcBuf = wc.getBuffer();
		wcBuf.setContents ("\n class Hello{ \n};");
		wc.reconcile();
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertAddedElement(ICElement.C_CLASS, "Hello");
		assertRemovedElement(ICElement.C_INCLUDE, "stdio.h");
		assertEmptyDelta();
		
		// add the field x		
		wcBuf.setContents ("\n class Hello{\n int x; \n};");
		wc.reconcile();		
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertChangedElement(ICElement.C_CLASS, "Hello");
		assertAddedElement(ICElement.C_FIELD, "x");
		assertEmptyDelta();
		
		// add the method setValue
		wcBuf.setContents ("\n class Hello{\n int x; \n void setValue(int val); \n};");
		wc.reconcile();		
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertChangedElement(ICElement.C_CLASS, "Hello");
		assertAddedElement(ICElement.C_METHOD_DECLARATION, "setValue");
		assertEmptyDelta();
		
		// rename x to y
		// this is not a change, this is add and remove
		wcBuf.setContents ("\n class Hello{\n int y; \n void setValue(int val); \n};");
		wc.reconcile();		
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertChangedElement(ICElement.C_CLASS, "Hello");
		assertAddedElement(ICElement.C_FIELD, "y");
		assertRemovedElement(ICElement.C_FIELD, "x");
		assertEmptyDelta();

		// remove the method
		wcBuf.setContents ("\n class Hello{\n String y; \n};");
		wc.reconcile();		
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertChangedElement(ICElement.C_CLASS, "Hello");
		assertChangedElement(ICElement.C_FIELD, "y");
		assertRemovedElement(ICElement.C_METHOD_DECLARATION, "setValue");
		assertEmptyDelta();
				
		// remove the field		
		wcBuf.setContents ("\n class Hello{ \n};");
		wc.reconcile();
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertChangedElement(ICElement.C_CLASS, "Hello");
		assertRemovedElement(ICElement.C_FIELD, "y");
		assertEmptyDelta();

		// remove the class
		wcBuf.setContents ("");
		wc.reconcile();
		wc.commit(true, monitor);
		assertChangedElement(ICElement.C_MODEL, "");
		assertChangedElement(ICElement.C_PROJECT, "TestProject1");
		assertChangedElement(ICElement.C_UNIT, "WorkingCopyTest.h");
		assertRemovedElement(ICElement.C_CLASS, "Hello");
		assertEmptyDelta();

		wc.destroy();
		assertFalse(wc.exists());		
	}
	
	public void assertAddedElement(int elementType, String elementName){
		if(!isElementInList(elementType, elementName, addedElements))
			fail("Element NOT found in Added list");
	}
	public void assertRemovedElement(int elementType, String elementName){
		if(!isElementInList(elementType, elementName, removedElements))
			fail("Element NOT found in Removed list");
	}
	public void assertChangedElement(int elementType, String elementName){
		if(!isElementInList(elementType, elementName, changedElements))
			fail("Element NOT found in Changed list");
	}
	public void assertEmptyDelta() {
		assertTrue(addedElements.isEmpty());
		assertTrue(removedElements.isEmpty());
		assertTrue(changedElements.isEmpty());
	}
	public boolean isElementInList(int elementType, String elementName, Vector elementList) {
		boolean found = false;
		Iterator i = elementList.iterator();
		while( i.hasNext()){
			ICElement element = (ICElement)i.next();

			if ((element.getElementName().equals(elementName)) && 
				(element.getElementType() == elementType)){
					// return true;
					// just to print the whole list
					found = true;
					// Remove the element
					elementList.remove(element);
					break;
				}
		}
		//return false;
		return found;						
	}
	
	public void elementChanged(ElementChangedEvent event){
		try {
			addedElements.clear();
			removedElements.clear();
			changedElements.clear();
			
			processDelta(event.getDelta());
		} catch(CModelException e) {
		}		
	}

	protected void processDelta(ICElementDelta delta) throws CModelException {
		// check the delta elements
		int kind= delta.getKind();
		int flags= delta.getFlags();
		ICElement element= delta.getElement();
		
		// handle open and closing of a solution or project
		if ((flags & ICElementDelta.F_CLOSED) != 0) {
		}
		if ((flags & ICElementDelta.F_OPENED) != 0) {
		}
		if (kind == ICElementDelta.REMOVED) {
			removedElements.add(element);			
		}
		if (kind == ICElementDelta.ADDED) {
			addedElements.add(element);			
		}
		if (kind == ICElementDelta.CHANGED) {
			changedElements.add(element);
				
			if (flags == ICElementDelta.F_MODIFIERS)	{	
			}
			if (flags == ICElementDelta.F_CONTENT)	{	
			}
			if (flags == ICElementDelta.F_CHILDREN)	{	
			}
		}

		ICElementDelta[] affectedChildren= delta.getAffectedChildren();
		for (int i= 0; i < affectedChildren.length; i++) {
			processDelta(affectedChildren[i]);
		}
	}
	
}

Back to the top