Path: blob/master/test/jdk/java/util/ServiceLoader/TwoIterators.java
41149 views
/*1* Copyright (c) 2015, 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* @summary Test ServiceLoader with two iterators, interleaving their use26* to test that they don't interfere with each other27* @run testng TwoIterators28*/2930import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.util.Arrays;34import java.util.Iterator;35import java.util.ServiceLoader;3637import org.testng.annotations.BeforeClass;38import org.testng.annotations.Test;39import static org.testng.Assert.*;4041public class TwoIterators {4243// service type44public static interface S { }4546// service provider implementations47public static class S1 implements S { }48public static class S2 implements S { }4950private ClassLoader testClassLoader;5152// creates the services configuration file and sets the ClassLoader53@BeforeClass54void setup() throws Exception {55String classes = System.getProperty("test.classes");56Path dir = Paths.get(classes, "META-INF", "services");57Files.createDirectories(dir);58Path config = dir.resolve(S.class.getName());59Files.write(config, Arrays.asList(S1.class.getName(), S2.class.getName()));6061this.testClassLoader = TwoIterators.class.getClassLoader();62}6364@Test65public void testSequentialUse1() {66ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);6768Iterator<S> iterator1 = sl.iterator();69iterator1.next();70iterator1.next();71assertFalse(iterator1.hasNext());7273Iterator<S> iterator2 = sl.iterator();74iterator2.next();75iterator2.next();76assertFalse(iterator2.hasNext());77}7879@Test80public void testSequentialUse2() {81ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);8283Iterator<S> iterator1 = sl.iterator();84Iterator<S> iterator2 = sl.iterator();8586iterator1.next();87iterator1.next();88assertFalse(iterator1.hasNext());8990iterator2.next();91iterator2.next();92assertFalse(iterator2.hasNext());93}9495@Test96public void testInterleaved1() {97ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);9899Iterator<S> iterator1 = sl.iterator();100Iterator<S> iterator2 = sl.iterator();101102iterator1.next();103iterator2.next();104iterator1.next();105iterator2.next();106assertFalse(iterator1.hasNext());107assertFalse(iterator2.hasNext());108}109110@Test111public void testInterleaved2() {112ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);113114Iterator<S> iterator1 = sl.iterator();115iterator1.next();116117Iterator<S> iterator2 = sl.iterator();118119assertTrue(iterator1.hasNext());120assertTrue(iterator2.hasNext());121122iterator1.next();123iterator2.next();124iterator2.next();125126assertFalse(iterator1.hasNext());127assertFalse(iterator2.hasNext());128}129130@Test131public void testInterleaved3() {132ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);133134Iterator<S> iterator1 = sl.iterator();135iterator1.next();136137Iterator<S> iterator2 = sl.iterator();138139assertTrue(iterator2.hasNext());140assertTrue(iterator1.hasNext());141142iterator2.next();143iterator2.next();144iterator1.next();145146assertFalse(iterator1.hasNext());147assertFalse(iterator2.hasNext());148}149}150151152153