001: /*******************************************************************************
002: * Copyright (c) 2000, 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.packageview;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.UnsupportedEncodingException;
014: import java.util.Arrays;
015:
016: import junit.framework.Test;
017: import junit.framework.TestCase;
018: import junit.framework.TestSuite;
019:
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.Path;
022:
023: import org.eclipse.core.resources.IContainer;
024: import org.eclipse.core.resources.IFile;
025: import org.eclipse.core.resources.IFolder;
026: import org.eclipse.core.resources.IWorkspace;
027: import org.eclipse.core.resources.IWorkspaceDescription;
028: import org.eclipse.core.resources.ResourcesPlugin;
029:
030: import org.eclipse.jdt.core.ICompilationUnit;
031: import org.eclipse.jdt.core.IJavaProject;
032: import org.eclipse.jdt.core.IPackageFragment;
033: import org.eclipse.jdt.core.IPackageFragmentRoot;
034:
035: import org.eclipse.jface.viewers.ITreeContentProvider;
036:
037: import org.eclipse.ui.IViewPart;
038: import org.eclipse.ui.IWorkbench;
039: import org.eclipse.ui.IWorkbenchPage;
040: import org.eclipse.ui.PartInitException;
041: import org.eclipse.ui.PlatformUI;
042:
043: import org.eclipse.jdt.internal.ui.JavaPlugin;
044: import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
045: import org.eclipse.jdt.internal.ui.util.CoreUtility;
046:
047: import org.eclipse.jdt.testplugin.JavaProjectHelper;
048:
049: import org.eclipse.jdt.ui.PreferenceConstants;
050:
051: /**
052: * Tests for the PackageExplorerContentProvider. Bugs:
053: * <ul>
054: * <li>66694 PackageExplorer shows elements twice</li>
055: * <li>35851 Content of folders with illegal package name are no shown in package explorer</li>
056: * <li>35851 Content of folders with illegal package name are no shown in package explorer</li>
057: * <li>35851 Content of folders with illegal package name are no shown in package explorer</li>
058: * </ul>
059: *
060: * @since 3.0+
061: */
062: public class ContentProviderTests5 extends TestCase {
063: private boolean fEnableAutoBuildAfterTesting;
064: private ITreeContentProvider fProvider;
065:
066: private IJavaProject fJProject;
067: private IFile fDotClasspath;
068: private IFile fDotProject;
069: private IPackageFragmentRoot jdk;
070:
071: public ContentProviderTests5(String name) {
072: super (name);
073: }
074:
075: public static Test suite() {
076: return new TestSuite(ContentProviderTests5.class);
077: }
078:
079: protected void setUp() throws Exception {
080: super .setUp();
081:
082: IWorkspace workspace = ResourcesPlugin.getWorkspace();
083: assertNotNull(workspace);
084: IWorkspaceDescription workspaceDesc = workspace
085: .getDescription();
086: fEnableAutoBuildAfterTesting = workspaceDesc.isAutoBuilding();
087: if (fEnableAutoBuildAfterTesting)
088: JavaProjectHelper.setAutoBuilding(false);
089:
090: //create project
091: fJProject = JavaProjectHelper.createJavaProject("TestProject",
092: "bin");
093: assertNotNull(fJProject);
094:
095: Object[] resource = fJProject.getNonJavaResources();
096: for (int i = 0; i < resource.length; i++) {
097: Object object = resource[i];
098: if (object instanceof IFile) {
099: IFile file = (IFile) object;
100: if (".classpath".equals(file.getName()))
101: fDotClasspath = file;
102: else if (".project".equals(file.getName()))
103: fDotProject = file;
104: }
105: }
106: assertNotNull(fDotClasspath);
107: assertNotNull(fDotProject);
108:
109: //add rt.jar
110: jdk = JavaProjectHelper.addVariableRTJar(fJProject,
111: "JRE_LIB_TEST", null, null);
112: assertTrue("jdk not found", jdk != null);
113:
114: setUpView();
115: }
116:
117: private void setUpView() throws PartInitException {
118: IWorkbench workbench = PlatformUI.getWorkbench();
119: assertNotNull(workbench);
120:
121: IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
122: .getActivePage();
123: assertNotNull(page);
124:
125: IViewPart myPart = page
126: .showView("org.eclipse.jdt.ui.PackageExplorer");
127: if (myPart instanceof PackageExplorerPart) {
128: PackageExplorerPart packageExplorerPart = (PackageExplorerPart) myPart;
129: packageExplorerPart.setShowLibrariesNode(false);
130: fProvider = (ITreeContentProvider) packageExplorerPart
131: .getTreeViewer().getContentProvider();
132: setFolding(false);
133: } else {
134: assertTrue("Unable to get view", false);
135: }
136: assertNotNull(fProvider);
137: }
138:
139: private void setFolding(boolean fold) {
140: JavaPlugin
141: .getDefault()
142: .getPreferenceStore()
143: .setValue(
144: PreferenceConstants.APPEARANCE_FOLD_PACKAGES_IN_PACKAGE_EXPLORER,
145: fold);
146: }
147:
148: protected void tearDown() throws Exception {
149: super .tearDown();
150: JavaProjectHelper.delete(fJProject);
151:
152: if (fEnableAutoBuildAfterTesting)
153: JavaProjectHelper.setAutoBuilding(true);
154: }
155:
156: private ByteArrayInputStream asInputStream(String string)
157: throws UnsupportedEncodingException {
158: return new ByteArrayInputStream(string.getBytes(ResourcesPlugin
159: .getEncoding()));
160: }
161:
162: public void testProjectSource1() throws Exception { //bug 35851, 66694
163: IPath[] inclusionFilters = { new Path("**"),
164: new Path("excl/incl/") };
165: IPath[] exclusionFilters = { new Path("excl/*"),
166: new Path("x/*.java"), new Path("y/") };
167: IPackageFragmentRoot root = JavaProjectHelper
168: .addSourceContainer(fJProject, "", inclusionFilters,
169: exclusionFilters);
170:
171: IPackageFragment defaultPackage = root.createPackageFragment(
172: "", true, null);
173:
174: IFolder ab = fJProject.getProject().getFolder("a-b");
175: CoreUtility.createFolder(ab, true, true, null);
176: IFile description = ab.getFile("description.txt");
177: description.create(asInputStream("description"), true, null);
178:
179: IPackageFragment exclInclPackage = root.createPackageFragment(
180: "excl.incl", true, null);
181: ICompilationUnit In = exclInclPackage
182: .createCompilationUnit("In.java",
183: "package excl.incl;\r\n"
184: + "public class In {\r\n" + "}\r\n",
185: true, null);
186:
187: IFolder excl = fJProject.getProject().getFolder("excl");
188: IFile Ex = excl.getFile("Ex.java");
189: Ex.create(asInputStream("package excl;\npublic class Ex{}"),
190: false, null);
191:
192: IPackageFragment xPackage = root.createPackageFragment("x",
193: true, null);
194: IFolder x = fJProject.getProject().getFolder("x");
195: IFile xhidden = x.getFile(".hidden");
196: xhidden.create(asInputStream(""), true, null);
197: IFile X = x.getFile("X.java");
198: X
199: .create(
200: asInputStream("package x;\r\npublic class X {\r\n\t\r\n}\r\n"),
201: true, null);
202:
203: x.copy(new Path("y"), true, null);
204: IFolder y = fJProject.getProject().getFolder("y");
205: IFile yX = y.getFile("X.java");
206: IFile yhidden = y.getFile(".hidden");
207:
208: IPackageFragment zPackage = root.createPackageFragment("z",
209: true, null);
210: ICompilationUnit Z = zPackage.createCompilationUnit("Z.java",
211: "package z;public class Z{}", true, null);
212:
213: assertEqualElements(new Object[] { defaultPackage,
214: exclInclPackage, xPackage, zPackage, jdk, ab, excl, y,
215: fDotClasspath, fDotProject }, fProvider
216: .getChildren(fJProject));
217: assertEqualElements(new Object[0], fProvider
218: .getChildren(defaultPackage));
219: assertEqualElements(new Object[] { In }, fProvider
220: .getChildren(exclInclPackage));
221: assertEqualElements(new Object[] { Ex }, fProvider
222: .getChildren(excl));
223: assertEqualElements(new Object[] { X, xhidden }, fProvider
224: .getChildren(xPackage));
225: assertEquals(xPackage, fProvider.getParent(X));
226: assertEquals(xPackage, fProvider.getParent(xhidden));
227: assertEqualElements(new Object[] { Z }, fProvider
228: .getChildren(zPackage));
229: assertEqualElements(new Object[] { description }, fProvider
230: .getChildren(ab));
231: assertEqualElements(new Object[] { Ex }, fProvider
232: .getChildren(excl));
233: assertEqualElements(new Object[] { yX, yhidden }, fProvider
234: .getChildren(y));
235: }
236:
237: public void testNestedSource1() throws Exception { //bug 35851, 66694
238: // <classpathentry excluding="a-b/a/b/" kind="src" path="src"/>
239: // <classpathentry kind="src" path="src/a-b/a/b"/>
240: IPath[] inclusionFilters = {};
241: IPath[] exclusionFilters = { new Path("a-b/a/b/") };
242: IPackageFragmentRoot src = JavaProjectHelper
243: .addSourceContainer(fJProject, "src", inclusionFilters,
244: exclusionFilters);
245: IPackageFragmentRoot srcabab = JavaProjectHelper
246: .addSourceContainer(fJProject, "src/a-b/a/b",
247: new IPath[0], new IPath[0]);
248:
249: IPackageFragment defaultSrc = src.createPackageFragment("",
250: true, null);
251: IPackageFragment p = src.createPackageFragment("p", true, null);
252: IFile file = ((IFolder) p.getCorrespondingResource())
253: .getFile("file.txt");
254: file.create(asInputStream("f"), true, null);
255:
256: IFolder ab = ((IFolder) src.getUnderlyingResource())
257: .getFolder("a-b");
258: CoreUtility.createFolder(ab, true, true, null);
259: IFolder aba = ab.getFolder("a");
260: CoreUtility.createFolder(aba, true, true, null);
261: IFile abaTxt = aba.getFile("aba.txt");
262: abaTxt.create(asInputStream("x"), true, null);
263:
264: IPackageFragment defaultAbab = srcabab.createPackageFragment(
265: "", true, null);
266: ICompilationUnit b = defaultAbab.createCompilationUnit(
267: "B.java", "public class B {}", true, null);
268:
269: assertEqualElements(new Object[] { src, srcabab, jdk,
270: fDotClasspath, fDotProject }, fProvider
271: .getChildren(fJProject));
272: assertEqualElements(new Object[] { defaultSrc, p, ab },
273: fProvider.getChildren(src));
274: assertEqualElements(new Object[] {}, fProvider
275: .getChildren(defaultSrc));
276: assertEqualElements(new Object[] { file }, fProvider
277: .getChildren(p));
278: assertEqualElements(new Object[] { aba }, fProvider
279: .getChildren(ab));
280: assertEqualElements(new Object[] { abaTxt }, fProvider
281: .getChildren(aba));
282:
283: assertEqualElements(new Object[] { defaultAbab }, fProvider
284: .getChildren(srcabab));
285: assertEqualElements(new Object[] { b }, fProvider
286: .getChildren(defaultAbab));
287: }
288:
289: public void testInclExcl1() throws Exception { //bug 35851, 66694
290: // <classpathentry including="a/b/c/" excluding="a/b/c/d/" kind="src" path="src2"/>
291: IPath[] inclusionFilters = { new Path("a/b/c/") };
292: IPath[] exclusionFilters = { new Path("a/b/c/d/") };
293: IPackageFragmentRoot src = JavaProjectHelper
294: .addSourceContainer(fJProject, "src", inclusionFilters,
295: exclusionFilters);
296:
297: IPackageFragment abc = src.createPackageFragment("a.b.c", true,
298: null);
299: ICompilationUnit x = abc.createCompilationUnit("X.java", "",
300: true, null);
301: IFolder d = ((IFolder) abc.getUnderlyingResource())
302: .getFolder("d");
303: CoreUtility.createFolder(d, false, true, null);
304: IFile dTxt = d.getFile("d.txt");
305: dTxt.create(asInputStream(""), true, null);
306:
307: IContainer b = d.getParent().getParent();
308: IContainer a = b.getParent();
309:
310: assertEqualElements(new Object[] { src, jdk, fDotClasspath,
311: fDotProject }, fProvider.getChildren(fJProject));
312: assertEqualElements(new Object[] { abc, a }, fProvider
313: .getChildren(src));
314: assertEqualElements(new Object[] { x, d }, fProvider
315: .getChildren(abc));
316: assertEqualElements(new Object[] { dTxt }, fProvider
317: .getChildren(d));
318: assertEqualElements(new Object[] { b }, fProvider
319: .getChildren(a));
320: }
321:
322: private void assertEqualElements(Object[] expected, Object[] actual) {
323: assertEquals("array length", expected.length, actual.length);
324: exp: for (int i = 0; i < expected.length; i++) {
325: Object e = expected[i];
326: for (int j = 0; j < actual.length; j++) {
327: Object a = actual[j];
328: if (e.equals(a))
329: continue exp;
330: }
331: fail("expected[" + i + "] not found in actual:"
332: + Arrays.asList(actual).toString());
333: }
334: }
335: }
|