Java源码示例:test.mixin.Lockable
示例1
@Test
public void testLockingWorks() {
Lockable lockable = (Lockable) testBeanProxy;
assertFalse(lockable.locked());
// Invoke a non-advised method
testBeanProxy.getAge();
testBeanProxy.setName("");
lockable.lock();
try {
testBeanProxy.setName(" ");
fail("Should be locked");
}
catch (IllegalStateException ex) {
// expected
}
}
示例2
private void testTestBeanIntroduction(ProxyFactory pc) {
int newAge = 65;
ITestBean itb = (ITestBean) createProxy(pc);
itb.setAge(newAge);
assertEquals(newAge, itb.getAge());
Lockable lockable = (Lockable) itb;
assertFalse(lockable.locked());
lockable.lock();
assertEquals(newAge, itb.getAge());
try {
itb.setAge(1);
fail("Setters should fail when locked");
}
catch (LockedException ex) {
// ok
}
assertEquals(newAge, itb.getAge());
// Unlock
assertTrue(lockable.locked());
lockable.unlock();
itb.setAge(1);
assertEquals(1, itb.getAge());
}
示例3
@Test
public void testLockingWorks() {
Lockable lockable = (Lockable) testBeanProxy;
assertFalse(lockable.locked());
// Invoke a non-advised method
testBeanProxy.getAge();
testBeanProxy.setName("");
lockable.lock();
try {
testBeanProxy.setName(" ");
fail("Should be locked");
}
catch (IllegalStateException ex) {
// expected
}
}
示例4
private void testTestBeanIntroduction(ProxyFactory pc) {
int newAge = 65;
ITestBean itb = (ITestBean) createProxy(pc);
itb.setAge(newAge);
assertEquals(newAge, itb.getAge());
Lockable lockable = (Lockable) itb;
assertFalse(lockable.locked());
lockable.lock();
assertEquals(newAge, itb.getAge());
try {
itb.setAge(1);
fail("Setters should fail when locked");
}
catch (LockedException ex) {
// ok
}
assertEquals(newAge, itb.getAge());
// Unlock
assertTrue(lockable.locked());
lockable.unlock();
itb.setAge(1);
assertEquals(1, itb.getAge());
}
示例5
@Test
public void testLockingWorks() {
Assume.group(TestGroup.LONG_RUNNING);
Object introductionObject = ctx.getBean("introduction");
assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject));
Lockable lockable = (Lockable) testBeanProxy;
assertFalse(lockable.locked());
// Invoke a non-advised method
testBeanProxy.getAge();
testBeanProxy.setName("");
lockable.lock();
try {
testBeanProxy.setName(" ");
fail("Should be locked");
}
catch (IllegalStateException ex) {
// expected
}
}
示例6
/**
* Check that we can provide a common interceptor that will
* appear in the chain before "specific" interceptors,
* which are sourced from matching advisors
*/
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
ITestBean test1 = (ITestBean) bf.getBean("test1");
assertTrue(AopUtils.isAopProxy(test1));
Lockable lockable1 = (Lockable) test1;
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
assertEquals(0, nop.getCount());
ITestBean test2 = (ITestBean) bf.getBean("test2");
Lockable lockable2 = (Lockable) test2;
// Locking should be independent; nop is shared
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
// equals 2 calls on shared nop, because it's first
// and sees calls against the Lockable interface introduced
// by the specific advisor
assertEquals(2, nop.getCount());
lockable1.lock();
assertTrue(lockable1.locked());
assertFalse(lockable2.locked());
assertEquals(5, nop.getCount());
}
示例7
private void testTestBeanIntroduction(ProxyFactory pc) {
int newAge = 65;
ITestBean itb = (ITestBean) createProxy(pc);
itb.setAge(newAge);
assertEquals(newAge, itb.getAge());
Lockable lockable = (Lockable) itb;
assertFalse(lockable.locked());
lockable.lock();
assertEquals(newAge, itb.getAge());
try {
itb.setAge(1);
fail("Setters should fail when locked");
}
catch (LockedException ex) {
// ok
}
assertEquals(newAge, itb.getAge());
// Unlock
assertTrue(lockable.locked());
lockable.unlock();
itb.setAge(1);
assertEquals(1, itb.getAge());
}
示例8
@Test
public void testJdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals(0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
assertEquals("introductionUsingJdk", tb.getName());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例9
@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals("NOP should not have done any work yet", 0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例10
/**
* Check that we can provide a common interceptor that will
* appear in the chain before "specific" interceptors,
* which are sourced from matching advisors
*/
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
ITestBean test1 = (ITestBean) bf.getBean("test1");
assertTrue(AopUtils.isAopProxy(test1));
Lockable lockable1 = (Lockable) test1;
NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
ITestBean test2 = (ITestBean) bf.getBean("test2");
Lockable lockable2 = (Lockable) test2;
// Locking should be independent; nop is shared
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
// equals 2 calls on shared nop, because it's first and sees calls
// against the Lockable interface introduced by the specific advisor
assertEquals(2, nop1.getCount());
assertEquals(0, nop2.getCount());
lockable1.lock();
assertTrue(lockable1.locked());
assertFalse(lockable2.locked());
assertEquals(5, nop1.getCount());
assertEquals(0, nop2.getCount());
PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
assertEquals(5, nop1.getCount());
assertEquals(0, nop2.getCount());
packageVisibleMethod.doSomething();
assertEquals(6, nop1.getCount());
assertEquals(1, nop2.getCount());
assertTrue(packageVisibleMethod instanceof Lockable);
Lockable lockable3 = (Lockable) packageVisibleMethod;
lockable3.lock();
assertTrue(lockable3.locked());
lockable3.unlock();
assertFalse(lockable3.locked());
}
示例11
@Test
public void testJdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals(0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
assertEquals("introductionUsingJdk", tb.getName());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例12
@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals("NOP should not have done any work yet", 0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例13
/**
* Check that we can provide a common interceptor that will
* appear in the chain before "specific" interceptors,
* which are sourced from matching advisors
*/
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
ITestBean test1 = (ITestBean) bf.getBean("test1");
assertTrue(AopUtils.isAopProxy(test1));
Lockable lockable1 = (Lockable) test1;
NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
ITestBean test2 = (ITestBean) bf.getBean("test2");
Lockable lockable2 = (Lockable) test2;
// Locking should be independent; nop is shared
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
// equals 2 calls on shared nop, because it's first and sees calls
// against the Lockable interface introduced by the specific advisor
assertEquals(2, nop1.getCount());
assertEquals(0, nop2.getCount());
lockable1.lock();
assertTrue(lockable1.locked());
assertFalse(lockable2.locked());
assertEquals(5, nop1.getCount());
assertEquals(0, nop2.getCount());
PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
assertEquals(5, nop1.getCount());
assertEquals(0, nop2.getCount());
packageVisibleMethod.doSomething();
assertEquals(6, nop1.getCount());
assertEquals(1, nop2.getCount());
assertTrue(packageVisibleMethod instanceof Lockable);
Lockable lockable3 = (Lockable) packageVisibleMethod;
lockable3.lock();
assertTrue(lockable3.locked());
lockable3.unlock();
assertFalse(lockable3.locked());
}
示例14
@Test
public void testJdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals(0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
assertEquals("introductionUsingJdk", tb.getName());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例15
@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals("NOP should not have done any work yet", 0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
}
catch (LockedException ex) {
// Ok
}
}
示例16
@Test
public void testIntroductionWasMade() {
assertTrue(AopUtils.isAopProxy(testBeanProxy));
assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject));
assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable);
}
示例17
public void checkNotLocked(Lockable mixin) {
if (mixin.locked()) {
throw new IllegalStateException("locked");
}
}
示例18
@Test
public void testIntroductionWasMade() {
assertTrue(AopUtils.isAopProxy(testBeanProxy));
assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject));
assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable);
}
示例19
public void checkNotLocked(Lockable mixin) {
if (mixin.locked()) {
throw new IllegalStateException("locked");
}
}
示例20
@Test
public void testIntroductionWasMade() {
assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable);
}
示例21
public void checkNotLocked(Lockable mixin) {
if (mixin.locked()) {
throw new IllegalStateException("locked");
}
}