Search in sources :

Example 1 with TestFailureException

use of ee.jakarta.tck.ws.rs.common.webclient.TestFailureException in project jaxrs-api by eclipse-ee4j.

the class JAXRSCommonClient method invoke.

/*
   * protected methods
   * ========================================================================
   */
/**
 * <PRE>
 * Invokes a test based on the properties
 * stored in TEST_PROPS.  Once the test has completed,
 * the properties in TEST_PROPS will be cleared.
 * </PRE>
 *
 * @throws Fault
 *           If an error occurs during the test run
 */
protected void invoke() throws Fault {
    TestUtil.logTrace("[JAXRSCommonClient] invoke");
    try {
        _testCase = new WebTestCase();
        setTestProperties(_testCase);
        TestUtil.logTrace("[JAXRSCommonClient] EXECUTING");
        if (_useSavedState && _state != null) {
            _testCase.getRequest().setState(_state);
        }
        if (_redirect != false) {
            TestUtil.logTrace("##########Call setFollowRedirects");
            _testCase.getRequest().setFollowRedirects(_redirect);
        }
        _testCase.execute();
        if (_saveState) {
            _state = _testCase.getResponse().getState();
        }
    } catch (TestFailureException tfe) {
        Throwable t = tfe.getRootCause();
        if (t != null) {
            TestUtil.logErr("Root cause of Failure: " + t.getMessage(), t);
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else if (t instanceof Error) {
                throw (Error) t;
            } else {
                throw new RuntimeException(t);
            }
        }
        throw new Fault("[JAXRSCommonClient] " + _testName + " failed!  Check output for cause of failure.", tfe);
    } finally {
        _useSavedState = false;
        _saveState = false;
        _redirect = false;
        clearTestProperties();
    }
}
Also used : WebTestCase(ee.jakarta.tck.ws.rs.common.webclient.WebTestCase) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)

Example 2 with TestFailureException

use of ee.jakarta.tck.ws.rs.common.webclient.TestFailureException in project jaxrs-api by eclipse-ee4j.

the class JaxrsWebTestCase method execute.

/**
 * Executes the test case.
 *
 * @throws TestFailureException
 *           if the test fails for any reason.
 * @throws IllegalStateException
 *           if no request was configured or if no Validator is available at
 *           runtime.
 */
public void execute() throws TestFailureException {
    verifyValidationStrategy();
    verifySettings();
    try {
        String url = logClientRequestAndGetUrl();
        client = getClientWithRegisteredProviders();
        WebTarget target = client.target(url.toString());
        Invocation i = buildRequest(target);
        response = invoke(i);
        if (bufferEntity)
            response.bufferEntity();
    } catch (Throwable t) {
        String message = t.getMessage();
        StringBuilder sb = new StringBuilder();
        sb.append("[FATAL] Unexpected failure during test execution.\n");
        // print client call code to report into JIRA when needed
        sb.append(printClientCall().toString());
        // Inherited message
        sb.append((message == null ? t.toString() : message));
        throw new TestFailureException(sb.toString(), t);
    }
    // Validate this test case instance
    if (!strategy.validate(this)) {
        throw new TestFailureException("Test FAILED!");
    }
}
Also used : Invocation(jakarta.ws.rs.client.Invocation) WebTarget(jakarta.ws.rs.client.WebTarget) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)

Example 3 with TestFailureException

use of ee.jakarta.tck.ws.rs.common.webclient.TestFailureException in project jaxrs-api by eclipse-ee4j.

the class JaxrsWebTestCase method invoke.

/**
 * Invoke the invocation synchronously, or asynchronously
 */
protected Response invoke(Invocation invocation) throws TestFailureException {
    Response response = null;
    switch(executionType) {
        case SYNCHRONOUS:
            response = invocation.invoke();
            break;
        case ASYNCHRONOUS:
            int cnt = 0;
            try {
                final boolean[] buffered = { false };
                InvocationCallback<Response> callback = new InvocationCallback<Response>() {

                    @Override
                    public void completed(Response res) {
                        try {
                            JaxrsWebTestCase.this.response = res;
                            // buffer before stream is closed
                            getResponse().getResponseBodyAsString();
                            buffered[0] = true;
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        throw new RuntimeException(throwable);
                    }
                };
                Future<Response> future = invocation.submit(callback);
                while (!buffered[0] && cnt++ < 50) {
                    Thread.sleep(100L);
                }
                response = future.get();
            // response = invocation.submit().get();
            } catch (Exception e) {
                throw new TestFailureException(e);
            }
            if (cnt > 49) {
                throw new TestFailureException("Invocation callback has not been called within 5 second");
            }
    }
    return response;
}
Also used : HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) Response(jakarta.ws.rs.core.Response) InvocationCallback(jakarta.ws.rs.client.InvocationCallback) IOException(java.io.IOException) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException) IOException(java.io.IOException) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)

Aggregations

TestFailureException (ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)3 WebTestCase (ee.jakarta.tck.ws.rs.common.webclient.WebTestCase)1 HttpResponse (ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse)1 Invocation (jakarta.ws.rs.client.Invocation)1 InvocationCallback (jakarta.ws.rs.client.InvocationCallback)1 WebTarget (jakarta.ws.rs.client.WebTarget)1 Response (jakarta.ws.rs.core.Response)1 IOException (java.io.IOException)1