1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.mock; 18 19 import static org.easymock.EasyMock.capture; 20 import static org.easymock.EasyMock.createMock; 21 import static org.easymock.EasyMock.createNiceMock; 22 import static org.easymock.EasyMock.eq; 23 import static org.easymock.EasyMock.expect; 24 import static org.easymock.EasyMock.expectLastCall; 25 import static org.easymock.EasyMock.isA; 26 import static org.easymock.EasyMock.replay; 27 28 import com.android.io.IAbstractFolder; 29 import com.android.io.IAbstractResource; 30 31 import org.easymock.Capture; 32 import org.easymock.EasyMock; 33 import org.easymock.IAnswer; 34 import org.eclipse.core.resources.IFile; 35 import org.eclipse.core.resources.IFolder; 36 import org.eclipse.core.resources.IProject; 37 import org.eclipse.core.resources.IResource; 38 import org.eclipse.core.runtime.IPath; 39 import org.eclipse.core.runtime.IProgressMonitor; 40 import org.eclipse.core.runtime.Path; 41 import org.eclipse.jdt.core.IClasspathEntry; 42 import org.eclipse.jdt.core.IJavaProject; 43 import org.eclipse.jdt.core.JavaCore; 44 45 import java.util.Map; 46 47 public class Mocks { 48 public static IJavaProject createProject(IClasspathEntry[] entries, IPath outputLocation) 49 throws Exception { 50 IJavaProject javaProject = createMock(IJavaProject.class); 51 final Capture<IClasspathEntry[]> capturedEntries = new Capture<IClasspathEntry[]>(); 52 Capture<IPath> capturedOutput = new Capture<IPath>(); 53 capturedEntries.setValue(entries); 54 capturedOutput.setValue(outputLocation); 55 56 IProject project = createProject(); 57 expect(javaProject.getProject()).andReturn(project).anyTimes(); 58 expect(javaProject.getOutputLocation()).andReturn(capturedOutput.getValue()).anyTimes(); 59 60 expect(javaProject.getRawClasspath()).andAnswer(new IAnswer<IClasspathEntry[]>() { 61 @Override 62 public IClasspathEntry[] answer() throws Throwable { 63 return capturedEntries.getValue(); 64 } 65 }).anyTimes(); 66 67 javaProject.setRawClasspath(capture(capturedEntries), isA(IProgressMonitor.class)); 68 expectLastCall().anyTimes(); 69 70 javaProject.setRawClasspath(capture(capturedEntries), capture(capturedOutput), 71 isA(IProgressMonitor.class)); 72 expectLastCall().anyTimes(); 73 74 final Capture<String> capturedCompliance = new Capture<String>(); 75 capturedCompliance.setValue("1.4"); 76 final Capture<String> capturedSource = new Capture<String>(); 77 capturedSource.setValue("1.4"); 78 final Capture<String> capturedTarget = new Capture<String>(); 79 capturedTarget.setValue("1.4"); 80 81 expect(javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)).andAnswer( 82 new IAnswer<String>() { 83 @Override 84 public String answer() throws Throwable { 85 return capturedCompliance.getValue(); 86 } 87 }); 88 expect(javaProject.getOption(JavaCore.COMPILER_SOURCE, true)).andAnswer( 89 new IAnswer<String>() { 90 @Override 91 public String answer() throws Throwable { 92 return capturedSource.getValue(); 93 } 94 }); 95 expect(javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).andAnswer( 96 new IAnswer<String>() { 97 @Override 98 public String answer() throws Throwable { 99 return capturedTarget.getValue(); 100 } 101 }); 102 103 javaProject.setOption(eq(JavaCore.COMPILER_COMPLIANCE), capture(capturedCompliance)); 104 expectLastCall().anyTimes(); 105 javaProject.setOption(eq(JavaCore.COMPILER_SOURCE), capture(capturedSource)); 106 expectLastCall().anyTimes(); 107 javaProject.setOption(eq(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM), 108 capture(capturedTarget)); 109 expectLastCall().anyTimes(); 110 111 replay(javaProject); 112 113 return javaProject; 114 } 115 116 /** 117 * Creates a mock implementation of {@link IFile}. 118 * <p/> 119 * Supported methods: 120 * <ul> 121 * <li>IFile#getName()</li> 122 * <li>IFile#getLocation()</li> 123 * </ul> 124 */ 125 public static IFile createFile(String fileName) { 126 IFile file = createNiceMock(IFile.class); 127 expect(file.getName()).andReturn(fileName).anyTimes(); 128 expect(file.getLocation()).andReturn(new Path(fileName)).anyTimes(); 129 replay(file); 130 return file; 131 } 132 133 /** 134 * Creates a mock implementation of {@link IFolder}. 135 * <p/> 136 * Supported methods: 137 * <ul> 138 * <li>{@link IFolder#getName()}</li> 139 * <li>{@link IFolder#members()}</li> 140 * </ul> 141 */ 142 public static IFolder createFolder(String name, IResource[] members) throws Exception { 143 IFolder file = createNiceMock(IFolder.class); 144 expect(file.getName()).andReturn(name).anyTimes(); 145 // expect(file.getLocation()).andReturn(new Path(name)).anyTimes(); 146 expect(file.members()).andReturn(members).anyTimes(); 147 replay(file); 148 return file; 149 } 150 151 public static IAbstractFolder createAbstractFolder(String name, IAbstractResource[] members) { 152 IAbstractFolder folder = createNiceMock(IAbstractFolder.class); 153 expect(folder.getName()).andReturn(name).anyTimes(); 154 // expect(file.getLocation()).andReturn(new Path(name)).anyTimes(); 155 expect(folder.listMembers()).andReturn(members).anyTimes(); 156 replay(folder); 157 158 return folder; 159 } 160 161 /** 162 * Mock implementation of {@link IProject}. 163 * <p/> 164 * Supported methods: 165 * <ul> 166 * <li>{@link IProject#build(int kind, IProgressMonitor monitor)}</li> 167 * <li> 168 * {@link IProject#build(int kind, String builderName, Map args, IProgressMonitor monitor)} 169 * </li> 170 * </ul> 171 */ 172 public static IProject createProject() { 173 IProject project = EasyMock.createNiceMock(IProject.class); 174 replay(project); 175 return project; 176 } 177 178 /** 179 * Creates a mock implementation of an {@link IClasspathEntry}, which supports 180 * {@link IClasspathEntry#getEntryKind} and {@link IClasspathEntry#getPath}. 181 */ 182 public static IClasspathEntry createClasspathEntry(IPath path, int kind) { 183 IClasspathEntry entry = createNiceMock(IClasspathEntry.class); 184 expect(entry.getEntryKind()).andReturn(kind).anyTimes(); 185 expect(entry.getPath()).andReturn(path).anyTimes(); 186 replay(entry); 187 return entry; 188 } 189 } 190