DATAJPA-564 - Polishing of SpEL integration test cases.
This commit is contained in:
@@ -58,7 +58,7 @@ import org.springframework.data.jpa.domain.sample.Address;
|
||||
import org.springframework.data.jpa.domain.sample.Role;
|
||||
import org.springframework.data.jpa.domain.sample.SpecialUser;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.sample.SampleSecurity.SampleSecurityContextHolder;
|
||||
import org.springframework.data.jpa.repository.sample.SampleEvaluationContextExtension.SampleSecurityContextHolder;
|
||||
import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -1591,7 +1591,7 @@ public class UserRepositoryTests {
|
||||
assertThat(result.isPresent(), is(true));
|
||||
assertThat(result.get(), is(firstUser));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see DATAJPA-564
|
||||
*/
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.data.jpa.repository.sample;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.jpa.repository.sample.SampleSecurity.SampleSecurityContextHolder;
|
||||
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
|
||||
import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport;
|
||||
|
||||
@@ -38,4 +37,88 @@ public class SampleEvaluationContextExtension extends EvaluationContextExtension
|
||||
public Map<String, Object> getProperties() {
|
||||
return Collections.singletonMap("principal", SampleSecurityContextHolder.getCurrent().getPrincipal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleSecurityContextHolder {
|
||||
|
||||
private static ThreadLocal<SampleAuthentication> auth = new ThreadLocal<SampleAuthentication>() {
|
||||
|
||||
protected SampleAuthentication initialValue() {
|
||||
return new SampleAuthentication(new SampleUser(-1, "anonymous"));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static SampleAuthentication getCurrent() {
|
||||
return auth.get();
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
auth.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleAuthentication {
|
||||
|
||||
private Object principal;
|
||||
|
||||
public SampleAuthentication(Object principal) {
|
||||
this.principal = principal;
|
||||
}
|
||||
|
||||
public Object getPrincipal() {
|
||||
return principal;
|
||||
}
|
||||
|
||||
public void setPrincipal(Object principal) {
|
||||
this.principal = principal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleUser {
|
||||
|
||||
private Object id;
|
||||
private String name;
|
||||
|
||||
public SampleUser(Object id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Object getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Object id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SampleUser withName(String name) {
|
||||
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleUser withId(Object id) {
|
||||
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.jpa.repository.sample;
|
||||
|
||||
/**
|
||||
* Minimalistic thread-scoped security context analogous to Spring Security for testing.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class SampleSecurity {
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleSecurityContextHolder {
|
||||
|
||||
private static ThreadLocal<SampleAuthentication> auth = new ThreadLocal<SampleAuthentication>() {
|
||||
|
||||
protected SampleAuthentication initialValue() {
|
||||
return new SampleAuthentication(new SampleUser(-1, "anonymous"));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static SampleAuthentication getCurrent() {
|
||||
return auth.get();
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
auth.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleAuthentication {
|
||||
|
||||
private Object principal;
|
||||
|
||||
public SampleAuthentication(Object principal) {
|
||||
this.principal = principal;
|
||||
}
|
||||
|
||||
public Object getPrincipal() {
|
||||
return principal;
|
||||
}
|
||||
|
||||
public void setPrincipal(Object principal) {
|
||||
this.principal = principal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public static class SampleUser {
|
||||
private Object id;
|
||||
private String name;
|
||||
|
||||
public SampleUser(Object id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Object getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Object id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SampleUser withName(String name) {
|
||||
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleUser withId(Object id) {
|
||||
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user