Skip to main content
summaryrefslogtreecommitdiffstats
blob: 92862d7c701028abbab14a85df6ba234df771b67 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *     Pawel Niewiadomski - fixes for bug 288347
 *******************************************************************************/

package org.eclipse.mylyn.tests.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import junit.framework.AssertionFailedError;

import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;

/**
 * @author Steffen Pingel
 * @deprecated use {@link CommonTestUtil} instead
 */
@Deprecated
public class TestUtil {

	public static final String KEY_CREDENTIALS_FILE = "mylyn.credentials";

	public enum PrivilegeLevel {
		ANONYMOUS, GUEST, USER, ADMIN, READ_ONLY
	};

	public static class Credentials {

		public final String username;

		public final String password;

		public Credentials(String username, String password) {
			this.username = username;
			this.password = password;
		}

		@Override
		public String toString() {
			return getClass().getSimpleName() + " [username=" + username + ",password=" + password + "]";
		}

		public String getShortUserName() {
			if (username.contains("@")) {
				return username.substring(0, username.indexOf("@"));
			}
			return username;
		}

	}

	public static Credentials readCredentials() {
		return readCredentials(PrivilegeLevel.USER, null);
	}

	public static Credentials readCredentials(PrivilegeLevel level) {
		return readCredentials(level, null);
	}

	public static Credentials readCredentials(PrivilegeLevel level, String realm) {
		Properties properties = new Properties();
		try {
			File file;
			String filename = System.getProperty(KEY_CREDENTIALS_FILE);
			if (filename == null) {
				try {
					file = getFile(TestUtil.class, "credentials.properties");
					if (!file.exists()) {
						throw new AssertionFailedError();
					}
				} catch (AssertionFailedError e) {
					file = new File(new File(System.getProperty("user.home"), ".mylyn"), "credentials.properties");
				}
			} else {
				file = new File(filename);
			}
			properties.load(new FileInputStream(file));
		} catch (Exception e) {
			AssertionFailedError error = new AssertionFailedError(
					"must define credentials in $HOME/.mylyn/credentials.properties");
			error.initCause(e);
			throw error;
		}

		String defaultPassword = properties.getProperty("pass");

		realm = (realm != null) ? realm + "." : "";
		switch (level) {
		case ANONYMOUS:
			return createCredentials(properties, realm + "anon.", "", "");
		case GUEST:
			return createCredentials(properties, realm + "guest.", "guest@mylyn.eclipse.org", defaultPassword);
		case USER:
			return createCredentials(properties, realm, "tests@mylyn.eclipse.org", defaultPassword);
		case READ_ONLY:
			return createCredentials(properties, realm, "read-only@mylyn.eclipse.org", defaultPassword);
		case ADMIN:
			return createCredentials(properties, realm + "admin.", "admin@mylyn.eclipse.org", null);
		}

		throw new AssertionFailedError("invalid privilege level");
	}

	private static Credentials createCredentials(Properties properties, String prefix, String defaultUsername,
			String defaultPassword) {
		String username = properties.getProperty(prefix + "user");
		String password = properties.getProperty(prefix + "pass");

		if (username == null) {
			username = defaultUsername;
		}

		if (password == null) {
			password = defaultPassword;
		}

		if (username == null || password == null) {
			throw new AssertionFailedError(
					"username or password not found in <plug-in dir>/credentials.properties, make sure file is valid");
		}

		return new Credentials(username, password);
	}

	public static File getFile(Object source, String filename) throws IOException {
		return CommonTestUtil.getFile(source, filename);
	}

	/**
	 * @deprecated use {org.eclipse.mylyn.commons.sdk.util.CommonTestUtil#runHeartbeatTestsOnly()} instead
	 */
	@Deprecated
	public static boolean runHeartbeatTestsOnly() {
		return !Boolean.parseBoolean(System.getProperty("org.eclipse.mylyn.tests.all"));
	}

//	public static File getFile(String bundleId, Class<?> clazz, String filename) throws IOException {
//		Bundle bundle = Platform.getBundle(bundleId);
//		if (bundle != null) {
//			URL localURL = FileLocator.toFileURL(bundle.getEntry(filename));
//			filename = localURL.getFile();
//		} else {
//			URL localURL = clazz.getResource("");
//			String path = localURL.getFile();
//			int i = path.indexOf("!");
//			if (i != -1) {
//				int j = path.lastIndexOf(File.separatorChar, i);
//				if (j != -1) {
//					path = path.substring(0, j) + File.separator;
//				} else {
//					Assert.fail("Unable to determine location for '" + filename + "' at '" + path + "'");
//				}
//				// class file is nested in jar, use jar path as base
//				if (path.startsWith("file:")) {
//					path = path.substring(5);
//				}
//			} else {
//				// create relative path to base of class file location
//				String[] tokens = clazz.getName().split("\\.");
//				for (int j = 0; j < tokens.length - 1; j++) {
//					path += ".." + File.separator;
//				}
//				if (path.contains("bin" + File.separator)) {
//					path += ".." + File.separator;
//				}
//			}
//			filename = path + filename.replaceAll("/", File.separator);
//		}
//		return new File(filename).getCanonicalFile();
//	}
}

Back to the top