public static void assertWagonExceptionMessage()

in wagon-tcks/wagon-tck-http/src/main/java/org/apache/maven/wagon/tck/http/Assertions.java [97:199]


    public static void assertWagonExceptionMessage( Exception e, int forStatusCode, String forUrl,
                                                    String forReasonPhrase, ProxyInfo proxyInfo )
    {
        // TODO: handle AuthenticationException for Wagon.connect() calls
        assertNotNull( e );
        try
        {
            assertTrue( "only verify instances of WagonException", e instanceof WagonException );

            String reasonPhrase;
            String assertMessageForBadMessage = "exception message not described properly";

            if ( proxyInfo != null )
            {
                assertTrue( "message should end with proxy information if proxy was used",
                        e.getMessage().endsWith( proxyInfo.toString() ) );
            }

            switch ( forStatusCode )
            {
                case HttpServletResponse.SC_NOT_FOUND:
                    // TODO: add test for 410: Gone?
                    assertTrue( "404 not found response should throw ResourceDoesNotExistException",
                            e instanceof ResourceDoesNotExistException );
                    reasonPhrase = StringUtils.isEmpty( forReasonPhrase ) ? " Not Found" : ( " " + forReasonPhrase );
                    assertEquals( assertMessageForBadMessage, "resource missing at " + forUrl + ", status: 404"
                            + reasonPhrase, e.getMessage() );
                    break;

                case HttpServletResponse.SC_UNAUTHORIZED:
                    // FIXME assumes Wagon.get()/put() returning 401 instead of Wagon.connect()
                    assertTrue( "401 Unauthorized should throw AuthorizationException since "
                                    + " AuthenticationException is not explicitly declared as thrown from wagon "
                                    + "methods",
                            e instanceof AuthorizationException );
                    reasonPhrase = StringUtils.isEmpty( forReasonPhrase ) ? " Unauthorized" : ( " " + forReasonPhrase );
                    assertEquals( assertMessageForBadMessage, "authentication failed for " + forUrl + ", status: 401"
                            + reasonPhrase, e.getMessage() );
                    break;

                case HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED:
                    assertTrue( "407 Proxy authentication required should throw AuthorizationException",
                            e instanceof AuthorizationException );
                    reasonPhrase = StringUtils.isEmpty( forReasonPhrase ) ? " Proxy Authentication Required"
                            : ( " " + forReasonPhrase );
                    assertEquals( assertMessageForBadMessage, "proxy authentication failed for "
                            + forUrl + ", status: 407" + reasonPhrase, e.getMessage() );
                    break;

                case HttpServletResponse.SC_FORBIDDEN:
                    assertTrue( "403 Forbidden should throw AuthorizationException",
                            e instanceof AuthorizationException );
                    reasonPhrase = StringUtils.isEmpty( forReasonPhrase ) ? " Forbidden" : ( " " + forReasonPhrase );
                    assertEquals( assertMessageForBadMessage, "authorization failed for " + forUrl + ", status: 403"
                            + reasonPhrase, e.getMessage() );
                    break;

                default:
                    assertTrue( "transfer failures should at least be wrapped in a TransferFailedException", e
                            instanceof TransferFailedException );

                    // the status code and reason phrase cannot always be learned due to implementation limitations
                    // so the message may not include them, but the implementation should use a consistent format
                    assertTrue( "message should always include url tried: " + e.getMessage(),
                            e.getMessage().startsWith( "transfer failed for " + forUrl ) );

                    String statusCodeStr = forStatusCode == NO_RESPONSE_STATUS_CODE ? ""
                            : ", status: " +  forStatusCode;
                    if ( forStatusCode != NO_RESPONSE_STATUS_CODE )
                    {

                        assertTrue( "if there was a response status line, the status code should be >= 400",
                                forStatusCode >= HttpServletResponse.SC_BAD_REQUEST );

                        if ( e.getMessage().length() > ( "transfer failed for " + forUrl ).length() )
                        {
                            assertTrue( "message should include url and status code: " + e.getMessage(),
                                    e.getMessage().startsWith( "transfer failed for " + forUrl + statusCodeStr ) );
                        }

                        reasonPhrase = forReasonPhrase == null ? "" : " " + forReasonPhrase;

                        if ( reasonPhrase.length() > 0 && e.getMessage().length() > ( "transfer failed for " + forUrl
                                + statusCodeStr ).length() )
                        {
                            assertTrue( "message should include url and status code and reason phrase: "
                                    + e.getMessage(), e.getMessage().startsWith( "transfer failed for "
                                            + forUrl + statusCodeStr
                                            + reasonPhrase ) );
                        }

                    }

                    break;
            }
        }
        catch ( AssertionError assertionError )
        {
            LOGGER.error( "Exception which failed assertions: ", e );
            throw assertionError;
        }

    }