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
|
/*******************************************************************************
* Copyright (c) 2009, 2020 Cloudsmith 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:
* Cloudsmith Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.p2.tests.repository;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.transport.ecf.RepositoryTransport;
import org.eclipse.equinox.p2.tests.testserver.helper.AbstractTestServerClientCase;
/**
* Tests FileReader.
*/
public class FileReaderTest extends AbstractTestServerClientCase {
public void testUnknownHost() throws URISyntaxException {
RepositoryTransport transport = new RepositoryTransport();
URI toDownload = new URI("http://bogus.nowhere/nothing.xml");
OutputStream target = new ByteArrayOutputStream();
IStatus status = transport.download(toDownload, target, new NullProgressMonitor());
assertEquals("Should be an error", status.getSeverity(), IStatus.ERROR);
assertTrue("Should begin with 'Unknown Host'", status.getMessage().startsWith("Unknown Host"));
}
public void testBadPort() throws URISyntaxException {
RepositoryTransport transport = new RepositoryTransport();
URI toDownload = new URI("http://localhost:1/nothing.xml");
OutputStream target = new ByteArrayOutputStream();
IStatus status = transport.download(toDownload, target, new NullProgressMonitor());
assertEquals("Should be an error", status.getSeverity(), IStatus.ERROR);
assertTrue("Should be a connect exception", status.getException() instanceof ConnectException);
assertTrue("Should begin with 'Connection refused'", status.getException().getMessage().startsWith("Connection refused"));
}
/**
* Tests a successful read.
*/
public void testReadStream() throws URISyntaxException, CoreException, IOException {
RepositoryTransport transport = new RepositoryTransport();
URI toDownload = new URI(getBaseURL() + "/public/index.html");
final NullProgressMonitor monitor = new NullProgressMonitor();
try (InputStream stream = transport.stream(toDownload, monitor)) {
// Avoid unused variable error
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=561134
stream.toString();
}
assertFalse("1.0", monitor.isCanceled());
}
/**
* Tests a successful read.
*/
public void testRead() throws URISyntaxException {
RepositoryTransport transport = new RepositoryTransport();
URI toDownload = new URI(getBaseURL() + "/public/index.html");
OutputStream target = new ByteArrayOutputStream();
final NullProgressMonitor monitor = new NullProgressMonitor();
IStatus result = transport.download(toDownload, target, monitor);
assertTrue("1.0", result.isOK());
}
// TODO: test
// timeout, cancel of timeout (TimeoutTest)
// bad date returned, very old, and in the future
// redirected many times = login
// handling of incorrect file size
//
}
|