Skip to main content
summaryrefslogtreecommitdiffstats
blob: bf2e0a86bbf4b88075837733a3b51d3fa79ad8d5 (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
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.directorywatcher;

import java.io.File;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;

public class RepositoryListenerTest extends AbstractDirectoryWatcherTest {

	/*
	 * Constructor for the class.
	 */
	public RepositoryListenerTest(String name) {
		super(name);
	}

	/*
	 * Run all the tests in this class.
	 */
	public static Test suite() {
		return new TestSuite(RepositoryListenerTest.class);
	}

	public static boolean isZipped(List<ITouchpointData> data) {
		if (data == null || data.size() == 0)
			return false;
		for (int i = 0; i < data.size(); i++) {
			if (data.get(i).getInstruction("zipped") != null) //$NON-NLS-1$
				return true;
		}
		return false;
	}

	/*
	 * Remove the files from the target, if they exist in the source.
	 */
	private void removeContents(File source, File target) {
		if (source.exists() && source.isDirectory() && target.exists() && target.isDirectory()) {
			File[] files = source.listFiles();
			for (int i = 0; i < files.length; i++)
				if (files[i] != null)
					delete(new File(target, files[i].getName()));
		}
	}

	public void testDirectoryWatcherListener() {
		File baseFolder = getTestData("0.99", "/testData/directorywatcher1");
		File baseFolder2 = getTestData("0.100", "/testData/directorywatcher2");

		// make sure we remove this file after we finish running the tests
		File folder = getTempFolder();
		toRemove.add(folder);

		// create the watcher
		TestRepositoryWatcher watcher = TestRepositoryWatcher.createWatcher(folder);
		watcher.poll();

		// We should have an empty repository because we haven't done anything yet
		assertEquals("1.0", 0, watcher.getInstallableUnits().length);
		assertEquals("1.1", 0, watcher.getArtifactKeys().length);

		// copy the first set of data to the folder so it is discovered and then verify the contents
		copy("2.0", baseFolder, folder);
		watcher.poll();
		IArtifactKey[] keys = watcher.getArtifactKeys();
		for (int i = 0; i < keys.length; i++) {
			String file = watcher.getArtifactFile(keys[i]).getAbsolutePath();
			assertTrue("2.1." + file, file.startsWith(folder.getAbsolutePath()));
		}
		assertEquals("3.0", 2, watcher.getInstallableUnits().length);
		assertEquals("3.1", 2, watcher.getArtifactKeys().length);

		// copy the second data set to be discovered and then verify the number of entries
		copy("4.99", baseFolder2, folder);
		watcher.poll();
		assertEquals("5.0", 3, watcher.getInstallableUnits().length);
		assertEquals("5.1", 3, watcher.getArtifactKeys().length);

		// remove some of the data and then verify the contents
		removeContents(baseFolder, folder);
		watcher.poll();
		assertEquals("6.0", 1, watcher.getInstallableUnits().length);
		assertEquals("6.1", 1, watcher.getArtifactKeys().length);
	}

	public void testBundleShape() {

		// make sure we remove this file after we finish running the tests
		File folder = getTempFolder();
		toRemove.add(folder);

		// create the watcher
		TestRepositoryWatcher watcher = TestRepositoryWatcher.createWatcher(folder);

		// this folder contains a jared plugin and a directory plugin
		File baseFolder = getTestData("0.99", "/testData/directorywatcher1");
		copy("2.0", baseFolder, folder);
		watcher.poll();

		// verify metadata
		IInstallableUnit jaredIU = null;
		IInstallableUnit directoryIU = null;
		IInstallableUnit[] ius = watcher.getInstallableUnits();
		assertEquals("3.0", 2, ius.length);
		for (int i = 0; i < ius.length; i++) {
			IInstallableUnit iu = ius[i];
			if (isZipped(iu.getTouchpointData())) {
				assertNull("4.0", jaredIU);
				jaredIU = iu;
			} else {
				assertNull("5.0", directoryIU);
				directoryIU = iu;
			}
		}
		assertTrue("6.0", directoryIU != null && jaredIU != null);

		// verify artifact descriptors
		IArtifactDescriptor jaredDescriptor = null;
		IArtifactDescriptor directoryDescriptor = null;
		IArtifactKey[] keys = watcher.getArtifactKeys();
		assertEquals("7.0", 2, keys.length);
		for (int i = 0; i < keys.length; i++) {
			IArtifactKey key = keys[i];
			IArtifactDescriptor[] descriptors = watcher.getArtifactDescriptors(key);
			assertEquals("8.0", 1, descriptors.length);
			SimpleArtifactDescriptor descriptor = (SimpleArtifactDescriptor) descriptors[0];
			String isFolder = descriptor.getRepositoryProperty("artifact.folder");
			if (Boolean.valueOf(isFolder).booleanValue()) {
				assertNull("9.0", directoryDescriptor);
				directoryDescriptor = descriptors[0];
			} else {
				assertNull("10.0", jaredDescriptor);
				jaredDescriptor = descriptors[0];
			}
		}
		assertTrue("11.0", jaredDescriptor != null && directoryDescriptor != null);
	}

	/*
	 * Test to ensure that we convert bundles with Eclipse 2.x-style plugin.xml files into 
	 * proper OSGi manifest files so the bundle can be installed
	 */
	public void testPluginXMLConversion() {
		// test plugin.xml in a JAR'd bundle
		File oldBundleJAR = getTestData("0.1", "/testData/repositoryListener/oldBundleJAR.jar");
		// test plugin.xml in a directory-based bundle
		File oldBundleDirectory = getTestData("0.2", "/testData/repositoryListener/oldBundleDirectory");
		// test plugin.xml in the presence of an Ant Manifest file in a JAR'd bundle
		File oldBundleJARTwo = getTestData("0.3", "/testData/repositoryListener/oldBundleJARTwo.jar");
		// test plugin.xml in the presence of an Ant Manifest file in a directory-based bundle
		File oldBundleDirectoryTwo = getTestData("0.4", "/testData/repositoryListener/oldBundleDirectoryTwo");

		// copy the test bundles over to where we will be watching in the repo listener
		File folder = getTempFolder();
		toRemove.add(folder);
		copy("1.0", oldBundleJAR, new File(folder, oldBundleJAR.getName()));
		copy("1.1", oldBundleJARTwo, new File(folder, oldBundleJARTwo.getName()));
		copy("1.2", oldBundleDirectory, new File(folder, oldBundleDirectory.getName()));
		copy("1.3", oldBundleDirectoryTwo, new File(folder, oldBundleDirectoryTwo.getName()));

		// We should have an empty repository because we haven't done anything yet
		TestRepositoryWatcher watcher = TestRepositoryWatcher.createWatcher(folder);
		assertEquals("2.0", 0, watcher.getInstallableUnits().length);
		assertEquals("2.1", 0, watcher.getArtifactKeys().length);

		watcher.poll();
		// the repo should have added all 4 bundles
		assertEquals("3.0", 4, watcher.getInstallableUnits().length);
		assertEquals("3.1", 4, watcher.getArtifactKeys().length);
	}

}

Back to the top