Changeset 9529 in josm


Ignore:
Timestamp:
2016-01-18T21:42:58+01:00 (4 years ago)
Author:
simon04
Message:

HttpClient: test progress monitor handling

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/progress/AbstractProgressMonitor.java

    r9078 r9529  
    219219    ==================*/
    220220
    221     abstract void updateProgress(double value);
     221    protected abstract void updateProgress(double value);
    222222
    223223    @Override
  • trunk/src/org/openstreetmap/josm/io/StreamProgressUpdater.java

    r9274 r9529  
    2727
    2828    private void initProgressMonitor() {
     29        progressMonitor.beginTask(taskTitle);
    2930        if (size > 0) {
    30             progressMonitor.subTask(taskTitle);
    3131            progressMonitor.setTicksCount((int) size);
    3232        } else {
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9396 r9529  
    103103            connection.setFixedLengthStreamingMode(requestBody.length);
    104104            connection.setDoOutput(true);
     105            final ProgressMonitor subTaskMonitor = progressMonitor.createSubTaskMonitor(1, false);
    105106            try (OutputStream out = new BufferedOutputStream(
    106                     new ProgressOutputStream(connection.getOutputStream(), requestBody.length, progressMonitor))) {
     107                    new ProgressOutputStream(connection.getOutputStream(), requestBody.length, subTaskMonitor))) {
    107108                out.write(requestBody);
    108109            }
     
    256257                in = connection.getErrorStream();
    257258            }
     259            monitor.subTask(tr("Fetching content..."));
    258260            if (in != null) {
    259                 in = new ProgressInputStream(in, getContentLength(), monitor);
     261                in = new ProgressInputStream(in, getContentLength(), monitor.createSubTaskMonitor(1, false));
    260262                in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
    261263                Compression compression = Compression.NONE;
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r9255 r9529  
    1717import javax.json.spi.JsonProvider;
    1818
     19import org.junit.Before;
    1920import org.junit.BeforeClass;
    2021import org.junit.Test;
    2122import org.openstreetmap.josm.JOSMFixture;
     23import org.openstreetmap.josm.TestUtils;
    2224import org.openstreetmap.josm.data.Version;
     25import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2326
    2427/**
     
    2730public class HttpClientTest {
    2831
     32    private ProgressMonitor progress;
     33
    2934    @BeforeClass
    3035    public static void setUpBeforeClass() {
    3136        JOSMFixture.createFunctionalTestFixture().init();
     37    }
     38
     39    @Before
     40    public void setUp() throws Exception {
     41        progress = TestUtils.newTestProgressMonitor();
    3242    }
    3343
     
    5161    @Test
    5262    public void testGet() throws Exception {
    53         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect();
     63        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/get?foo=bar")).connect(progress);
    5464        assertThat(response.getRequestMethod(), is("GET"));
    5565        assertThat(response.getResponseCode(), is(200));
     
    7080    @Test
    7181    public void testUserAgent() throws Exception {
    72         try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect().getContent();
     82        try (final InputStream in = HttpClient.create(new URL("https://httpbin.org/user-agent")).connect(progress).getContent();
    7383             final JsonReader json = JsonProvider.provider().createReader(in)) {
    7484            assertThat(json.readObject().getString("user-agent"), is(Version.getInstance().getFullAgentString()));
     
    7888    @Test
    7989    public void testFetchUtf8Content() throws Exception {
    80         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect();
     90        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/encoding/utf8")).connect(progress);
    8191        assertThat(response.getResponseCode(), is(200));
    8292        final String content = response.fetchContent();
     
    91101                .setHeader("Content-Type", "text/plain")
    92102                .setRequestBody(text.getBytes(StandardCharsets.UTF_8))
    93                 .connect();
     103                .connect(progress);
    94104        assertThat(response.getResponseCode(), is(200));
    95105        try (final InputStream in = response.getContent();
     
    100110
    101111    @Test
     112    public void testPostZero() throws Exception {
     113        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/post"), "POST")
     114                .setHeader("Content-Type", "text/plain")
     115                .setRequestBody("".getBytes(StandardCharsets.UTF_8))
     116                .connect(progress);
     117        assertThat(response.getResponseCode(), is(200));
     118        try (final InputStream in = response.getContent();
     119             final JsonReader json = JsonProvider.provider().createReader(in)) {
     120            assertThat(json.readObject().getString("data"), is(""));
     121        }
     122    }
     123
     124    @Test
    102125    public void testRelativeRedirects() throws Exception {
    103         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect();
     126        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect(progress);
    104127        assertThat(response.getResponseCode(), is(200));
    105128        assertThat(response.getContentLength() > 100, is(true));
     
    108131    @Test
    109132    public void testAbsoluteRedirects() throws Exception {
    110         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect();
     133        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect(progress);
    111134        assertThat(response.getResponseCode(), is(200));
    112135        assertThat(response.getContentLength() > 100, is(true));
     
    115138    @Test(expected = IOException.class)
    116139    public void testTooMuchRedirects() throws Exception {
    117         HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect();
     140        HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect(progress);
    118141    }
    119142
     
    121144    public void test418() throws Exception {
    122145        // https://tools.ietf.org/html/rfc2324
    123         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect();
     146        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/status/418")).connect(progress);
    124147        assertThat(response.getResponseCode(), is(418));
    125148        assertThat(response.getResponseMessage(), is("I'M A TEAPOT"));
     
    130153    @Test
    131154    public void testRequestInTime() throws Exception {
    132         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect();
     155        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(3500).connect(progress);
    133156        assertThat(response.getResponseCode(), is(200));
    134157    }
     
    136159    @Test(expected = IOException.class)
    137160    public void testTakesTooLong() throws Exception {
    138         HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect();
     161        HttpClient.create(new URL("https://httpbin.org/delay/3")).setReadTimeout(2500).connect(progress);
    139162    }
    140163}
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r9489 r9529  
    44import static org.junit.Assert.fail;
    55
     6import java.awt.Component;
    67import java.io.File;
    78import java.io.IOException;
     
    1011import java.util.Comparator;
    1112
     13import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
     14import org.openstreetmap.josm.gui.progress.CancelHandler;
     15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     16import org.openstreetmap.josm.gui.progress.ProgressTaskId;
    1217import org.openstreetmap.josm.io.Compression;
    1318
     
    145150                dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
    146151    }
     152
     153    /**
     154     * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
     155     * but does not show the progress.
     156     * @return a progress monitor
     157     */
     158    public static ProgressMonitor newTestProgressMonitor() {
     159        return new AbstractProgressMonitor(new CancelHandler()) {
     160
     161            @Override
     162            protected void doBeginTask() {
     163            }
     164
     165            @Override
     166            protected void doFinishTask() {
     167            }
     168
     169            @Override
     170            protected void doSetIntermediate(boolean value) {
     171            }
     172
     173            @Override
     174            protected void doSetTitle(String title) {
     175            }
     176
     177            @Override
     178            protected void doSetCustomText(String title) {
     179            }
     180
     181            @Override
     182            protected void updateProgress(double value) {
     183            }
     184
     185            @Override
     186            public void setProgressTaskId(ProgressTaskId taskId) {
     187            }
     188
     189            @Override
     190            public ProgressTaskId getProgressTaskId() {
     191                return null;
     192            }
     193
     194            @Override
     195            public Component getWindowParent() {
     196                return null;
     197            }
     198
     199        };
     200    }
    147201}
Note: See TracChangeset for help on using the changeset viewer.