Skip to content

Commit

Permalink
Add test. Fix throwing wrapped cause from HTTP response on closing st…
Browse files Browse the repository at this point in the history
…ream exception.

Former-commit-id: 4413f1aa862c2d0f7b8bedb769c1193637b137b8
  • Loading branch information
dkocher committed May 24, 2016
1 parent 2ff38fc commit 0d80f18
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
Expand Up @@ -27,9 +27,6 @@
import java.io.IOException;
import java.io.OutputStream;

/**
* @version $Id$
*/
public abstract class ResponseOutputStream<T> extends ProxyOutputStream {
private static final Logger log = Logger.getLogger(AbstractHttpWriteFeature.class);

Expand All @@ -54,7 +51,7 @@ public void close() throws IOException {
}
}
catch(BackgroundException e) {
throw new IOException(e);
throw new IOException(e.getDetail(), e);
}
}
}
Expand Up @@ -36,6 +36,9 @@ public void close(final InputStream in) throws BackgroundException {
in.close();
}
catch(IOException e) {
if(e.getCause() instanceof BackgroundException) {
throw (BackgroundException) e.getCause();
}
throw new DefaultIOExceptionMappingService().map(e);
}
}
Expand All @@ -50,6 +53,9 @@ public void close(final OutputStream out) throws BackgroundException {
out.close();
}
catch(IOException e) {
if(e.getCause() instanceof BackgroundException) {
throw (BackgroundException) e.getCause();
}
throw new DefaultIOExceptionMappingService().map(e);
}
}
Expand Down
@@ -0,0 +1,45 @@
package ch.cyberduck.core.http;

/*
* Copyright (c) 2002-2016 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.InteroperabilityException;

import org.apache.commons.io.output.NullOutputStream;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class ResponseOutputStreamTest {

@Test(expected = IOException.class)
public void testClose() throws Exception {
try {
new ResponseOutputStream<Void>(new NullOutputStream()) {
@Override
public Void getResponse() throws BackgroundException {
throw new InteroperabilityException("d");
}
}.close();
}
catch(IOException e) {
assertEquals("d. Please contact your web hosting service provider for assistance.", e.getMessage());
throw e;
}
}
}
@@ -0,0 +1,36 @@
package ch.cyberduck.core.io;

/*
* Copyright (c) 2002-2016 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.InteroperabilityException;
import ch.cyberduck.core.http.ResponseOutputStream;

import org.apache.commons.io.output.NullOutputStream;
import org.junit.Test;

public class DefaultStreamCloserTest {

@Test(expected = InteroperabilityException.class)
public void testClose() throws Exception {
new DefaultStreamCloser().close(new ResponseOutputStream<Void>(new NullOutputStream()) {
@Override
public Void getResponse() throws BackgroundException {
throw new InteroperabilityException("d");
}
});
}
}

0 comments on commit 0d80f18

Please sign in to comment.