Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da7ffc9038de5d6d99e8f07086238541d169f30e (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
/*******************************************************************************
 * Copyright (c) 2008 Code 9 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: 
 *   Code 9 - initial API and implementation
 *   IBM - ongoing development
 ******************************************************************************/
package org.eclipse.equinox.p2.tests.publisher.actions;

import static org.easymock.EasyMock.expect;

import java.io.*;
import java.util.*;
import java.util.zip.ZipInputStream;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.publisher.AbstractPublisherAction;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.actions.*;
import org.eclipse.equinox.p2.tests.TestActivator;
import org.eclipse.equinox.p2.tests.TestData;
import org.eclipse.equinox.p2.tests.publisher.TestArtifactRepository;
import org.osgi.framework.Version;

@SuppressWarnings( {"restriction", "unchecked"})
public class RootFilesActionTest extends ActionTest {
	private static final int INCLUDES_ROOT = 1;
	private static final int ARTIFACT_REPO = 2;
	private static final int INCLUDES_FILES = 4;
	private static final int EXCLUDE_INCLUDED = 8;
	private static final int EXCLUDES_UNUSED = 16;
	private static final int ALL = 31;

	protected static String topArg = "sdk.rootfiles.win32.win32.x86"; //$NON-NLS-1$
	protected String rootExclusions = null;
	protected IArtifactRepository artifactRepository;
	protected String idArg = "sdk"; //$NON-NLS-1$
	protected Version versionArg = new Version("3.4.0.i0305"); //$NON-NLS-1$
	private File root = new File(TestActivator.getTestDataFolder(), "RootFilesActionTest/eclipse"); //$NON-NLS-1$
	private File[] includedFiles;
	private File[] excludedFiles;
	private Collection<RootFilesAdvice> adviceCollection;
	private String FILE1 = "level1/level2/file1.jar"; //$NON-NLS-1$
	private String FILE2 = "level1/level2/level3/file1.jar"; //$NON-NLS-1$
	private int testArg;

	public void testAll() throws Exception {
		for (int i = 0; i < ALL; i++) {
			setupTestCase(i);
			setupPublisherInfo();
			setupPublisherResult();
			testAction = new RootFilesAction(publisherInfo, idArg, versionArg, flavorArg);
			assertEquals(Status.OK_STATUS, testAction.perform(publisherInfo, publisherResult, new NullProgressMonitor()));
			verifyRepositoryContents(i);
			cleanup();
		}
	}

	public void insertPublisherInfoBehavior() {
		expect(publisherInfo.getArtifactRepository()).andReturn(artifactRepository).anyTimes();
		expect(publisherInfo.getArtifactOptions()).andReturn(IPublisherInfo.A_INDEX | IPublisherInfo.A_OVERWRITE | IPublisherInfo.A_PUBLISH).anyTimes();
		expect(publisherInfo.getAdvice(configSpec, true, null, null, IRootFilesAdvice.class)).andReturn(adviceCollection).anyTimes();
	}

	private void setupTestCase(int testArg) throws Exception {
		this.testArg = testArg;
		adviceCollection = new ArrayList();
		topLevel = AbstractPublisherAction.getArrayFromString(topArg, COMMA_SEPARATOR);

		if ((testArg & ARTIFACT_REPO) > 0)
			artifactRepository = new TestArtifactRepository();

		if ((testArg & INCLUDES_FILES) > 0) {
			adviceCollection.add(new RootFilesAdvice(null, root.listFiles(), null, configSpec));
		}

		if ((testArg & INCLUDES_ROOT) > 0) {
			adviceCollection.add(new RootFilesAdvice(root, null, null, configSpec));
		}

		if (((testArg & EXCLUDE_INCLUDED) > 0) && includedFiles != null && includedFiles.length > 1) {
			excludedFiles = new File[1];
			excludedFiles[0] = includedFiles[0];
			adviceCollection.add(new RootFilesAdvice(null, null, excludedFiles, configSpec));
		}

		if ((testArg & EXCLUDES_UNUSED) > 0) {
			excludedFiles = new File[1];
			excludedFiles[0] = new File(root, "/eclipse/notHere"); //$NON-NLS-1$
			adviceCollection.add(new RootFilesAdvice(null, null, excludedFiles, configSpec));
		}
		setupPublisherInfo();
	}

	private void verifyRepositoryContents(int arg) throws Exception {
		boolean artifactRepo = (arg & ARTIFACT_REPO) > 0;
		boolean includeFiles = (arg & INCLUDES_FILES) > 0;
		boolean includeRoot = (arg & INCLUDES_ROOT) > 0;
		if (!(artifactRepo && (includeFiles)))
			return;

		IArtifactKey key = ArtifactKey.parse("binary,sdk.rootfiles.win32.win32.x86,3.4.0.i0305"); //$NON-NLS-1$
		assertTrue(artifactRepository.contains(key));
		// File [] repoFiles = getRepoFiles();
		Map fileList = getRepoFiles(new HashMap());
		ZipInputStream zis = ((TestArtifactRepository) artifactRepository).getZipInputStream(key);

		TestData.assertContains(fileList, zis, !(!includeRoot && includeFiles && artifactRepo));
	}

	/**
	 * 
	 * @return a list of relative files to the rootPath.
	 */
	private Map getRepoFiles(Map map) {
		if ((testArg & INCLUDES_FILES) > 0) {
			map = addEntry(map, "simpleconfigurator.source.jar"); //$NON-NLS-1$
			map = addEntry(map, FILE1);
			map = addEntry(map, FILE2);
		}
		return map;
	}

	public void cleanup() {
		super.cleanup();

		if (artifactRepository != null)
			artifactRepository.removeAll();
		artifactRepository = null;

		excludedFiles = null;

		if (adviceCollection != null)
			adviceCollection.clear();

		if (includedFiles != null)
			includedFiles = null;

		adviceCollection = null;
	}

	protected String toArgString(int arg) {
		String result = ""; //$NON-NLS-1$
		if ((arg & INCLUDES_ROOT) > 0)
			result += " INCLUDES_ROOT"; //$NON-NLS-1$
		if ((arg & EXCLUDES_UNUSED) > 0)
			result += " EXCLUDES_UNUSED"; //$NON-NLS-1$
		if ((arg & ARTIFACT_REPO) > 0)
			result += " ARTIFACT_REPO"; //$NON-NLS-1$
		if ((arg & INCLUDES_FILES) > 0)
			result += " INCLUDES_FILES"; //$NON-NLS-1$
		if ((arg & EXCLUDE_INCLUDED) > 0)
			result += " EXCLUDE_INCLUDED"; //$NON-NLS-1$
		return result;
	}

	private Map addEntry(Map map, String fileEntry) {
		try {
			ByteArrayOutputStream content = new ByteArrayOutputStream();
			File contentBytes = new File(root, fileEntry);
			FileUtils.copyStream(new FileInputStream(contentBytes), false, content, true);
			boolean includeRootInEntry = ((testArg & INCLUDES_ROOT) > 0);
			String entry = includeRootInEntry ? new File(fileEntry).getPath() : new File(fileEntry).getName();
			entry = new Path(entry).toString();
			map.put(entry, new Object[] {contentBytes, content.toByteArray()});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
}

Back to the top