1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package com.android.sdklib.internal.repository.packages;
18
19import com.android.sdklib.SdkManager;
20import com.android.sdklib.internal.repository.archives.Archive;
21import com.android.sdklib.internal.repository.archives.Archive.Arch;
22import com.android.sdklib.internal.repository.archives.Archive.Os;
23import com.android.sdklib.internal.repository.packages.Package;
24import com.android.sdklib.internal.repository.sources.SdkSource;
25
26import java.io.File;
27import java.util.Properties;
28
29/**
30 * A mock empty package, of no particular subpackage type.
31 * {@link #sameItemAs(Package)} will return true if these packages have the same handle.
32 */
33public class MockEmptyPackage extends MajorRevisionPackage {
34    private final String mTestHandle;
35
36    /**
37     * Creates a new {@link MockEmptyPackage} with a local archive.
38     *
39     * @param testHandle The comparison handle for {@link #sameItemAs(Package)}.
40     */
41    public MockEmptyPackage(String testHandle) {
42        super(
43            null /*source*/,
44            null /*props*/,
45            0 /*revision*/,
46            null /*license*/,
47            null /*description*/,
48            null /*descUrl*/,
49            Os.ANY /*archiveOs*/,
50            Arch.ANY /*archiveArch*/,
51            "/sdk/tmp/empty_pkg" /*archiveOsPath*/
52            );
53        mTestHandle = testHandle;
54    }
55
56    /**
57     * Creates a new {@link MockEmptyPackage} with a local archive.
58     *
59     * @param testHandle The comparison handle for {@link #sameItemAs(Package)}.
60     * @param revision The revision of the package, printed in the short description.
61     */
62    public MockEmptyPackage(String testHandle, int revision) {
63        super(
64            null /*source*/,
65            null /*props*/,
66            revision,
67            null /*license*/,
68            null /*description*/,
69            null /*descUrl*/,
70            Os.ANY /*archiveOs*/,
71            Arch.ANY /*archiveArch*/,
72            "/sdk/tmp/empty_pkg" /*archiveOsPath*/
73            );
74        mTestHandle = testHandle;
75    }
76
77    /**
78     * Creates a new {@link MockEmptyPackage} with a local archive.
79     *
80     * @param source The source associate with this package.
81     * @param testHandle The comparison handle for {@link #sameItemAs(Package)}.
82     * @param revision The revision of the package, printed in the short description.
83     */
84    public MockEmptyPackage(SdkSource source, String testHandle, int revision) {
85        super(
86            source,
87            null /*props*/,
88            revision,
89            null /*license*/,
90            null /*description*/,
91            null /*descUrl*/,
92            Os.ANY /*archiveOs*/,
93            Arch.ANY /*archiveArch*/,
94            "/sdk/tmp/empty_pkg" /*archiveOsPath*/
95            );
96        mTestHandle = testHandle;
97    }
98
99    @Override
100    protected Archive[] initializeArchives(
101            Properties props,
102            Os archiveOs,
103            Arch archiveArch,
104            String archiveOsPath) {
105        return new Archive[] {
106            new Archive(this, props, archiveOs, archiveArch, archiveOsPath) {
107                @Override
108                public String toString() {
109                    return mTestHandle;
110                }
111            } };
112    }
113
114    public Archive getLocalArchive() {
115        return getArchives()[0];
116    }
117
118    @Override
119    public File getInstallFolder(String osSdkRoot, SdkManager sdkManager) {
120        return new File(new File(osSdkRoot, "mock"), mTestHandle);
121    }
122
123    @Override
124    public String installId() {
125        return "mock-empty-" + mTestHandle;  //$NON-NLS-1$
126    }
127
128    @Override
129    public String getListDescription() {
130        return this.getClass().getSimpleName();
131    }
132
133    @Override
134    public String getShortDescription() {
135        StringBuilder sb = new StringBuilder(this.getClass().getSimpleName());
136        sb.append(" '").append(mTestHandle).append('\'');
137        if (getRevision().getMajor() > 0) {
138            sb.append(" rev=").append(getRevision());
139        }
140        return sb.toString();
141    }
142
143    /** Returns true if these packages have the same handle. */
144    @Override
145    public boolean sameItemAs(Package pkg) {
146        return (pkg instanceof MockEmptyPackage) &&
147            mTestHandle.equals(((MockEmptyPackage) pkg).mTestHandle);
148    }
149
150    @Override
151    public int hashCode() {
152        final int prime = 31;
153        int result = super.hashCode();
154        result = prime * result + ((mTestHandle == null) ? 0 : mTestHandle.hashCode());
155        return result;
156    }
157
158    @Override
159    public boolean equals(Object obj) {
160        if (this == obj) {
161            return true;
162        }
163        if (!super.equals(obj)) {
164            return false;
165        }
166        if (!(obj instanceof MockEmptyPackage)) {
167            return false;
168        }
169        MockEmptyPackage other = (MockEmptyPackage) obj;
170        if (mTestHandle == null) {
171            if (other.mTestHandle != null) {
172                return false;
173            }
174        } else if (!mTestHandle.equals(other.mTestHandle)) {
175            return false;
176        }
177        return true;
178    }
179}
180