aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/mock/Mocks.java
blob: 65e2144bea7e2bd132b9206dd6b96bf40b8abc25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.mock;

import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;

import com.android.io.IAbstractFolder;
import com.android.io.IAbstractResource;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import java.util.Map;

public class Mocks {
    public static IJavaProject createProject(IClasspathEntry[] entries, IPath outputLocation)
            throws Exception {
        IJavaProject javaProject = createMock(IJavaProject.class);
        final Capture<IClasspathEntry[]> capturedEntries = new Capture<IClasspathEntry[]>();
        Capture<IPath> capturedOutput = new Capture<IPath>();
        capturedEntries.setValue(entries);
        capturedOutput.setValue(outputLocation);

        IProject project = createProject();
        expect(javaProject.getProject()).andReturn(project).anyTimes();
        expect(javaProject.getOutputLocation()).andReturn(capturedOutput.getValue()).anyTimes();

        expect(javaProject.getRawClasspath()).andAnswer(new IAnswer<IClasspathEntry[]>() {
            @Override
            public IClasspathEntry[] answer() throws Throwable {
                return capturedEntries.getValue();
            }
        }).anyTimes();

        javaProject.setRawClasspath(capture(capturedEntries), isA(IProgressMonitor.class));
        expectLastCall().anyTimes();

        javaProject.setRawClasspath(capture(capturedEntries), capture(capturedOutput),
                isA(IProgressMonitor.class));
        expectLastCall().anyTimes();

        final Capture<String> capturedCompliance = new Capture<String>();
        capturedCompliance.setValue("1.4");
        final Capture<String> capturedSource = new Capture<String>();
        capturedSource.setValue("1.4");
        final Capture<String> capturedTarget = new Capture<String>();
        capturedTarget.setValue("1.4");

        expect(javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)).andAnswer(
                new IAnswer<String>() {
                    @Override
                    public String answer() throws Throwable {
                        return capturedCompliance.getValue();
                    }
                });
        expect(javaProject.getOption(JavaCore.COMPILER_SOURCE, true)).andAnswer(
                new IAnswer<String>() {
                    @Override
                    public String answer() throws Throwable {
                        return capturedSource.getValue();
                    }
                });
        expect(javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).andAnswer(
                new IAnswer<String>() {
                    @Override
                    public String answer() throws Throwable {
                        return capturedTarget.getValue();
                    }
                });

        javaProject.setOption(eq(JavaCore.COMPILER_COMPLIANCE), capture(capturedCompliance));
        expectLastCall().anyTimes();
        javaProject.setOption(eq(JavaCore.COMPILER_SOURCE), capture(capturedSource));
        expectLastCall().anyTimes();
        javaProject.setOption(eq(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM),
                capture(capturedTarget));
        expectLastCall().anyTimes();

        replay(javaProject);

        return javaProject;
    }

    /**
     * Creates a mock implementation of {@link IFile}.
     * <p/>
     * Supported methods:
     * <ul>
     * <li>IFile#getName()</li>
     * <li>IFile#getLocation()</li>
     * </ul>
     */
    public static IFile createFile(String fileName) {
        IFile file = createNiceMock(IFile.class);
        expect(file.getName()).andReturn(fileName).anyTimes();
        expect(file.getLocation()).andReturn(new Path(fileName)).anyTimes();
        replay(file);
        return file;
    }

    /**
     * Creates a mock implementation of {@link IFolder}.
     * <p/>
     * Supported methods:
     * <ul>
     * <li>{@link IFolder#getName()}</li>
     * <li>{@link IFolder#members()}</li>
     * </ul>
     */
    public static IFolder createFolder(String name, IResource[] members) throws Exception {
        IFolder file = createNiceMock(IFolder.class);
        expect(file.getName()).andReturn(name).anyTimes();
        // expect(file.getLocation()).andReturn(new Path(name)).anyTimes();
        expect(file.members()).andReturn(members).anyTimes();
        replay(file);
        return file;
    }

    public static IAbstractFolder createAbstractFolder(String name, IAbstractResource[] members) {
        IAbstractFolder folder = createNiceMock(IAbstractFolder.class);
        expect(folder.getName()).andReturn(name).anyTimes();
        // expect(file.getLocation()).andReturn(new Path(name)).anyTimes();
        expect(folder.listMembers()).andReturn(members).anyTimes();
        replay(folder);

        return folder;
    }

    /**
     * Mock implementation of {@link IProject}.
     * <p/>
     * Supported methods:
     * <ul>
     * <li>{@link IProject#build(int kind, IProgressMonitor monitor)}</li>
     * <li>
     * {@link IProject#build(int kind, String builderName, Map args, IProgressMonitor monitor)}
     * </li>
     * </ul>
     */
    public static IProject createProject() {
        IProject project = EasyMock.createNiceMock(IProject.class);
        replay(project);
        return project;
    }

    /**
     * Creates a mock implementation of an {@link IClasspathEntry}, which supports
     * {@link IClasspathEntry#getEntryKind} and {@link IClasspathEntry#getPath}.
     */
    public static IClasspathEntry createClasspathEntry(IPath path, int kind) {
        IClasspathEntry entry = createNiceMock(IClasspathEntry.class);
        expect(entry.getEntryKind()).andReturn(kind).anyTimes();
        expect(entry.getPath()).andReturn(path).anyTimes();
        replay(entry);
        return entry;
    }
}