1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @summary Test ServiceLoader with two iterators, interleaving their use 27 * to test that they don't interfere with each other 28 * @run testng TwoIterators 29 */ 30 31import java.nio.file.Files; 32import java.nio.file.Path; 33import java.nio.file.Paths; 34import java.util.Arrays; 35import java.util.Iterator; 36import java.util.ServiceLoader; 37 38import org.testng.annotations.BeforeClass; 39import org.testng.annotations.Test; 40import static org.testng.Assert.*; 41 42public class TwoIterators { 43 44 // service type 45 public static interface S { } 46 47 // service provider implementations 48 public static class S1 implements S { } 49 public static class S2 implements S { } 50 51 private ClassLoader testClassLoader; 52 53 // creates the services configuration file and sets the ClassLoader 54 @BeforeClass 55 void setup() throws Exception { 56 String classes = System.getProperty("test.classes"); 57 Path dir = Paths.get(classes, "META-INF", "services"); 58 Files.createDirectories(dir); 59 Path config = dir.resolve(S.class.getName()); 60 Files.write(config, Arrays.asList(S1.class.getName(), S2.class.getName())); 61 62 this.testClassLoader = TwoIterators.class.getClassLoader(); 63 } 64 65 @Test 66 public void testSequentialUse1() { 67 ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader); 68 69 Iterator<S> iterator1 = sl.iterator(); 70 iterator1.next(); 71 iterator1.next(); 72 assertFalse(iterator1.hasNext()); 73 74 Iterator<S> iterator2 = sl.iterator(); 75 iterator2.next(); 76 iterator2.next(); 77 assertFalse(iterator2.hasNext()); 78 } 79 80 @Test 81 public void testSequentialUse2() { 82 ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader); 83 84 Iterator<S> iterator1 = sl.iterator(); 85 Iterator<S> iterator2 = sl.iterator(); 86 87 iterator1.next(); 88 iterator1.next(); 89 assertFalse(iterator1.hasNext()); 90 91 iterator2.next(); 92 iterator2.next(); 93 assertFalse(iterator2.hasNext()); 94 } 95 96 @Test 97 public void testInterleaved1() { 98 ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader); 99 100 Iterator<S> iterator1 = sl.iterator(); 101 Iterator<S> iterator2 = sl.iterator(); 102 103 iterator1.next(); 104 iterator2.next(); 105 iterator1.next(); 106 iterator2.next(); 107 assertFalse(iterator1.hasNext()); 108 assertFalse(iterator2.hasNext()); 109 } 110 111 @Test 112 public void testInterleaved2() { 113 ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader); 114 115 Iterator<S> iterator1 = sl.iterator(); 116 iterator1.next(); 117 118 Iterator<S> iterator2 = sl.iterator(); 119 120 assertTrue(iterator1.hasNext()); 121 assertTrue(iterator2.hasNext()); 122 123 iterator1.next(); 124 iterator2.next(); 125 iterator2.next(); 126 127 assertFalse(iterator1.hasNext()); 128 assertFalse(iterator2.hasNext()); 129 } 130 131 @Test 132 public void testInterleaved3() { 133 ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader); 134 135 Iterator<S> iterator1 = sl.iterator(); 136 iterator1.next(); 137 138 Iterator<S> iterator2 = sl.iterator(); 139 140 assertTrue(iterator2.hasNext()); 141 assertTrue(iterator1.hasNext()); 142 143 iterator2.next(); 144 iterator2.next(); 145 iterator1.next(); 146 147 assertFalse(iterator1.hasNext()); 148 assertFalse(iterator2.hasNext()); 149 } 150} 151 152