test.mixin.Lockable Java Examples

The following examples show how to use test.mixin.Lockable. 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: DeclareParentsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
	}
}
 
Example #2
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #3
Source File: AdvisorAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example #4
Source File: DeclareParentsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
	}
}
 
Example #5
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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());
}
 
Example #6
Source File: DeclareParentsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
	}
}
 
Example #7
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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());
}
 
Example #8
Source File: AdvisorAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example #9
Source File: BeanNameAutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
	}
}
 
Example #10
Source File: BeanNameAutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
	}
}
 
Example #11
Source File: AdvisorAutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example #12
Source File: BeanNameAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
	}
}
 
Example #13
Source File: BeanNameAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
	}
}
 
Example #14
Source File: BeanNameAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #15
Source File: BeanNameAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #16
Source File: DeclareParentsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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);
}
 
Example #17
Source File: DeclareParentsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void checkNotLocked(Lockable mixin) {
	if (mixin.locked()) {
		throw new IllegalStateException("locked");
	}
}
 
Example #18
Source File: DeclareParentsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testIntroductionWasMade() {
	assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable);
}
 
Example #19
Source File: DeclareParentsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void checkNotLocked(Lockable mixin) {
	if (mixin.locked()) {
		throw new IllegalStateException("locked");
	}
}
 
Example #20
Source File: DeclareParentsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void checkNotLocked(Lockable mixin) {
	if (mixin.locked()) {
		throw new IllegalStateException("locked");
	}
}
 
Example #21
Source File: DeclareParentsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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);
}