下面列出了java.util.concurrent.CopyOnWriteArrayList#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
@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();
}
/**
* 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));
}
@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();
}
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) {
}
}
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) {
}
}
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());
}
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) {
}
}
/**
* 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));
}
public static void clearNotConnectedHandler() {
CopyOnWriteArrayList<RpcClientInvoker> notConnectedHandlersClone = getNotConnectedHandlersClone();
notConnectedHandlersClone.clear();
notConnectedHandlers=notConnectedHandlersClone;
}
public static void clear(){
CopyOnWriteArrayList<RpcClientInvoker> newHandlers = getConnectedHandlersClone();
newHandlers.clear();
connectedHandlers=newHandlers;
}
/**
* clear removes all elements from the list
*/
public void testClear() {
CopyOnWriteArrayList full = populatedArray(SIZE);
full.clear();
assertEquals(0, full.size());
}
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 = "[email protected]";
//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();
}
}
}
/**
* clear removes all elements from the list
*/
public void testClear() {
CopyOnWriteArrayList full = populatedArray(SIZE);
full.clear();
assertEquals(0, full.size());
}