diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CPathEntryTest.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CPathEntryTest.java new file mode 100644 index 00000000000..6b8604943ab --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CPathEntryTest.java @@ -0,0 +1,168 @@ +/********************************************************************** + * Copyright (c) 2002,2003 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * QNX Software Systems - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.core.model.tests; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ElementChangedEvent; +import org.eclipse.cdt.core.model.ICElementDelta; +import org.eclipse.cdt.core.model.ICPathEntry; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IElementChangedListener; +import org.eclipse.cdt.testplugin.CProjectHelper; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceDescription; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; + +/* + * CPathEntryTest + */ +public class CPathEntryTest extends TestCase { + IWorkspace workspace; + IWorkspaceRoot root; + IProject project_c, project_cc; + NullProgressMonitor monitor; + String pluginRoot; + + class CElementListener implements IElementChangedListener { + + int count = 0; + + void processDelta(ICElementDelta delta) { + if (delta == null) { + return ; + } + int flags = delta.getFlags(); + int kind = delta.getKind(); + if (kind == ICElementDelta.CHANGED ) { + if ((flags & ICElementDelta.F_ADDED_TO_CPATHENTRY) != 0) { + count++; + } + } + ICElementDelta[] affectedChildren= delta.getAffectedChildren(); + for (int i= 0; i < affectedChildren.length; i++) { + processDelta(affectedChildren[i]); + } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.cdt.core.model.IElementChangedListener#elementChanged(org.eclipse.cdt.core.model.ElementChangedEvent) + */ + public void elementChanged(ElementChangedEvent event) { + processDelta(event.getDelta()); + } + + } + + /** + * Constructor for CModelTests. + * + * @param name + */ + public CPathEntryTest(String name) { + super(name); + } + + /** + * Sets up the test fixture. + * + * Called before every test case method. + * + */ + protected void setUp() throws CoreException { + /*************************************************************************************************************************** + * The test of the tests assume that they have a working workspace and workspace root object to use to create + * projects/files in, so we need to get them setup first. + */ + IWorkspaceDescription desc; + workspace = ResourcesPlugin.getWorkspace(); + root = workspace.getRoot(); + monitor = new NullProgressMonitor(); + if (workspace == null) + fail("Workspace was not setup"); + if (root == null) + fail("Workspace root was not setup"); + pluginRoot = org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.core.tests").find(new Path("/")).getFile(); + desc = workspace.getDescription(); + desc.setAutoBuilding(false); + workspace.setDescription(desc); + + } + + /** + * Tears down the test fixture. + * + * Called after every test case method. + */ + protected void tearDown() { + // release resources here and clean-up + } + + public static TestSuite suite() { + return new TestSuite(CPathEntryTest.class); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + /******************************************************************************************************************************* + * Check if the CPathEntry's are generated. + * + * @see CProjectHelper#createCProject + */ + public void testCPathEntries() throws CoreException { + ICProject testProject; + testProject = CProjectHelper.createCProject("cpathtest", "none"); + if (testProject == null) + fail("Unable to create project"); + ICPathEntry[] entries = testProject.getResolvedCPathEntries(); + assertTrue("No cpathentries", entries.length == 0); + entries = new ICPathEntry[3]; + entries[0] = CoreModel.newIncludeEntry(new Path("cpathtest"), new Path("/usr/include"), true); + entries[1] = CoreModel.newIncludeEntry(new Path("cpaththest/foo.c"), new Path("/usr/include"), true); + entries[2] = CoreModel.newLibraryEntry(new Path("/usr/lib/libc.so.1"), null, null, null); + testProject.setRawCPathEntries(entries, new NullProgressMonitor()); + entries = testProject.getResolvedCPathEntries(); + assertTrue("Tree cpathenties", entries.length == 3); + } + + /******************************************************************************************************************************* + * Check if the CPathEntry's are generated. + * + * @see CProjectHelper#createCProject + */ + public void testCPathEntriesDelta() throws CoreException { + ICProject testProject; + testProject = CProjectHelper.createCProject("cpathtest2", "none"); + if (testProject == null) + fail("Unable to create project"); + ICPathEntry[] entries = new ICPathEntry[3]; + entries[0] = CoreModel.newIncludeEntry(new Path("cpathtest"), new Path("/usr/include"), true); + entries[1] = CoreModel.newIncludeEntry(new Path("cpaththest/foo.c"), new Path("/usr/include"), true); + entries[2] = CoreModel.newLibraryEntry(new Path("/usr/lib/libc.so.1"), null, null, null); + final int count = 0; + CElementListener listener = new CElementListener(); + CoreModel.getDefault().addElementChangedListener(listener); + testProject.setRawCPathEntries(entries, new NullProgressMonitor()); + entries = testProject.getResolvedCPathEntries(); + assertTrue("Tree cpathenties", listener.count == 2); + } +}