List of usage examples for org.springframework.security.core.userdetails User equals
@Override public boolean equals(Object rhs)
From source file:com.mastercard.test.spring.security.WatchWithUserTestExecutionListener.java
/** * Build a list of mock/test users for the provided TestContext if not present. If * present, validate that the user set in the SecurityContext matches the expected User based * on the current iteration of the TestContext. If the actual user does not match expected, * then a test failure is raised.//www . j a va 2s . co m * @param testContext The TestContext being executed * @throws Exception if an Exception occurs */ @Override public void beforeTestMethod(TestContext testContext) throws Exception { testClass = testContext.getTestClass(); String methodMapKey = testClass.getName() + ":" + testContext.getTestMethod().getName(); TestMethodContext context = methodContextMap.get(methodMapKey); if (context == null) { context = new TestMethodContext(testContext.getTestMethod().getName(), findUserAnnotations(testContext), testContext.getApplicationContext()); methodContextMap.put(methodMapKey, context); } boolean pop = false; User expectedUser = null; User user = null; if (SecurityContextHolder.getContext().getAuthentication() != null) { user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); } if (context.getExecutionStack().size() > 0) { expectedUser = context.getExecutionStack().pop(); } if (user != null || expectedUser != null) { if (user != null && !user.equals(expectedUser)) { throw new AssertionFailedError( "Invalid SecurityUser, actual=" + user + ", expected=" + expectedUser); } } }