Java Code Examples for org.springframework.web.context.support.AnnotationConfigWebApplicationContext#getBean()

The following examples show how to use org.springframework.web.context.support.AnnotationConfigWebApplicationContext#getBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceUrlProviderJavaConfigTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	this.filterChain = new MockFilterChain(this.servlet,
			new ResourceUrlEncodingFilter(),
			(request, response, chain) -> {
				Object urlProvider = context.getBean(ResourceUrlProvider.class);
				request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
				chain.doFilter(request, response);
			});
}
 
Example 2
Source File: ResourceUrlProviderJavaConfigTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	this.filterChain = new MockFilterChain(this.servlet,
			new ResourceUrlEncodingFilter(),
			(request, response, chain) -> {
				Object urlProvider = context.getBean(ResourceUrlProvider.class);
				request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
				chain.doFilter(request, response);
			});
}
 
Example 3
Source File: ResourceUrlProviderJavaConfigTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	Object urlProvider = context.getBean(ResourceUrlProvider.class);
	this.request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
}
 
Example 4
Source File: ResourceUrlProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // SPR-12592
@SuppressWarnings("resource")
public void initializeOnce() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(HandlerMappingConfiguration.class);
	context.refresh();

	ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class);
	assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**"));
	assertFalse(urlProviderBean.isAutodetect());
}
 
Example 5
Source File: ResourceUrlProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-12592
@SuppressWarnings("resource")
public void initializeOnce() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(HandlerMappingConfiguration.class);
	context.refresh();

	ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class);
	assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**"));
	assertFalse(urlProviderBean.isAutodetect());
}
 
Example 6
Source File: ResourceUrlProviderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void initializeOnce() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(HandlerMappingConfiguration.class);
	context.refresh();
	ResourceUrlProvider translator = context.getBean(ResourceUrlProvider.class);
	assertThat(translator.getHandlerMap(), Matchers.hasKey("/resources/**"));
	assertFalse(translator.isAutodetect());
}
 
Example 7
Source File: IndexDocTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void runSpringHttpSessionConfig() {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(SpringHttpSessionConfig.class);
	context.setServletContext(new MockServletContext());
	context.refresh();

	try {
		context.getBean(SessionRepositoryFilter.class);
	}
	finally {
		context.close();
	}
}
 
Example 8
Source File: HybridServiceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testHybridService() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);
	context.register(TestCombinedService.class);

	WebSocketClient wsClient = new WebSocketClient();

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		final MessageSerDe serDe = context.getBean(ProtobufMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		messageRegistry.registerType("stuff", TestObject.class);
		
		wsClient.start();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);
		
		QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);

		Session session = wsClient.connect(webSocket, new URI("ws://localhost:" +  properties.get("websocket.port") + "/protobuf")).get(5000, TimeUnit.MILLISECONDS);

		Envelope envelope = new Envelope("getStuff", null, null, null);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("stuff", response.value);

		byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
		
		envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("stuff", response.value);

		envelope = new Envelope("getStuff", null, null, null);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);
		
		response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);

		response = httpClient.postForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				new TestObject("even more stuff"),
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);
		
		response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("even more stuff", response.value);
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}
 
Example 9
Source File: WebSocketTransportTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyWebSocketFrameUsingBinary() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);
	context.register(TestWebSocketService.class);

	WebSocketClient wsClient = new WebSocketClient();
	
	try {
		//start server
		context.refresh();

		// start client
		wsClient.start();

		final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		QueuingWebSocketListener listener = new QueuingWebSocketListener(serDe, messageRegistry, null);
		
		WebSocketSession session = (WebSocketSession)wsClient.connect(listener, new URI("ws://localhost:" +  properties.get("websocket.port") + "/" + serDe.getMessageFormatName()))
				.get(5000, TimeUnit.MILLISECONDS);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(new byte[0]));
		
		ServiceError error = listener.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(error);
		Assert.assertEquals("EMPTY_ENVELOPE", error.code);
		Assert.assertEquals("STOPPED", session.getState());
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}
 
Example 10
Source File: WebSocketTransportTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyWebSocketFrameUsingText() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);
	context.register(TestWebSocketService.class);

	WebSocketClient wsClient = new WebSocketClient();
	
	try {
		//start server
		context.refresh();

		// start client
		wsClient.start();

		final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		QueuingWebSocketListener listener = new QueuingWebSocketListener(serDe, messageRegistry, null);
		
		WebSocketSession session = (WebSocketSession)wsClient.connect(listener, new URI("ws://localhost:" +  properties.get("websocket.port") + "/" + serDe.getMessageFormatName()))
				.get(5000, TimeUnit.MILLISECONDS);
		
		session.getRemote().sendString("");
		
		ServiceError error = listener.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(error);
		Assert.assertEquals("EMPTY_ENVELOPE", error.code);
		Assert.assertEquals("STOPPED", session.getState());
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}