KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > util > Resources


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.util;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.Status;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IResourceStatus;
27 import org.eclipse.core.resources.ResourcesPlugin;
28
29 import org.eclipse.jdt.internal.junit.Messages;
30 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
31 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
32
33
34 public class Resources {
35
36     private Resources() {
37     }
38
39     /**
40      * Checks if the given resource is in sync with the underlying file system.
41      *
42      * @param resource the resource to be checked
43      * @return IStatus status describing the check's result. If <code>status.
44      * isOK()</code> returns <code>true</code> then the resource is in sync
45      */

46     public static IStatus checkInSync(IResource resource) {
47         return checkInSync(new IResource[] {resource});
48     }
49     
50     /**
51      * Checks if the given resources are in sync with the underlying file
52      * system.
53      *
54      * @param resources the resources to be checked
55      * @return IStatus status describing the check's result. If <code>status.
56      * isOK() </code> returns <code>true</code> then the resources are in sync
57      */

58     public static IStatus checkInSync(IResource[] resources) {
59         IStatus result= null;
60         for (int i= 0; i < resources.length; i++) {
61             IResource resource= resources[i];
62             if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) {
63                 result= addOutOfSync(result, resource);
64             }
65         }
66         if (result != null)
67             return result;
68         return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
69
}
70
71     /**
72      * Makes the given resource committable. Committable means that it is
73      * writeable and that its content hasn't changed by calling
74      * <code>validateEdit</code> for the given resource on <tt>IWorkspace</tt>.
75      *
76      * @param resource the resource to be checked
77      * @param context the context passed to <code>validateEdit</code>
78      * @return IStatus status describing the method's result. If <code>status.
79      * isOK()</code> returns <code>true</code> then the resource are committable
80      *
81      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.
82      * resources.IFile[], java.lang.Object)
83      */

84     public static IStatus makeCommittable(IResource resource, Object JavaDoc context) {
85         return makeCommittable(new IResource[] { resource }, context);
86     }
87     
88     /**
89      * Makes the given resources committable. Committable means that all
90      * resources are writeable and that the content of the resources hasn't
91      * changed by calling <code>validateEdit</code> for a given file on
92      * <tt>IWorkspace</tt>.
93      *
94      * @param resources the resources to be checked
95      * @param context the context passed to <code>validateEdit</code>
96      * @return IStatus status describing the method's result. If <code>status.
97      * isOK()</code> returns <code>true</code> then the add resources are
98      * committable
99      *
100      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
101      */

102     public static IStatus makeCommittable(IResource[] resources, Object JavaDoc context) {
103         List JavaDoc readOnlyFiles= new ArrayList JavaDoc();
104         for (int i= 0; i < resources.length; i++) {
105             IResource resource= resources[i];
106             if (resource.getType() == IResource.FILE && resource.getResourceAttributes().isReadOnly())
107                 readOnlyFiles.add(resource);
108         }
109         if (readOnlyFiles.size() == 0)
110             return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
111

112         Map JavaDoc oldTimeStamps= createModificationStampMap(readOnlyFiles);
113         IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
114             (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
115         if (!status.isOK())
116             return status;
117             
118         IStatus modified= null;
119         Map JavaDoc newTimeStamps= createModificationStampMap(readOnlyFiles);
120         for (Iterator JavaDoc iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) {
121             IFile file= (IFile) iter.next();
122             if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file)))
123                 modified= addModified(modified, file);
124         }
125         if (modified != null)
126             return modified;
127         return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
128
}
129
130     private static Map JavaDoc createModificationStampMap(List JavaDoc files){
131         Map JavaDoc map= new HashMap JavaDoc();
132         for (Iterator JavaDoc iter= files.iterator(); iter.hasNext(); ) {
133             IFile file= (IFile)iter.next();
134             map.put(file, new Long JavaDoc(file.getModificationStamp()));
135         }
136         return map;
137     }
138     
139     private static IStatus addModified(IStatus status, IFile file) {
140         IStatus entry= JUnitStatus.createError(Messages.format(JUnitMessages.Resources_fileModified, file.getFullPath().toString()));
141         if (status == null) {
142             return entry;
143         } else if (status.isMultiStatus()) {
144             ((MultiStatus)status).add(entry);
145             return status;
146         } else {
147             MultiStatus result= new MultiStatus(JUnitPlugin.getPluginId(),
148                 IJUnitStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
149             JUnitMessages.Resources_modifiedResources, null);
150             result.add(status);
151             result.add(entry);
152             return result;
153         }
154     }
155
156     private static IStatus addOutOfSync(IStatus status, IResource resource) {
157         IStatus entry= new Status(
158             IStatus.ERROR,
159             ResourcesPlugin.PI_RESOURCES,
160             IResourceStatus.OUT_OF_SYNC_LOCAL,
161         Messages.format(JUnitMessages.Resources_outOfSync, resource.getFullPath().toString()),
162             null);
163         if (status == null) {
164             return entry;
165         } else if (status.isMultiStatus()) {
166             ((MultiStatus)status).add(entry);
167             return status;
168         } else {
169             MultiStatus result= new MultiStatus(
170                 ResourcesPlugin.PI_RESOURCES,
171                 IResourceStatus.OUT_OF_SYNC_LOCAL,
172             JUnitMessages.Resources_outOfSyncResources, null);
173             result.add(status);
174             result.add(entry);
175             return result;
176         }
177     }
178 }
Popular Tags