Java Code Examples for java.util.concurrent.CopyOnWriteArrayList#clear()

The following examples show how to use java.util.concurrent.CopyOnWriteArrayList#clear() . 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: DefaultLifecycleProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void dependencyStartedFirstAndIsSmartLifecycle() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>();
	TestSmartLifecycleBean beanNegative = TestSmartLifecycleBean.forStartupTests(-99, startedBeans);
	TestSmartLifecycleBean bean99 = TestSmartLifecycleBean.forStartupTests(99, startedBeans);
	TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forStartupTests(7, startedBeans);
	TestLifecycleBean simpleBean = TestLifecycleBean.forStartupTests(startedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("beanNegative", beanNegative);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.getBeanFactory().registerSingleton("bean99", bean99);
	context.getBeanFactory().registerSingleton("simpleBean", simpleBean);
	context.getBeanFactory().registerDependentBean("bean7", "simpleBean");
	context.refresh();
	context.stop();
	startedBeans.clear();
	// clean start so that simpleBean is included
	context.start();
	assertTrue(beanNegative.isRunning());
	assertTrue(bean99.isRunning());
	assertTrue(bean7.isRunning());
	assertTrue(simpleBean.isRunning());
	assertEquals(4, startedBeans.size());
	assertEquals(-99, getPhase(startedBeans.get(0)));
	assertEquals(7, getPhase(startedBeans.get(1)));
	assertEquals(0, getPhase(startedBeans.get(2)));
	assertEquals(99, getPhase(startedBeans.get(3)));
	context.stop();
}
 
Example 2
Source File: DefaultLifecycleProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void dependencyStartedFirstAndIsSmartLifecycle() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>();
	TestSmartLifecycleBean beanNegative = TestSmartLifecycleBean.forStartupTests(-99, startedBeans);
	TestSmartLifecycleBean bean99 = TestSmartLifecycleBean.forStartupTests(99, startedBeans);
	TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forStartupTests(7, startedBeans);
	TestLifecycleBean simpleBean = TestLifecycleBean.forStartupTests(startedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("beanNegative", beanNegative);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.getBeanFactory().registerSingleton("bean99", bean99);
	context.getBeanFactory().registerSingleton("simpleBean", simpleBean);
	context.getBeanFactory().registerDependentBean("bean7", "simpleBean");
	context.refresh();
	context.stop();
	startedBeans.clear();
	// clean start so that simpleBean is included
	context.start();
	assertTrue(beanNegative.isRunning());
	assertTrue(bean99.isRunning());
	assertTrue(bean7.isRunning());
	assertTrue(simpleBean.isRunning());
	assertEquals(4, startedBeans.size());
	assertEquals(-99, getPhase(startedBeans.get(0)));
	assertEquals(7, getPhase(startedBeans.get(1)));
	assertEquals(0, getPhase(startedBeans.get(2)));
	assertEquals(99, getPhase(startedBeans.get(3)));
	context.stop();
}
 
Example 3
Source File: CopyOnWriteArrayListTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Cloned list is equal
 */
public void testClone() {
    CopyOnWriteArrayList l1 = populatedArray(SIZE);
    CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
    assertEquals(l1, l2);
    l1.clear();
    assertFalse(l1.equals(l2));
}
 
Example 4
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void dependencyStartedFirstAndIsSmartLifecycle() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
	TestSmartLifecycleBean beanNegative = TestSmartLifecycleBean.forStartupTests(-99, startedBeans);
	TestSmartLifecycleBean bean99 = TestSmartLifecycleBean.forStartupTests(99, startedBeans);
	TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forStartupTests(7, startedBeans);
	TestLifecycleBean simpleBean = TestLifecycleBean.forStartupTests(startedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("beanNegative", beanNegative);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.getBeanFactory().registerSingleton("bean99", bean99);
	context.getBeanFactory().registerSingleton("simpleBean", simpleBean);
	context.getBeanFactory().registerDependentBean("bean7", "simpleBean");
	context.refresh();
	context.stop();
	startedBeans.clear();
	// clean start so that simpleBean is included
	context.start();
	assertTrue(beanNegative.isRunning());
	assertTrue(bean99.isRunning());
	assertTrue(bean7.isRunning());
	assertTrue(simpleBean.isRunning());
	assertEquals(4, startedBeans.size());
	assertEquals(-99, getPhase(startedBeans.get(0)));
	assertEquals(7, getPhase(startedBeans.get(1)));
	assertEquals(0, getPhase(startedBeans.get(2)));
	assertEquals(99, getPhase(startedBeans.get(3)));
	context.stop();
}
 
Example 5
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSubListAndStructuralChanges() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    List<String> bcd = list.subList(1, 4);
    list.clear();
    try {
        bcd.get(1);
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}
 
Example 6
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSubListAndSizePreservingStructuralChanges() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    List<String> bcd = list.subList(1, 4);
    list.clear();
    list.addAll(Arrays.asList("A", "B", "C", "D", "E"));
    try {
        bcd.get(1);
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}
 
Example 7
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSubListIteratorGetsSnapshot() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    Iterator<String> bcd = list.subList(1, 4).iterator();
    list.clear();
    assertEquals("b", bcd.next());
    assertEquals("c", bcd.next());
    assertEquals("d", bcd.next());
    assertFalse(bcd.hasNext());
}
 
Example 8
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testListIterator() {
    CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
    list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
    ListIterator<String> i = list.listIterator(5);
    list.clear();

    assertEquals(5, i.nextIndex());
    assertEquals(4, i.previousIndex());
    assertEquals("e", i.previous());
    assertEquals(4, i.nextIndex());
    assertEquals(3, i.previousIndex());
    assertTrue(i.hasNext());
    assertTrue(i.hasPrevious());
    assertEquals("d", i.previous());
    assertEquals(3, i.nextIndex());
    assertEquals(2, i.previousIndex());
    assertTrue(i.hasNext());
    assertTrue(i.hasPrevious());
    assertEquals("c", i.previous());
    assertEquals(2, i.nextIndex());
    assertEquals(1, i.previousIndex());
    assertTrue(i.hasNext());
    assertTrue(i.hasPrevious());
    assertEquals("b", i.previous());
    assertEquals(1, i.nextIndex());
    assertEquals(0, i.previousIndex());
    assertTrue(i.hasNext());
    assertTrue(i.hasPrevious());
    assertEquals("a", i.previous());
    assertEquals(0, i.nextIndex());
    assertEquals(-1, i.previousIndex());
    assertTrue(i.hasNext());
    assertFalse(i.hasPrevious());
    try {
        i.previous();
        fail();
    } catch (NoSuchElementException expected) {
    }
}
 
Example 9
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Cloned list is equal
 */
public void testClone() {
    CopyOnWriteArrayList l1 = populatedArray(SIZE);
    CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
    assertEquals(l1, l2);
    l1.clear();
    assertFalse(l1.equals(l2));
}
 
Example 10
Source File: RpcClientInvokerCache.java    From jim-framework with Apache License 2.0 4 votes vote down vote up
public static void clearNotConnectedHandler() {
    CopyOnWriteArrayList<RpcClientInvoker> notConnectedHandlersClone = getNotConnectedHandlersClone();
    notConnectedHandlersClone.clear();
    notConnectedHandlers=notConnectedHandlersClone;
}
 
Example 11
Source File: RpcClientInvokerCache.java    From jim-framework with Apache License 2.0 4 votes vote down vote up
public static void clear(){
    CopyOnWriteArrayList<RpcClientInvoker> newHandlers = getConnectedHandlersClone();
    newHandlers.clear();
    connectedHandlers=newHandlers;
}
 
Example 12
Source File: CopyOnWriteArrayListTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * clear removes all elements from the list
 */
public void testClear() {
    CopyOnWriteArrayList full = populatedArray(SIZE);
    full.clear();
    assertEquals(0, full.size());
}
 
Example 13
Source File: FrontAndChats_IntegrationTest.java    From live-chat-engine with Apache License 2.0 4 votes vote down vote up
private void test_chats_ChangeUserPrivs() throws Exception {
	
	ChatService chats1 = frontApp1.chats;
	ChatService chats2 = frontApp2.chats;
	
	CopyOnWriteArrayList<Future<?>> asyncFutures = new CopyOnWriteArrayList<>();
	frontApp1.addAsyncListener((f) -> asyncFutures.add(f));
	
	long userId1 = 100;
	long userId2 = 101;
	User user1 = new User(userId1);
	String uid = null;
	String userEmail2 = "root@system";
	
	//create acc
	pushToSecurityContext(user1);
	try {
		uid = chats1.createAccByUser("changeUserPrivs");
	}finally {
		popUserFromSecurityContext();
	}
	lastFrom(asyncFutures).get();
	asyncFutures.clear();
	String serverUrl = chats1.getServerByAcc(uid).httpUrl;
	ChatsApp chatApp = serverUrl.equals(chatsUrl1)? chatApp1 : chatApp2;
	
	assertEquals(set(CHAT_OWNER), chats2.getAccPrivilegesForUser(uid, userId1));
	
	
	//create op
	{
		pushToSecurityContext(user1);
		try {
			chats1.addUserPrivileges(uid, userId2, set(CHAT_OPERATOR));
		}finally {
			popUserFromSecurityContext();
		}
		
		assertTrue(asyncFutures.size() > 0);
		lastFrom(asyncFutures).get();
		
		//оператор появился во чатах
		pushToSecurityContext_SYSTEM_USER();
		try {
			assertEquals(set(CHAT_OPERATOR), chats2.getAccPrivilegesForUser(uid, userId2));
			ChatOperator op = chatApp.chats.getOperator(uid, userId2);
			assertNotNull(op);
			assertEquals(userEmail2, op.email);
		}finally {
			popUserFromSecurityContext();
		}


	}
	
	//remove op
	{
		pushToSecurityContext(user1);
		try {
			chats1.removeUserPrivileges(uid, userId2, set(CHAT_OPERATOR));
		}finally {
			popUserFromSecurityContext();
		}
		
		assertTrue(asyncFutures.size() > 0);
		lastFrom(asyncFutures).get();
		
		pushToSecurityContext_SYSTEM_USER();
		try {
			assertEquals(set(), chats2.getAccPrivilegesForUser(uid, userId2));
			assertNull(chatApp.chats.getOperator(uid, userId2));
		}finally {
			popUserFromSecurityContext();
		}
	}
	
	
	
}
 
Example 14
Source File: CopyOnWriteArrayListTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * clear removes all elements from the list
 */
public void testClear() {
    CopyOnWriteArrayList full = populatedArray(SIZE);
    full.clear();
    assertEquals(0, full.size());
}