/** * Get the port to be used for test application deployments. * * @return The HTTP port of the URI */ protected final int getPort() { final TestContainer container = getTestContainer(); if (container != null) { // called from outside of JerseyTest constructor return container.getBaseUri().getPort(); } // called from within JerseyTest constructor final String value = getProperty(TestProperties.CONTAINER_PORT); if (value != null) { try { final int i = Integer.parseInt(value); if (i < 0) { throw new NumberFormatException("Value not positive."); } return i; } catch (final NumberFormatException e) { LOGGER.log( Level.CONFIG, "Value of " + TestProperties.CONTAINER_PORT + " property is not a valid positive integer [" + value + "]." + " Reverting to default [" + TestProperties.DEFAULT_CONTAINER_PORT + "].", e); } } return TestProperties.DEFAULT_CONTAINER_PORT; }
// TODO make final protected URI getBaseUri() { final TestContainer container = getTestContainer(); if (container != null) { // called from outside of JerseyTest constructor return container.getBaseUri(); } // called from within JerseyTest constructor return UriBuilder.fromUri("http://localhost/").port(getPort()).build(); }
/** * Tear down the test by {@link TestContainer#stop() stopping} the test container obtained from * the {@link #getTestContainerFactory() test container factory} and by {@link * javax.ws.rs.client.Client#close() closing} and discarding the {@link * #configureClient(org.glassfish.jersey.client.ClientConfig) pre-configured} test client that was * {@link #setUp() set up} for the test. * * @throws Exception if an exception is thrown during tearing down the test environment. */ @After public void tearDown() throws Exception { if (isLogRecordingEnabled()) { unregisterLogHandler(); } try { TestContainer oldContainer = setTestContainer(null); if (oldContainer != null) { oldContainer.stop(); } } finally { closeIfNotNull(setClient(null)); } }
/** * Set up the test by creating a test container instance, {@link TestContainer#start() starting} * it and by creating a new {@link #configureClient(org.glassfish.jersey.client.ClientConfig) * pre-configured} test client. The test container is obtained from the {@link * #getTestContainerFactory() test container factory}. * * @throws TestContainerException if the default test container factory cannot be obtained, or the * test application deployment context is not supported by the test container factory. * @throws Exception if an exception is thrown during setting up the test environment. */ @Before public void setUp() throws Exception { if (isLogRecordingEnabled()) { registerLogHandler(); } final TestContainer testContainer = createTestContainer(context); // Set current instance of test container and start it. setTestContainer(testContainer); testContainer.start(); // Create an set new client. setClient(getClient(testContainer.getClientConfig())); }
/** * Set up the test by invoking {@link TestContainer#start() } on the test container obtained from * the test container factory. * * @throws Exception if an exception is thrown during setting up the test environment. */ @Before public void setUp() throws Exception { if (isLogRecordingEnabled()) { loggedRuntimeRecords.clear(); registerLogHandler(); } tc.start(); Client old = client.getAndSet(getClient(tc, application)); close(old); }
/** * Tear down the test by invoking {@link TestContainer#stop() } on the test container obtained * from the test container factory. * * @throws Exception if an exception is thrown during tearing down the test environment. */ @After public void tearDown() throws Exception { if (isLogRecordingEnabled()) { loggedRuntimeRecords.clear(); unregisterLogHandler(); } try { tc.stop(); } finally { Client old = client.getAndSet(null); close(old); } }
/** * Creates an instance of {@link Client}. * * <p>Checks whether TestContainer provides ClientConfig instance and if not, empty new {@link * org.glassfish.jersey.client.ClientConfig} instance will be used to create new client instance. * * <p>This method is called exactly once when JerseyTest is created. * * @param tc instance of {@link TestContainer} * @param applicationHandler instance of {@link ApplicationHandler} * @return A Client instance. */ protected Client getClient(TestContainer tc, ApplicationHandler applicationHandler) { ClientConfig cc = tc.getClientConfig(); if (cc == null) { cc = new ClientConfig(); } // check if logging is required if (isEnabled(TestProperties.LOG_TRAFFIC)) { cc.register(new LoggingFilter(LOGGER, isEnabled(TestProperties.DUMP_ENTITY))); } configureClient(cc); return ClientBuilder.newClient(cc); }
/** * Create a web resource whose URI refers to the base URI the Web application is deployed at. * * @return the created web resource */ public WebTarget target() { return client().target(tc.getBaseUri()); }