From 883b9f1a3cbf9f2d2eb78dea6c11188a159c4792 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 12 May 2011 19:36:10 +0200 Subject: [PATCH] improve exception handling --- .../java/sonia/scm/client/ClientHandler.java | 45 ++++--- .../main/java/sonia/scm/client/ScmClient.java | 3 +- .../sonia/scm/client/ScmClientException.java | 96 +++++++++++++- .../sonia/scm/client/ScmClientProvider.java | 4 +- .../sonia/scm/client/ScmClientSession.java | 9 ++ .../scm/client/ScmForbiddenException.java | 67 ++++++++++ ...ception.java => ScmNotFoundException.java} | 29 ++++- .../scm/client/ScmUnauthorizedException.java | 67 ++++++++++ .../java/sonia/scm/client/ClientUtil.java | 119 ++++++++++++++++++ .../scm/client/JerseyClientProvider.java | 21 +--- .../sonia/scm/client/JerseyClientSession.java | 3 +- .../client/JerseyRepositoryClientHandler.java | 63 +++------- .../client/it/JerseyClientProviderITCase.java | 17 +-- ...eyClientRepositoryClientHandlerITCase.java | 77 +++++++----- .../java/sonia/scm/client/it/TestUtil.java | 12 -- 15 files changed, 486 insertions(+), 146 deletions(-) create mode 100644 scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmForbiddenException.java rename scm-clients/scm-client-api/src/main/java/sonia/scm/client/{UnauthorizedException.java => ScmNotFoundException.java} (77%) create mode 100644 scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUnauthorizedException.java diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientHandler.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientHandler.java index 4aa9482b31..b178ab3680 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientHandler.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientHandler.java @@ -1,10 +1,10 @@ /** * Copyright (c) 2010, Sebastian Sdorra * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, @@ -13,7 +13,7 @@ * 3. Neither the name of SCM-Manager; nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,27 +24,34 @@ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * http://bitbucket.org/sdorra/scm-manager - * + * */ + + + package sonia.scm.client; +//~--- JDK imports ------------------------------------------------------------ + import java.util.List; /** * * @author Sebastian Sdorra + * + * @param */ public interface ClientHandler { - - + /** * Method description * * - * @param repository + * + * @param item */ public void create(T item); @@ -60,7 +67,8 @@ public interface ClientHandler * Method description * * - * @param repository + * + * @param item */ public void delete(T item); @@ -68,20 +76,13 @@ public interface ClientHandler * Method description * * - * @param repository + * + * @param item */ public void modify(T item); //~--- get methods ---------------------------------------------------------- - /** - * Method description - * - * - * @return - */ - public List getAll(); - /** * Method description * @@ -91,4 +92,12 @@ public interface ClientHandler * @return */ public T get(String id); + + /** + * Method description + * + * + * @return + */ + public List getAll(); } diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClient.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClient.java index e83e834b18..94b5206109 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClient.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClient.java @@ -98,9 +98,8 @@ public class ScmClient * * @return * - * @throws ScmClientException */ - private static ScmClientProvider getProvider() throws ScmClientException + private static ScmClientProvider getProvider() { if (provider == null) { diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientException.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientException.java index 1e4036f22d..2a15a277f7 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientException.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientException.java @@ -37,14 +37,36 @@ package sonia.scm.client; * * @author Sebastian Sdorra */ -public class ScmClientException extends Exception +public class ScmClientException extends RuntimeException { + /** Field description */ + public static final int SC_FORBIDDEN = 403; + + /** Field description */ + public static final int SC_NOTFOUND = 404; + + /** Field description */ + public static final int SC_UNAUTHORIZED = 401; + + /** Field description */ + public static final int SC_UNKNOWN = -1; + + /** Field description */ + private static final long serialVersionUID = -2302107106896701393L; + + //~--- constructors --------------------------------------------------------- + /** * Constructs ... * + * + * @param statusCode */ - public ScmClientException() {} + public ScmClientException(int statusCode) + { + this.statusCode = statusCode; + } /** * Constructs ... @@ -68,6 +90,20 @@ public class ScmClientException extends Exception super(cause); } + /** + * Constructs ... + * + * + * + * @param statusCode + * @param message + */ + public ScmClientException(int statusCode, String message) + { + super(message); + this.statusCode = statusCode; + } + /** * Constructs ... * @@ -79,4 +115,60 @@ public class ScmClientException extends Exception { super(message, cause); } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public String getContent() + { + return content; + } + + /** + * Method description + * + * + * @return + */ + public int getStatusCode() + { + return statusCode; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param content + */ + public void setContent(String content) + { + this.content = content; + } + + /** + * Method description + * + * + * @param statusCode + */ + public void setStatusCode(int statusCode) + { + this.statusCode = statusCode; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String content; + + /** Field description */ + private int statusCode = SC_UNKNOWN; } diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientProvider.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientProvider.java index aed69a372e..1a03696400 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientProvider.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientProvider.java @@ -50,9 +50,7 @@ public interface ScmClientProvider * * @return * - * @throws ScmClientException */ public ScmClientSession createSession(String url, String username, - String password) - throws ScmClientException; + String password); } diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientSession.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientSession.java index 3a63e0c092..c1f21e841b 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientSession.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmClientSession.java @@ -50,6 +50,15 @@ import java.io.Closeable; public interface ScmClientSession extends Closeable { + /** + * Method description + * + */ + @Override + public void close(); + + //~--- get methods ---------------------------------------------------------- + /** * Method description * diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmForbiddenException.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmForbiddenException.java new file mode 100644 index 0000000000..d029d654a8 --- /dev/null +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmForbiddenException.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.client; + +/** + * + * @author Sebastian Sdorra + */ +public class ScmForbiddenException extends ScmClientException +{ + + /** Field description */ + private static final long serialVersionUID = 3937346624508458660L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public ScmForbiddenException() + { + super(SC_FORBIDDEN); + } + + /** + * Constructs ... + * + * + * @param message + */ + public ScmForbiddenException(String message) + { + super(SC_FORBIDDEN, message); + } +} diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/UnauthorizedException.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmNotFoundException.java similarity index 77% rename from scm-clients/scm-client-api/src/main/java/sonia/scm/client/UnauthorizedException.java rename to scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmNotFoundException.java index 4b18858175..ae0835e8cb 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/UnauthorizedException.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmNotFoundException.java @@ -37,4 +37,31 @@ package sonia.scm.client; * * @author Sebastian Sdorra */ -public class UnauthorizedException extends ScmClientException {} +public class ScmNotFoundException extends ScmClientException +{ + + /** Field description */ + private static final long serialVersionUID = -7015126639998723954L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public ScmNotFoundException() + { + super(SC_NOTFOUND); + } + + /** + * Constructs ... + * + * + * @param message + */ + public ScmNotFoundException(String message) + { + super(SC_NOTFOUND, message); + } +} diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUnauthorizedException.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUnauthorizedException.java new file mode 100644 index 0000000000..fc561117e1 --- /dev/null +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUnauthorizedException.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.client; + +/** + * + * @author Sebastian Sdorra + */ +public class ScmUnauthorizedException extends ScmClientException +{ + + /** Field description */ + private static final long serialVersionUID = 4398907424134588809L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public ScmUnauthorizedException() + { + super(SC_UNAUTHORIZED); + } + + /** + * Constructs ... + * + * + * @param message + */ + public ScmUnauthorizedException(String message) + { + super(SC_UNAUTHORIZED, message); + } +} diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java index aada9f15c5..7a19c5d916 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/ClientUtil.java @@ -33,6 +33,11 @@ package sonia.scm.client; +//~--- non-JDK imports -------------------------------------------------------- + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + //~--- JDK imports ------------------------------------------------------------ import com.sun.jersey.api.client.Client; @@ -47,6 +52,67 @@ import com.sun.jersey.api.client.filter.LoggingFilter; public class ClientUtil { + /** the logger for ClientUtil */ + private static final Logger logger = + LoggerFactory.getLogger(ClientUtil.class); + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param exception + * @param response + */ + public static void appendContent(ScmClientException exception, + ClientResponse response) + { + try + { + exception.setContent(response.getEntity(String.class)); + } + catch (Exception ex) + { + logger.warn("could not read content", ex); + } + } + + /** + * Method description + * + * + * @param response + * @param expectedStatusCode + */ + public static void checkResponse(ClientResponse response, + int expectedStatusCode) + { + int sc = response.getStatus(); + + if (sc != expectedStatusCode) + { + sendException(response, sc); + } + } + + /** + * Method description + * + * + * @param response + * + */ + public static void checkResponse(ClientResponse response) + { + int sc = response.getStatus(); + + if (sc >= 300) + { + sendException(response, sc); + } + } + /** * Method description * @@ -97,4 +163,57 @@ public class ClientUtil return resource; } + + /** + * Method description + * + * + * @param response + * @param sc + */ + public static void sendException(ClientResponse response, int sc) + { + ScmClientException exception = null; + + switch (sc) + { + case ScmClientException.SC_UNAUTHORIZED : + exception = new ScmUnauthorizedException(); + + break; + + case ScmClientException.SC_FORBIDDEN : + exception = new ScmForbiddenException(); + + break; + + case ScmClientException.SC_NOTFOUND : + exception = new ScmNotFoundException(); + + break; + + default : + exception = new ScmClientException(sc); + appendContent(exception, response); + } + + throw exception; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param response + * + * @return + */ + public static boolean isSuccessfull(ClientResponse response) + { + int status = response.getStatus(); + + return (status > 200) && (status < 300); + } } diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientProvider.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientProvider.java index 61e2a689f2..0b99b03edf 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientProvider.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientProvider.java @@ -95,12 +95,10 @@ public class JerseyClientProvider implements ScmClientProvider * * @return * - * @throws ScmClientException */ @Override public JerseyClientSession createSession(String url, String username, String password) - throws ScmClientException { AssertUtil.assertIsNotEmpty(url); @@ -153,24 +151,7 @@ public class JerseyClientProvider implements ScmClientProvider response = resource.get(ClientResponse.class); } - if (response.getStatus() != 200) - { - String msg = - "server returned ".concat(String.valueOf(response.getStatus())); - - if (logger.isWarnEnabled()) - { - logger.warn(msg); - } - - if (logger.isTraceEnabled()) - { - logger.trace("server returned content: {}", - response.getEntity(String.class)); - } - - throw new ScmClientException(msg); - } + ClientUtil.checkResponse(response); ScmState state = response.getEntity(ScmState.class); diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java index b27a7180a3..899407b434 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientSession.java @@ -83,10 +83,9 @@ public class JerseyClientSession implements ScmClientSession * Method description * * - * @throws IOException */ @Override - public void close() throws IOException + public void close() { if (logger.isInfoEnabled()) { diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java index 61e162502b..696b46ae68 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyRepositoryClientHandler.java @@ -88,26 +88,19 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler try { response = resource.post(ClientResponse.class, repository); + ClientUtil.checkResponse(response, 201); - if (response.getStatus() == 201) - { - String url = response.getHeaders().get("Location").get(0); + String url = response.getHeaders().get("Location").get(0); - AssertUtil.assertIsNotEmpty(url); + AssertUtil.assertIsNotEmpty(url); - Repository newRepository = getRepository(url); + Repository newRepository = getRepository(url); - AssertUtil.assertIsNotNull(newRepository); - newRepository.copyProperties(repository); + AssertUtil.assertIsNotNull(newRepository); + newRepository.copyProperties(repository); - // copyProperties does not copy the repository id - repository.setId(newRepository.getId()); - } - else - { - - // todo errorhandling - } + // copyProperties does not copy the repository id + repository.setId(newRepository.getId()); } finally { @@ -132,12 +125,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler try { response = resource.delete(ClientResponse.class); - - if (response.getStatus() != 204) - { - - // todo errorhandling - } + ClientUtil.checkResponse(response, 204); } finally { @@ -179,12 +167,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler try { response = resource.post(ClientResponse.class, repository); - - if (response.getStatus() != 204) - { - - // todo errorhandling - } + ClientUtil.checkResponse(response, 204); } finally { @@ -224,17 +207,9 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler try { response = resource.get(ClientResponse.class); - - if (response.getStatus() == 200) - { - repositories = response.getEntity(new GenericType>() {} - ); - } - else - { - - // todo errorhandling - } + ClientUtil.checkResponse(response, 200); + repositories = response.getEntity(new GenericType>() {} + ); } finally { @@ -274,14 +249,12 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler { response = resource.get(ClientResponse.class); - if (response.getStatus() == 200) - { - repository = response.getEntity(Repository.class); - } - else - { + int sc = response.getStatus(); - // todo errorhandling + if (sc != ScmClientException.SC_NOTFOUND) + { + ClientUtil.checkResponse(response, 200); + repository = response.getEntity(Repository.class); } } finally diff --git a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientProviderITCase.java b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientProviderITCase.java index c624cea33f..33c263b7ad 100644 --- a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientProviderITCase.java +++ b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientProviderITCase.java @@ -39,6 +39,7 @@ import org.junit.Test; import sonia.scm.client.JerseyClientSession; import sonia.scm.client.ScmClientException; +import sonia.scm.client.ScmUnauthorizedException; import static org.junit.Assert.*; @@ -60,12 +61,9 @@ public class JerseyClientProviderITCase * * * - * @throws IOException - * @throws ScmClientException */ - @Test(expected = ScmClientException.class) + @Test(expected = ScmUnauthorizedException.class) public void createSessionAnonymousFailedTest() - throws ScmClientException, IOException { createAnonymousSession().close(); } @@ -75,12 +73,9 @@ public class JerseyClientProviderITCase * * * - * @throws IOException - * @throws ScmClientException */ @Test public void createSessionAnonymousTest() - throws ScmClientException, IOException { // enable anonymous access @@ -98,11 +93,9 @@ public class JerseyClientProviderITCase * * * - * @throws IOException - * @throws ScmClientException */ @Test - public void createSessionTest() throws ScmClientException, IOException + public void createSessionTest() { JerseyClientSession session = createAdminSession(); @@ -121,7 +114,7 @@ public class JerseyClientProviderITCase * @throws IOException * @throws ScmClientException */ - @Test(expected = ScmClientException.class) + @Test(expected = ScmUnauthorizedException.class) public void createSessionWithUnkownUserTest() throws ScmClientException, IOException { @@ -136,7 +129,7 @@ public class JerseyClientProviderITCase * @throws IOException * @throws ScmClientException */ - @Test(expected = ScmClientException.class) + @Test(expected = ScmUnauthorizedException.class) public void createSessionWithWrongPasswordTest() throws ScmClientException, IOException { diff --git a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientRepositoryClientHandlerITCase.java b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientRepositoryClientHandlerITCase.java index 6ddc175cbf..24c5f0dff3 100644 --- a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientRepositoryClientHandlerITCase.java +++ b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/JerseyClientRepositoryClientHandlerITCase.java @@ -40,7 +40,8 @@ import org.junit.Test; import sonia.scm.client.JerseyClientSession; import sonia.scm.client.RepositoryClientHandler; -import sonia.scm.client.ScmClientException; +import sonia.scm.client.ScmForbiddenException; +import sonia.scm.client.ScmUnauthorizedException; import sonia.scm.repository.Repository; import sonia.scm.repository.RepositoryTestData; import sonia.scm.util.Util; @@ -51,8 +52,6 @@ import static sonia.scm.client.it.TestUtil.*; //~--- JDK imports ------------------------------------------------------------ -import java.io.IOException; - import java.util.List; /** @@ -66,12 +65,9 @@ public class JerseyClientRepositoryClientHandlerITCase * Method description * * - * @throws IOException - * @throws ScmClientException */ @AfterClass public static void removeTestRepositories() - throws ScmClientException, IOException { JerseyClientSession session = createAdminSession(); RepositoryClientHandler handler = session.getRepositoryHandler(); @@ -86,17 +82,16 @@ public class JerseyClientRepositoryClientHandlerITCase } session.close(); + setAnonymousAccess(false); } /** * Method description * * - * @throws IOException - * @throws ScmClientException */ @Test - public void testCreate() throws ScmClientException, IOException + public void testCreate() { JerseyClientSession session = createAdminSession(); Repository hog = RepositoryTestData.createHeartOfGold(REPOSITORY_TYPE); @@ -118,28 +113,9 @@ public class JerseyClientRepositoryClientHandlerITCase * Method description * * - * @throws IOException - * @throws ScmClientException - */ - @Test(expected = ScmClientException.class) - public void testCreateAnonymous() throws ScmClientException, IOException - { - JerseyClientSession session = createAnonymousSession(); - Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE); - - session.getRepositoryHandler().create(p42); - session.close(); - } - - /** - * Method description - * - * - * @throws IOException - * @throws ScmClientException */ @Test - public void testDelete() throws ScmClientException, IOException + public void testDelete() { JerseyClientSession session = createAdminSession(); Repository hvpt = @@ -158,4 +134,47 @@ public class JerseyClientRepositoryClientHandlerITCase assertNull(r); } + + /** + * Method description + * + * + */ + @Test(expected = ScmUnauthorizedException.class) + public void testDisabledCreateAnonymous() + { + JerseyClientSession session = createAnonymousSession(); + Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE); + + session.getRepositoryHandler().create(p42); + session.close(); + } + + /** + * Method description + * + * + */ + @Test + public void testEnabledCreateAnonymous() + { + setAnonymousAccess(true); + + JerseyClientSession session = createAnonymousSession(); + Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE); + boolean forbidden = false; + + try + { + session.getRepositoryHandler().create(p42); + } + catch (ScmForbiddenException ex) + { + forbidden = true; + } + + assertTrue(forbidden); + session.close(); + setAnonymousAccess(false); + } } diff --git a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/TestUtil.java b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/TestUtil.java index b60f416396..da6786c192 100644 --- a/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/TestUtil.java +++ b/scm-clients/scm-client-impl/src/test/java/sonia/scm/client/it/TestUtil.java @@ -38,7 +38,6 @@ package sonia.scm.client.it; import sonia.scm.client.ClientUtil; import sonia.scm.client.JerseyClientProvider; import sonia.scm.client.JerseyClientSession; -import sonia.scm.client.ScmClientException; import sonia.scm.client.ScmUrlProvider; import sonia.scm.config.ScmConfiguration; @@ -47,8 +46,6 @@ import sonia.scm.config.ScmConfiguration; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; -import java.io.IOException; - /** * * @author Sebastian Sdorra @@ -79,10 +76,8 @@ public class TestUtil * * @return * - * @throws ScmClientException */ public static JerseyClientSession createAdminSession() - throws ScmClientException { return createSession(ADMIN_USERNAME, ADMIN_PASSWORD); } @@ -93,10 +88,8 @@ public class TestUtil * * @return * - * @throws ScmClientException */ public static JerseyClientSession createAnonymousSession() - throws ScmClientException { return createSession(null, null); } @@ -110,11 +103,9 @@ public class TestUtil * * @return * - * @throws ScmClientException */ public static JerseyClientSession createSession(String username, String password) - throws ScmClientException { JerseyClientProvider provider = new JerseyClientProvider(REQUEST_LOGGING); @@ -129,11 +120,8 @@ public class TestUtil * * @param access * - * @throws IOException - * @throws ScmClientException */ public static void setAnonymousAccess(boolean access) - throws ScmClientException, IOException { JerseyClientSession adminSession = createAdminSession(); ScmUrlProvider up = adminSession.getUrlProvider();