📄 portletutilstests.java
字号:
}
public void testExposeRequestAttributesWithNullAttributesMap() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.exposeRequestAttributes(new MockPortletRequest(), null);
}
}.runTest();
}
public void testExposeRequestAttributesWithAttributesMapContainingBadKeyType() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MockPortletRequest request = new MockPortletRequest();
Map attributes = new HashMap();
attributes.put(new Object(), "bad key type");
PortletUtils.exposeRequestAttributes(request, attributes);
}
}.runTest();
}
public void testExposeRequestAttributesSunnyDay() throws Exception {
MockPortletRequest request = new MockPortletRequest();
Map attributes = new HashMap();
attributes.put("ace", "Rick Hunter");
attributes.put("mentor", "Roy Fokker");
PortletUtils.exposeRequestAttributes(request, attributes);
assertEquals("Obviously all of the entries in the supplied attributes Map are not being copied over (exposed)",
attributes.size(), countElementsIn(request.getAttributeNames()));
assertEquals("Rick Hunter", request.getAttribute("ace"));
assertEquals("Roy Fokker", request.getAttribute("mentor"));
}
public void testExposeRequestAttributesWithEmptyAttributesMapIsAnIdempotentOperation() throws Exception {
MockPortletRequest request = new MockPortletRequest();
Map attributes = new HashMap();
PortletUtils.exposeRequestAttributes(request, attributes);
assertEquals("Obviously all of the entries in the supplied attributes Map are not being copied over (exposed)",
attributes.size(), countElementsIn(request.getAttributeNames()));
}
public void testGetOrCreateSessionAttributeWithNullSession() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getOrCreateSessionAttribute(null, "bean", TestBean.class);
}
}.runTest();
}
public void testGetOrCreateSessionAttributeJustReturnsAttributeIfItAlreadyExists() throws Exception {
MockPortletSession session = new MockPortletSession();
final TestBean expectedAttribute = new TestBean("Donna Tartt");
session.setAttribute("donna", expectedAttribute);
Object actualAttribute = PortletUtils.getOrCreateSessionAttribute(session, "donna", TestBean.class);
assertSame(expectedAttribute, actualAttribute);
}
public void testGetOrCreateSessionAttributeCreatesAttributeIfItDoesNotAlreadyExist() throws Exception {
MockPortletSession session = new MockPortletSession();
Object actualAttribute = PortletUtils.getOrCreateSessionAttribute(session, "bean", TestBean.class);
assertNotNull(actualAttribute);
assertEquals("Wrong type of object being instantiated", TestBean.class, actualAttribute.getClass());
}
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndNullClass() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", null);
}
}.runTest();
}
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndClassThatIsAnInterfaceType() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", ITestBean.class);
}
}.runTest();
}
public void testGetOrCreateSessionAttributeWithNoExistingAttributeAndClassWithNoPublicCtor() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getOrCreateSessionAttribute(new MockPortletSession(), "bean", NoPublicCtor.class);
}
}.runTest();
}
public void testGetSessionMutexWithNullSession() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getSessionMutex(null);
}
}.runTest();
}
public void testGetSessionMutexWithNoExistingSessionMutexDefinedJustReturnsTheSessionArgument() throws Exception {
MockPortletSession session = new MockPortletSession();
Object sessionMutex = PortletUtils.getSessionMutex(session);
assertNotNull("PortletUtils.getSessionMutex(..) must never return a null mutex", sessionMutex);
assertSame("PortletUtils.getSessionMutex(..) must return the exact same PortletSession supplied as an argument if no mutex has been bound as a Session attribute beforehand",
session, sessionMutex);
}
public void testGetSessionMutexWithExistingSessionMutexReturnsTheExistingSessionMutex() throws Exception {
MockPortletSession session = new MockPortletSession();
Object expectSessionMutex = new Object();
session.setAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE, expectSessionMutex);
Object actualSessionMutex = PortletUtils.getSessionMutex(session);
assertNotNull("PortletUtils.getSessionMutex(..) must never return a null mutex", actualSessionMutex);
assertSame("PortletUtils.getSessionMutex(..) must return the bound mutex attribute if a mutex has been bound as a Session attribute beforehand",
expectSessionMutex, actualSessionMutex);
}
public void testGetSessionAttributeWithNullPortletRequest() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getSessionAttribute(null, "foo");
}
}.runTest();
}
public void testGetRequiredSessionAttributeWithNullPortletRequest() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.getRequiredSessionAttribute(null, "foo");
}
}.runTest();
}
public void testSetSessionAttributeWithNullPortletRequest() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
PortletUtils.setSessionAttribute(null, "foo", "bar");
}
}.runTest();
}
public void testGetSessionAttributeDoes_Not_CreateANewSession() throws Exception {
MockControl mock = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mock.getMock();
request.getPortletSession(false);
mock.setReturnValue(null);
mock.replay();
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
assertNull("Must return null if session attribute does not exist (or if Session does not exist)", sessionAttribute);
mock.verify();
}
public void testGetSessionAttributeWithExistingSession() throws Exception {
MockControl mock = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mock.getMock();
request.getPortletSession(false);
MockPortletSession session = new MockPortletSession();
session.setAttribute("foo", "foo");
mock.setReturnValue(session);
mock.replay();
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
assertEquals("foo", sessionAttribute);
mock.verify();
}
public void testGetRequiredSessionAttributeWithExistingSession() throws Exception {
MockControl mock = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mock.getMock();
request.getPortletSession(false);
MockPortletSession session = new MockPortletSession();
session.setAttribute("foo", "foo");
mock.setReturnValue(session);
mock.replay();
Object sessionAttribute = PortletUtils.getRequiredSessionAttribute(request, "foo");
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
assertEquals("foo", sessionAttribute);
mock.verify();
}
public void testGetRequiredSessionAttributeWithExistingSessionAndNoAttribute() throws Exception {
MockControl mock = MockControl.createControl(PortletRequest.class);
final PortletRequest request = (PortletRequest) mock.getMock();
request.getPortletSession(false);
MockPortletSession session = new MockPortletSession();
mock.setReturnValue(session);
mock.replay();
new AssertThrows(IllegalStateException.class) {
public void test() throws Exception {
PortletUtils.getRequiredSessionAttribute(request, "foo");
}
}.runTest();
mock.verify();
}
public void testSetSessionAttributeWithExistingSessionAndNullValue() throws Exception {
MockControl mockSession = MockControl.createControl(PortletSession.class);
PortletSession session = (PortletSession) mockSession.getMock();
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mockRequest.getMock();
request.getPortletSession(false); // must not create Session for null value...
mockRequest.setReturnValue(session);
session.removeAttribute("foo", PortletSession.APPLICATION_SCOPE);
mockSession.setVoidCallable();
mockRequest.replay();
mockSession.replay();
PortletUtils.setSessionAttribute(request, "foo", null, PortletSession.APPLICATION_SCOPE);
mockRequest.verify();
mockSession.verify();
}
public void testSetSessionAttributeWithNoExistingSessionAndNullValue() throws Exception {
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mockRequest.getMock();
request.getPortletSession(false); // must not create Session for null value...
mockRequest.setReturnValue(null);
mockRequest.replay();
PortletUtils.setSessionAttribute(request, "foo", null, PortletSession.APPLICATION_SCOPE);
mockRequest.verify();
}
public void testSetSessionAttributeWithExistingSessionAndSpecificScope() throws Exception {
MockControl mockSession = MockControl.createControl(PortletSession.class);
PortletSession session = (PortletSession) mockSession.getMock();
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mockRequest.getMock();
request.getPortletSession(); // must create Session...
mockRequest.setReturnValue(session);
session.setAttribute("foo", "foo", PortletSession.APPLICATION_SCOPE);
mockSession.setVoidCallable();
mockRequest.replay();
mockSession.replay();
PortletUtils.setSessionAttribute(request, "foo", "foo", PortletSession.APPLICATION_SCOPE);
mockRequest.verify();
mockSession.verify();
}
public void testGetSessionAttributeWithExistingSessionAndSpecificScope() throws Exception {
MockControl mockSession = MockControl.createControl(PortletSession.class);
PortletSession session = (PortletSession) mockSession.getMock();
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mockRequest.getMock();
request.getPortletSession(false);
mockRequest.setReturnValue(session);
session.getAttribute("foo", PortletSession.APPLICATION_SCOPE);
mockSession.setReturnValue("foo");
mockRequest.replay();
mockSession.replay();
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo", PortletSession.APPLICATION_SCOPE);
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
assertEquals("foo", sessionAttribute);
mockRequest.verify();
mockSession.verify();
}
public void testGetSessionAttributeWithExistingSessionDefaultsToPortletScope() throws Exception {
MockControl mockSession = MockControl.createControl(PortletSession.class);
PortletSession session = (PortletSession) mockSession.getMock();
MockControl mockRequest = MockControl.createControl(PortletRequest.class);
PortletRequest request = (PortletRequest) mockRequest.getMock();
request.getPortletSession(false);
mockRequest.setReturnValue(session);
session.getAttribute("foo", PortletSession.PORTLET_SCOPE);
mockSession.setReturnValue("foo");
mockRequest.replay();
mockSession.replay();
Object sessionAttribute = PortletUtils.getSessionAttribute(request, "foo");
assertNotNull("Must not return null if session attribute exists (and Session exists)", sessionAttribute);
assertEquals("foo", sessionAttribute);
mockRequest.verify();
mockSession.verify();
}
private static int countElementsIn(Enumeration enumeration) {
int count = 0;
while (enumeration.hasMoreElements()) {
enumeration.nextElement();
++count;
}
return count;
}
private static final class NoPublicCtor {
private NoPublicCtor() {
throw new IllegalArgumentException("Just for eclipse...");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -