org.springframework.core.task.MockRunnable Java Examples

The following examples show how to use org.springframework.core.task.MockRunnable. 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: RequestContextListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #2
Source File: RequestContextListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #3
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #4
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #5
Source File: RequestContextListenerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #6
Source File: RequestContextListenerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example #7
Source File: RequestContextListenerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}
 
Example #8
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}
 
Example #9
Source File: RequestContextListenerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}