| /******************************************************************************* |
| * Copyright (c) 2000, 2006 IBM Corporation and others. |
| * 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: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.jdt.ui.tests.refactoring; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Comparator; |
| import java.util.List; |
| |
| import junit.framework.Assert; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.resources.IResourceDelta; |
| import org.eclipse.core.resources.IResourceDeltaVisitor; |
| import org.eclipse.core.resources.mapping.ModelProvider; |
| |
| public class TestModelProvider extends ModelProvider { |
| |
| private static class Sorter implements Comparator { |
| public int compare(Object o1, Object o2) { |
| IResourceDelta d1= (IResourceDelta) o1; |
| IResourceDelta d2= (IResourceDelta) o2; |
| return d1.getResource().getFullPath().toPortableString().compareTo( |
| d2.getResource().getFullPath().toPortableString()); |
| } |
| } |
| |
| private static final Comparator COMPARATOR= new Sorter(); |
| |
| public static IResourceDelta LAST_DELTA; |
| public static boolean IS_COPY_TEST; |
| |
| private static final int PRE_DELTA_FLAGS= IResourceDelta.CONTENT | IResourceDelta.MOVED_TO | |
| IResourceDelta.MOVED_FROM | IResourceDelta.OPEN; |
| |
| public static void clearDelta() { |
| LAST_DELTA= null; |
| } |
| |
| public IStatus validateChange(IResourceDelta delta, IProgressMonitor pm) { |
| LAST_DELTA= delta; |
| return super.validateChange(delta, pm); |
| } |
| |
| public static void assertTrue(IResourceDelta expected) { |
| Assert.assertNotNull(LAST_DELTA); |
| assertTrue(expected, LAST_DELTA); |
| LAST_DELTA= null; |
| } |
| |
| private static void assertTrue(IResourceDelta expected, IResourceDelta actual) { |
| assertEqual(expected.getResource(), actual.getResource()); |
| int actualKind= actual.getKind(); |
| int actualFlags= actual.getFlags(); |
| // The real delta can't combine kinds so we remove it from the received one as well. |
| if ((actualKind & (IResourceDelta.ADDED | IResourceDelta.REMOVED)) != 0) { |
| actualKind= actualKind & ~IResourceDelta.CHANGED; |
| } |
| |
| // The expected delta doesn't support copy from flag. So remove it |
| actualFlags= actualFlags & ~IResourceDelta.COPIED_FROM; |
| |
| int expectKind= expected.getKind(); |
| int expectedFlags= expected.getFlags() & PRE_DELTA_FLAGS; |
| if ((expectKind & IResourceDelta.ADDED) != 0 && (expectedFlags & IResourceDelta.MOVED_FROM) != 0) { |
| expectedFlags= expectedFlags & ~IResourceDelta.OPEN; |
| } |
| Assert.assertEquals("Same kind", expectKind, actualKind); |
| Assert.assertEquals("Same flags", expectedFlags, actualFlags); |
| IResourceDelta[] expectedChildren= getExpectedChildren(expected); |
| IResourceDelta[] actualChildren= getActualChildren(actual, expectedChildren); |
| Assert.assertEquals("Same number of children", expectedChildren.length, actualChildren.length); |
| Arrays.sort(expectedChildren, COMPARATOR); |
| Arrays.sort(actualChildren, COMPARATOR); |
| for (int i= 0; i < expectedChildren.length; i++) { |
| assertTrue(expectedChildren[i], actualChildren[i]); |
| } |
| } |
| |
| private static void assertEqual(IResource expected, IResource actual) { |
| // This is a simple approach to deal with renamed resources in the deltas. |
| // However it will not work if there is more than on child per delta since |
| // the children will be sorted and their order might change. |
| if (IS_COPY_TEST) { |
| IPath expectedPath= expected.getFullPath(); |
| IPath actualPath= actual.getFullPath(); |
| Assert.assertEquals("Same path length", expectedPath.segmentCount(), actualPath.segmentCount()); |
| for(int i= 0; i < expectedPath.segmentCount(); i++) { |
| String expectedSegment= expectedPath.segment(i); |
| if (expectedSegment.startsWith("UnusedName") || expectedSegment.equals("unusedName")) |
| continue; |
| Assert.assertEquals("Different path segment", expectedSegment, actualPath.segment(i)); |
| } |
| } else { |
| Assert.assertEquals("Same resource", expected, actual); |
| } |
| } |
| |
| private static IResourceDelta[] getExpectedChildren(IResourceDelta delta) { |
| List result= new ArrayList(); |
| IResourceDelta[] children= delta.getAffectedChildren(); |
| for (int i= 0; i < children.length; i++) { |
| IResourceDelta child= children[i]; |
| IResource resource= child.getResource(); |
| if (resource != null && isIgnorable(resource)) |
| continue; |
| if (child.getAffectedChildren().length > 0) { |
| result.add(child); |
| } else { |
| int flags= child.getFlags(); |
| if (flags == 0 || (flags & PRE_DELTA_FLAGS) != 0) { |
| result.add(child); |
| } |
| } |
| } |
| return (IResourceDelta[]) result.toArray(new IResourceDelta[result.size()]); |
| } |
| |
| private static boolean isIgnorable(IResource resource) { |
| final String name= resource.getName(); |
| if (resource.getType() != IResource.FOLDER) |
| return false; |
| return name.startsWith("."); |
| } |
| |
| private static IResourceDelta[] getActualChildren(IResourceDelta delta, IResourceDelta[] expectedChildren) { |
| List result= new ArrayList(); |
| if (!IS_COPY_TEST) { |
| IResourceDelta[] children= delta.getAffectedChildren(); |
| for (int i= 0; i < children.length; i++) { |
| IResourceDelta child= children[i]; |
| IResource resource= child.getResource(); |
| if (resource != null && isIgnorable(resource)) |
| continue; |
| result.add(child); |
| } |
| } else { |
| IResourceDelta[] candidates= delta.getAffectedChildren(); |
| for (int i= 0; i < candidates.length; i++) { |
| IResourceDelta candidate= candidates[i]; |
| IResource resource= candidate.getResource(); |
| if (resource != null && isIgnorable(resource)) |
| continue; |
| if (contains(expectedChildren, candidate)) { |
| result.add(candidate); |
| } else { |
| assertCopySource(candidate); |
| } |
| } |
| } |
| return (IResourceDelta[]) result.toArray(new IResourceDelta[result.size()]); |
| } |
| |
| private static boolean contains(IResourceDelta[] expectedChildren, IResourceDelta actualDelta) { |
| IResource actualResource= actualDelta.getResource(); |
| for (int i= 0; i < expectedChildren.length; i++) { |
| if (isSameResourceInCopy(expectedChildren[i].getResource(), actualResource)) |
| return true; |
| } |
| return false; |
| } |
| |
| private static boolean isSameResourceInCopy(IResource expected, IResource actual) { |
| IPath expectedPath= expected.getFullPath(); |
| IPath actualPath= actual.getFullPath(); |
| if (expectedPath.segmentCount()!= actualPath.segmentCount()) |
| return false; |
| for(int i= 0; i < expectedPath.segmentCount(); i++) { |
| String expectedSegment= expectedPath.segment(i); |
| if (expectedSegment.startsWith("UnusedName") || expectedSegment.equals("unusedName")) |
| continue; |
| if (!expectedSegment.equals(actualPath.segment(i))) |
| return false; |
| } |
| return true; |
| } |
| |
| private static void assertCopySource(IResourceDelta delta) { |
| try { |
| delta.accept(new IResourceDeltaVisitor() { |
| public boolean visit(IResourceDelta d) throws CoreException { |
| Assert.assertTrue("Not a copy delta", (d.getKind() & ~IResourceDelta.CHANGED) == 0); |
| return true; |
| } |
| }); |
| } catch (CoreException e) { |
| Assert.assertTrue("Shouldn't happen", false); |
| } |
| } |
| } |