Path: blob/master/test/jdk/java/util/ServiceLoader/ReloadTest.java
41149 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @modules java.scripting26* @library modules classpath/pearscript27* @build ReloadTest org.pear.PearScript org.pear.PearScriptEngineFactory bananascript/*28* @run testng/othervm ReloadTest29* @summary Basic test of ServiceLoader.reload30*/3132import java.util.ConcurrentModificationException;33import java.util.Iterator;34import java.util.List;35import java.util.ServiceLoader;36import java.util.ServiceLoader.Provider;37import java.util.Spliterator;38import java.util.stream.Collectors;39import java.util.stream.Stream;40import static java.util.ServiceLoader.*;41import javax.script.ScriptEngineFactory;42import org.testng.annotations.Test;43import static org.testng.Assert.*;4445@Test46public class ReloadTest {4748public void testReload() {49ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);50List<String> names1 = sl.stream()51.map(Provider::get)52.map(ScriptEngineFactory::getEngineName)53.collect(Collectors.toList());54assertFalse(names1.isEmpty());55sl.reload();56List<String> names2 = sl.stream()57.map(Provider::get)58.map(ScriptEngineFactory::getEngineName)59.collect(Collectors.toList());60assertEquals(names1, names2);61}6263@Test(expectedExceptions = { ConcurrentModificationException.class })64public void testIteratorHasNext() {65ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);66Iterator<ScriptEngineFactory> iterator = sl.iterator();67sl.reload();68iterator.hasNext();69}7071@Test(expectedExceptions = { ConcurrentModificationException.class })72public void testIteratorNext() {73ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);74Iterator<ScriptEngineFactory> iterator = sl.iterator();75assertTrue(iterator.hasNext());76sl.reload();77iterator.next();78}7980@Test(expectedExceptions = { ConcurrentModificationException.class })81public void testStreamFindAny() {82ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);83Stream<Provider<ScriptEngineFactory>> stream = sl.stream();84sl.reload();85stream.findAny();86}8788@Test(expectedExceptions = { ConcurrentModificationException.class })89public void testSpliteratorTryAdvance() {90ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);91Stream<Provider<ScriptEngineFactory>> stream = sl.stream();92Spliterator<Provider<ScriptEngineFactory>> spliterator = stream.spliterator();93sl.reload();94spliterator.tryAdvance(System.out::println);95}9697}9899100