001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.model;
011:
012: import java.io.ByteArrayInputStream;
013:
014: import junit.framework.TestCase;
015:
016: import org.eclipse.core.runtime.CoreException;
017:
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IFolder;
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.IWorkspace;
022: import org.eclipse.core.resources.IWorkspaceDescription;
023: import org.eclipse.core.resources.ResourcesPlugin;
024:
025: import org.eclipse.jface.viewers.ITreeContentProvider;
026:
027: import org.eclipse.ui.IViewPart;
028: import org.eclipse.ui.IWorkbench;
029: import org.eclipse.ui.IWorkbenchPage;
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.jdt.core.IJavaElement;
033: import org.eclipse.jdt.core.IJavaProject;
034: import org.eclipse.jdt.core.JavaCore;
035:
036: import org.eclipse.jdt.testplugin.JavaProjectHelper;
037:
038: public class ContentProviderTests extends TestCase {
039:
040: private IWorkspace fWorkspace;
041: private IJavaProject fJProject1;
042: private boolean fEnableAutoBuildAfterTesting;
043: private IWorkbench fWorkbench;
044: private IWorkbenchPage fPage;
045: private MockPluginView fMyPart;
046: private ITreeContentProvider fProvider;
047: private IJavaElement fPackageFragment1;
048: private IJavaElement fPackageFragment2;
049: private IFile fFile1;
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053:
054: fWorkspace = ResourcesPlugin.getWorkspace();
055: assertNotNull(fWorkspace);
056: IWorkspaceDescription workspaceDesc = fWorkspace
057: .getDescription();
058: fEnableAutoBuildAfterTesting = workspaceDesc.isAutoBuilding();
059: if (fEnableAutoBuildAfterTesting)
060: JavaProjectHelper.setAutoBuilding(false);
061:
062: assertNotNull(fWorkspace);
063:
064: fJProject1 = JavaProjectHelper.createJavaProject(
065: "TestProject1", "bin");//$NON-NLS-1$//$NON-NLS-2$
066: assertNotNull("project1 null", fJProject1);//$NON-NLS-1$
067: // Use the project root as the classpath
068: fJProject1.setRawClasspath(null, null);
069:
070: // Create some packages
071: IProject project = (IProject) fJProject1.getResource();
072: project.getFolder("f1").create(false, true, null);
073: fPackageFragment1 = JavaCore.create(project.getFolder("f1"));
074: project.getFolder("f2").create(false, true, null);
075: fPackageFragment2 = JavaCore.create(project.getFolder("f2"));
076:
077: //Create a non-Java file in one of the packges
078: fFile1 = project.getFile("f1/b");
079: fFile1.create(new ByteArrayInputStream("".getBytes()), false,
080: null);
081:
082: setUpMockView();
083: }
084:
085: protected void tearDown() throws Exception {
086: JavaProjectHelper.delete(fJProject1);
087: if (fEnableAutoBuildAfterTesting)
088: JavaProjectHelper.setAutoBuilding(true);
089: tearDownMockView();
090: super .tearDown();
091: }
092:
093: private void setUpMockView() throws CoreException {
094: fWorkbench = PlatformUI.getWorkbench();
095: assertNotNull(fWorkbench);
096:
097: fPage = fWorkbench.getActiveWorkbenchWindow().getActivePage();
098: assertNotNull(fPage);
099:
100: //just testing to make sure my part can be created
101: IViewPart myPart = new MockPluginView();
102: assertNotNull(myPart);
103:
104: myPart = fPage
105: .showView("org.eclipse.jdt.ui.tests.model.MockPluginView");//$NON-NLS-1$
106: if (myPart instanceof MockPluginView) {
107: fMyPart = (MockPluginView) myPart;
108: fProvider = fMyPart.getContentProvider();
109: fMyPart.setProject(fJProject1);
110: } else
111: assertTrue("Unable to get view", false);//$NON-NLS-1$
112:
113: assertNotNull(fProvider);
114: }
115:
116: private void tearDownMockView() {
117: fPage.hideView(fMyPart);
118: }
119:
120: private boolean compareArrays(Object[] children,
121: Object[] expectedChildren) {
122: if (children.length != expectedChildren.length)
123: return false;
124: for (int i = 0; i < children.length; i++) {
125: Object child = children[i];
126: if (!contains(child, expectedChildren))
127: return false;
128: }
129: return true;
130: }
131:
132: private boolean contains(Object expected, Object[] expectedChildren) {
133: for (int i = 0; i < expectedChildren.length; i++) {
134: Object object = expectedChildren[i];
135: if (object.equals(expected))
136: return true;
137: }
138: return false;
139: }
140:
141: public void testOutgoingDeletion148118() throws CoreException {
142: IProject project = (IProject) fJProject1.getResource();
143: fMyPart.addOutgoingDeletion(project, "f1/a");
144: fMyPart.addOutgoingDeletion(project, "f2/a");
145: fMyPart.addOutgoingDeletion(project, "f3/");
146:
147: // Children of project
148: Object[] expectedChildren = new Object[] { fPackageFragment1,
149: fPackageFragment2, project.getFolder("f3/") };
150: Object[] children = fProvider.getChildren(fJProject1);
151: assertTrue(
152: "Expected children of project does not match actual children",
153: compareArrays(children, expectedChildren));
154:
155: // Children of fragment 1
156: expectedChildren = new Object[] {
157: fFile1,
158: ((IFolder) fPackageFragment1.getResource())
159: .getFile("a") };
160: children = fProvider.getChildren(fPackageFragment1);
161:
162: // Children of fragment 2
163: expectedChildren = new Object[] { ((IFolder) fPackageFragment2
164: .getResource()).getFile("a") };
165: children = fProvider.getChildren(fPackageFragment2);
166: }
167:
168: public void testIncomingAddition159884() throws CoreException {
169: IProject project = (IProject) fJProject1.getResource();
170: fMyPart.addIncomingAddition(project, "f1/newFolder/");
171: fMyPart.addIncomingAddition(project, "f1/newFolder/a");
172:
173: // Children of project
174: IFolder f1 = project.getFolder("f1");
175: Object[] expectedChildren = new Object[] { f1 };
176: Object[] children = fProvider.getChildren(fJProject1);
177: assertTrue(
178: "Expected children of project does not match actual children",
179: compareArrays(children, expectedChildren));
180:
181: IFolder addedFolder = project.getFolder("f1/newFolder");
182:
183: // Children of f1
184: expectedChildren = new Object[] { addedFolder };
185: children = fProvider.getChildren(f1);
186: assertTrue(
187: "Expected children of f1 does not match actual children",
188: compareArrays(children, expectedChildren));
189:
190: // Children of newFolder
191:
192: expectedChildren = new Object[] { addedFolder.getFile("a") };
193: children = fProvider.getChildren(addedFolder);
194: assertTrue(
195: "Expected children of new folder does not match actual children",
196: compareArrays(children, expectedChildren));
197: }
198:
199: public void testIncomingAddition159884Part2() throws CoreException {
200: IProject project = (IProject) fJProject1.getResource();
201: fMyPart.addIncomingAddition(project, "f1/newFolder/a");
202:
203: // Children of project
204: IFolder f1 = project.getFolder("f1");
205: Object[] expectedChildren = new Object[] { f1 };
206: Object[] children = fProvider.getChildren(fJProject1);
207: assertTrue(
208: "Expected children of project does not match actual children",
209: compareArrays(children, expectedChildren));
210:
211: IFolder addedFolder = project.getFolder("f1/newFolder");
212:
213: // Children of f1
214: expectedChildren = new Object[] { addedFolder };
215: children = fProvider.getChildren(f1);
216: assertTrue(
217: "Expected children of f1 does not match actual children",
218: compareArrays(children, expectedChildren));
219:
220: // Children of newFolder
221:
222: expectedChildren = new Object[] { addedFolder.getFile("a") };
223: children = fProvider.getChildren(addedFolder);
224: assertTrue(
225: "Expected children of new folder does not match actual children",
226: compareArrays(children, expectedChildren));
227: }
228: }
|