Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3c96083aea177ac4e29514fceeca1f0189827e0 (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
/*******************************************************************************
 * Copyright (c) 2012,2018 Red Hat, Inc. and others.
 *
 * This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License 2.0 which accompanies this distribution, and is
 * available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Red Hat, Inc. - initial API and implementation
 *      Ericsson AB - ongoing development
 *      Red Hat, Inc. - fragment support
 ******************************************************************************/
package org.eclipse.equinox.p2.tests.simpleconfigurator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.util.Properties;
import org.eclipse.equinox.internal.simpleconfigurator.SimpleConfiguratorImpl;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.p2.tests.sharedinstall.AbstractSharedInstallTest;

public class SimpleConfiguratorTest extends AbstractProvisioningTest {

	protected URL relativeURL;
	protected File userConfiguration;
	protected File masterConfguration;
	protected URL[] sharedConfiguration = new URL[2];
	protected URL[] localConfiguration = new URL[1];
	protected SimpleConfiguratorImpl configurator;

	@Override
	public void setUp() throws Exception {
		relativeURL = new URL("file://bundles.info");
		File tmp = getTempFolder();
		final String USER_PATH = "testData/simpleconfigurator/user";
		userConfiguration = new File(tmp, USER_PATH);
		copy("copyUserConfiguration", getTestData("userConfiguration", USER_PATH), userConfiguration);

		final String MASTER_PATH = "testData/simpleconfigurator/master";
		masterConfguration = new File(tmp, MASTER_PATH);
		copy("copymasterConfiguration", getTestData("masterConfiguration", MASTER_PATH), masterConfguration);

		sharedConfiguration[0] = userConfiguration.toURL();

		sharedConfiguration[1] = masterConfguration.toURL();
		localConfiguration[0] = sharedConfiguration[1];
		configurator = getSimpleConfigurator();
	}

	private SimpleConfiguratorImpl getSimpleConfigurator() {
		return new SimpleConfiguratorImpl(null, null);
	}

	protected void storeTimestamp(long timestamp) throws IOException {
		File f = new File(userConfiguration.getParent(), SimpleConfiguratorImpl.BASE_TIMESTAMP_FILE_BUNDLESINFO);
		Properties p = new Properties();
		p.put(SimpleConfiguratorImpl.KEY_BUNDLESINFO_TIMESTAMP, "" + timestamp);
		p.store(new FileOutputStream(f), "");
	}

	@Override
	protected void tearDown() throws Exception {
		System.setProperty(SimpleConfiguratorImpl.PROP_IGNORE_USER_CONFIGURATION, "false");

		super.tearDown();
	}

	protected void assertIsPropertySet(boolean set) {
		assertEquals(set, Boolean.TRUE.toString().equalsIgnoreCase(System.getProperty(SimpleConfiguratorImpl.PROP_IGNORE_USER_CONFIGURATION)));
	}

	public void testSimpleConfiguration() throws MalformedURLException {
		assertEquals(localConfiguration[0], configurator.chooseConfigurationURL(relativeURL, localConfiguration));
		assertIsPropertySet(false);
	}

	public void testNotExistingConfigiration() throws MalformedURLException {
		assertNull(configurator.chooseConfigurationURL(relativeURL, new URL[] {new File(".", "notexisting").toURL()}));
		assertIsPropertySet(false);
	}

	public void testSharedConfigurationUserNotExisting() throws MalformedURLException {
		sharedConfiguration[0] = new File(".", "notexisting").toURL();
		assertEquals(sharedConfiguration[1], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(false);
	}

	// no timestamp -> pick user
	public void testSharedConfigurationNoTimestamp() throws MalformedURLException {
		assertEquals(sharedConfiguration[0], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(false);
	}

	//master modified -> pick master
	public void testSharedConfigurationMasterModified() throws IOException {
		storeTimestamp(1000);
		assertEquals(sharedConfiguration[1], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(true);
	}

	//master not modified -> extension configured
	//on adding extension master must be selected in order to create new profile with extensions!
	public void testSharedConfigurationMasterUnmodified() throws IOException {
		storeTimestamp(new File(masterConfguration, relativeURL.getFile()).lastModified());
		assertEquals(sharedConfiguration[0], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(false);
	}

	//master not modified, but property present -> pick master
	public void testSharedConfigurationMasterUnmodifiedPropertySet() throws IOException {
		System.setProperty(SimpleConfiguratorImpl.PROP_IGNORE_USER_CONFIGURATION, "true");
		storeTimestamp(new File(masterConfguration, relativeURL.getFile()).lastModified());
		assertEquals(sharedConfiguration[1], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(true);
	}

	/**
	 * Sets the mtime of the given file to zero, optionally record zero to the timestamp file instead of the last modified time.
	 */
	protected void clearLastModified(File file, boolean storeZero) throws IOException {
		long ctime = file.lastModified();
		file.setLastModified(0);
		try {
			FileTime ft = (FileTime) Files.getAttribute(file.toPath(), "unix:ctime");
			ctime = ft.toMillis();
		} catch (UnsupportedOperationException | IllegalArgumentException | IOException e) {
			// Not applicable on non-posix platforms
		}
		if (storeZero) {
			storeTimestamp(0);
		} else {
			storeTimestamp(ctime);
		}
	}

	// master modified, but the mtime of the master config is set to zero --> choose master
	public void testSharedConfigurationMasterModifiedNoMtime() throws IOException {
		if (AbstractSharedInstallTest.WINDOWS) {
			// See bug 540069 and bug 540310. Test below is for posix OS only
			return;
		}
		clearLastModified(masterConfguration, true);
		assertEquals(sharedConfiguration[1], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(true);
	}

	// master not modified, but the mtime of the master config is set to zero --> choose user
	public void testSharedConfigurationMasterUnmodifiedNoMtime() throws IOException {
		if (AbstractSharedInstallTest.WINDOWS) {
			// See bug 540069 and bug 540310. Test below is for posix OS only
			return;
		}
		clearLastModified(masterConfguration, false);
		assertEquals(sharedConfiguration[0], configurator.chooseConfigurationURL(relativeURL, sharedConfiguration));
		assertIsPropertySet(false);
	}
}

Back to the top