SpringServletContextTest.java
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.systest.servlet;
import java.util.Arrays;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
public class SpringServletContextTest extends AbstractServletTest {
@Ignore
public static class EmbeddedJettyServer extends AbstractJettyServer {
public static final int PORT = allocatePortAsInt(EmbeddedJettyServer.class);
public EmbeddedJettyServer() {
super("/org/apache/cxf/systest/servlet/web-spring-context.xml", "/", CONTEXT, PORT);
}
}
@Configuration
public static class AppConfig {
@Bean(destroyMethod = "shutdown")
public SpringBus cxf() {
return new SpringBus();
}
@Bean @DependsOn("cxf")
public Server jaxRsServer() {
final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setServiceBeans(Arrays.asList(contextRestService()));
factory.setAddress("/services/context");
return factory.create();
}
@Bean
public ContextRestService contextRestService() {
return new ContextRestService();
}
}
@Path("/")
public static class ContextRestService {
@Autowired
private ConfigurableApplicationContext context;
@GET
@Path("/refresh")
public void refresh() {
context.refresh();
}
}
@BeforeClass
public static void startServers() throws Exception {
AbstractResourceInfo.clearAllMaps();
assertTrue("server did not launch correctly", launchServer(EmbeddedJettyServer.class, true));
createStaticBus();
}
@AfterClass
public static void tearDown() throws Exception {
stopAllServers();
}
@Test
public void testContextRefresh() throws Exception {
try (CloseableHttpClient client = newClient()) {
final HttpGet method = new HttpGet(uri("/services/context/refresh"));
for (int i = 0; i < 5; ++i) {
try (CloseableHttpResponse response = client.execute(method)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(204));
}
}
}
}
@Override
protected int getPort() {
return EmbeddedJettyServer.PORT;
}
}