Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0b8d9b19f2c196cf85406eafee81bfb6911b609 (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) 2006-2009, Cloudsmith Inc.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the copyright holder
 * listed above, as the Initial Contributor under such license. The text of
 * such license is available at www.eclipse.org.
 ******************************************************************************/

package org.eclipse.equinox.p2.tests.metadata.repository;

import java.net.URI;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ecf.filetransfer.IFileRangeSpecification;
import org.eclipse.ecf.filetransfer.IIncomingFileTransfer;
import org.eclipse.equinox.internal.p2.repository.FileReader;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class ResumeDownloadTest extends AbstractProvisioningTest {
	private static String UPDATE_SITE = "http://download.eclipse.org/eclipse/updates/3.4";
	private IMetadataRepositoryManager mgr;
	private URI repoLoc;
	private String originalResumeProp;

	protected void setUp() throws Exception {
		super.setUp();
		repoLoc = new URI(UPDATE_SITE);
		originalResumeProp = System.getProperty("org.eclipse.equinox.p2.metadata.repository.resumable", "true");

		mgr = (IMetadataRepositoryManager) getAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);
		if (mgr == null) {
			throw new RuntimeException("Repository manager could not be loaded");
		}
		mgr.removeRepository(repoLoc);
		if (mgr.contains(repoLoc))
			throw new RuntimeException("Error - An earlier test did not leave a clean state - could not remove repo");
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		FileReader.setTestProbe(null);
		// reset the resume property to what it was before the test.
		System.setProperty("org.eclipse.equinox.p2.metadata.repository.resumable", originalResumeProp);
		mgr.removeRepository(repoLoc);
	}

	public void testResume() throws ProvisionException {
		boolean caught = false;
		try {
			FileReader.setTestProbe(new CancelSimulator());
			mgr.loadRepository(repoLoc, null);
		} catch (OperationCanceledException e) {
			/* ignore - the operation is supposed to be canceled */
			caught = true;
		}
		assertTrue("Cancel should have been caught (1)", caught);
		caught = false;
		FileReader.setTestProbe(null);

		FileReader.setTestProbe(new ResumeCheck());
		// Try again - this time it should resume
		mgr.loadRepository(repoLoc, null);

		assertTrue("Cancelation was made before entire file was downloaded", bytesReceived < entireLength);
		assertEquals("First+remaining size equals entire size", bytesReceived + remainingLength, entireLength);
		assertTrue("Remaining length smaller than entire length", entireLength > remainingLength);

	}

	public void testResumeTwice() throws ProvisionException {
		boolean caught = false;
		try {
			FileReader.setTestProbe(new CancelSimulator());
			mgr.loadRepository(repoLoc, null);
		} catch (OperationCanceledException e) {
			/* ignore - the operation is supposed to be canceled */
			caught = true;
		}
		assertTrue("Cancel should have been caught (1)", caught);
		caught = false;

		try {
			FileReader.setTestProbe(new CancelSimulator());
			mgr.loadRepository(repoLoc, null);
		} catch (OperationCanceledException e) {
			/* ignore - the operation is supposed to be canceled */
			caught = true;
		}
		assertTrue("Cancel should have been caught (2)", caught);
		caught = false;
		FileReader.setTestProbe(null);

		FileReader.setTestProbe(new ResumeCheck());
		mgr.loadRepository(repoLoc, null);

		assertTrue("Cancelation was made before entire file was downloaded", bytesReceived < entireLength);
		assertEquals("First+remaining size equals entire size", bytesReceived + remainingLength, entireLength);
		assertTrue("Remaining length smaller than entire length", entireLength > remainingLength);

	}

	public void testBlockedResume() throws ProvisionException {
		// block the resume functionality
		System.setProperty("org.eclipse.equinox.p2.metadata.repository.resumable", "false");

		boolean caught = false;
		try {
			FileReader.setTestProbe(new CancelSimulator());
			mgr.loadRepository(repoLoc, null);
		} catch (OperationCanceledException e) {
			/* ignore - the operation is supposed to be canceled */
			caught = true;
		}
		assertTrue("Cancel should have been caught (1)", caught);
		caught = false;
		FileReader.setTestProbe(null);

		FileReader.setTestProbe(new ResumeCheck());
		// Try again - this time it should NOT resume
		mgr.loadRepository(repoLoc, null);

		assertTrue("Cancelation was made before entire file was downloaded", bytesReceived < entireLength);
		assertEquals("Resume starts at 0", 0L, resumeStart);
		assertEquals("Remaining length == original length", entireLength, remainingLength);

	}

	protected long bytesReceived = 0L;
	protected long entireLength = 0L;
	protected long resumeStart = 0L;
	protected long remainingLength = 0L;
	protected long resumedReceived = 0L;

	private class CancelSimulator implements FileReader.IFileReaderProbe {

		public CancelSimulator() {
			//
		}

		public void onData(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			bytesReceived = source.getBytesReceived();
			if (bytesReceived > 1000)
				monitor.setCanceled(true);
		}

		public void onDone(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			bytesReceived = source.getBytesReceived();
		}

		public void onStart(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			entireLength = source.getFileLength();
		}

	}

	private class ResumeCheck implements FileReader.IFileReaderProbe {

		public ResumeCheck() {
			//
		}

		public void onData(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			resumedReceived = source.getBytesReceived();
		}

		public void onDone(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			/* ignore */
		}

		public void onStart(FileReader reader, IIncomingFileTransfer source, IProgressMonitor monitor) {
			IFileRangeSpecification spec = source.getFileRangeSpecification();
			resumeStart = spec == null ? 0L : spec.getStartPosition();
			remainingLength = source.getFileLength();

		}

	}
}

Back to the top