< prev index next >

test/java/util/Iterator/PrimitiveIteratorDefaults.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 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 import org.testng.annotations.Test;
  25 
  26 import java.util.PrimitiveIterator;
  27 import java.util.function.Consumer;
  28 import java.util.function.DoubleConsumer;
  29 import java.util.function.IntConsumer;
  30 import java.util.function.LongConsumer;
  31 
  32 import static org.testng.Assert.assertNotNull;
  33 import static org.testng.Assert.assertTrue;


  34 
  35 /**
  36  * @test
  37  * @run testng PrimitiveIteratorDefaults
  38  * @summary test default methods on PrimitiveIterator
  39  */
  40 @Test
  41 public class PrimitiveIteratorDefaults {
  42 
  43     public void testIntForEachRemainingWithNull() {
  44         PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {
  45             @Override
  46             public int nextInt() {
  47                 return 0;
  48             }
  49 
  50             @Override
  51             public boolean hasNext() {
  52                 return false;
  53             }
  54         };
  55 
  56         executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));
  57         executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));
  58     }
  59 
  60     public void testLongForEachRemainingWithNull() {
  61         PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
  62             @Override
  63             public long nextLong() {
  64                 return 0;
  65             }
  66 
  67             @Override
  68             public boolean hasNext() {
  69                 return false;
  70             }
  71         };
  72 
  73         executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
  74         executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
  75     }
  76 
  77     public void testDoubleForEachRemainingWithNull() {
  78         PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
  79             @Override
  80             public double nextDouble() {
  81                 return 0;
  82             }
  83 
  84             @Override
  85             public boolean hasNext() {
  86                 return false;
  87             }
  88         };
  89 
  90         executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
  91         executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
  92     }
  93 
  94     private void executeAndCatch(Runnable r) {
  95         executeAndCatch(NullPointerException.class, r);
  96     }
  97 
  98     private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {
  99         Exception caught = null;
 100         try {
 101             r.run();
 102         }
 103         catch (Exception e) {
 104             caught = e;
 105         }
 106 
 107         assertNotNull(caught,
 108                       String.format("No Exception was thrown, expected an Exception of %s to be thrown",
 109                                     expected.getName()));
 110         assertTrue(expected.isInstance(caught),
 111                    String.format("Exception thrown %s not an instance of %s",
 112                                  caught.getClass().getName(), expected.getName()));
 113     }
 114 
 115 }
   1 /*
   2  * Copyright (c) 2013, 2017, 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 import java.util.PrimitiveIterator;
  25 import java.util.function.Consumer;
  26 import java.util.function.DoubleConsumer;
  27 import java.util.function.IntConsumer;
  28 import java.util.function.LongConsumer;
  29 
  30 import org.testng.Assert.ThrowingRunnable;
  31 import org.testng.annotations.Test;
  32 
  33 import static org.testng.Assert.assertThrows;
  34 
  35 /**
  36  * @test
  37  * @run testng PrimitiveIteratorDefaults
  38  * @summary test default methods on PrimitiveIterator
  39  */
  40 @Test
  41 public class PrimitiveIteratorDefaults {
  42 
  43     public void testIntForEachRemainingWithNull() {
  44         PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {
  45             @Override
  46             public int nextInt() {
  47                 return 0;
  48             }
  49 
  50             @Override
  51             public boolean hasNext() {
  52                 return false;
  53             }
  54         };
  55 
  56         assertThrowsNPE(() -> i.forEachRemaining((IntConsumer) null));
  57         assertThrowsNPE(() -> i.forEachRemaining((Consumer<Integer>) null));
  58     }
  59 
  60     public void testLongForEachRemainingWithNull() {
  61         PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
  62             @Override
  63             public long nextLong() {
  64                 return 0;
  65             }
  66 
  67             @Override
  68             public boolean hasNext() {
  69                 return false;
  70             }
  71         };
  72 
  73         assertThrowsNPE(() -> i.forEachRemaining((LongConsumer) null));
  74         assertThrowsNPE(() -> i.forEachRemaining((Consumer<Long>) null));
  75     }
  76 
  77     public void testDoubleForEachRemainingWithNull() {
  78         PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
  79             @Override
  80             public double nextDouble() {
  81                 return 0;
  82             }
  83 
  84             @Override
  85             public boolean hasNext() {
  86                 return false;
  87             }
  88         };
  89 
  90         assertThrowsNPE(() -> i.forEachRemaining((DoubleConsumer) null));
  91         assertThrowsNPE(() -> i.forEachRemaining((Consumer<Double>) null));













  92     }
  93 
  94     private void assertThrowsNPE(ThrowingRunnable r) {
  95         assertThrows(NullPointerException.class, r);




  96     }
  97 
  98 }
< prev index next >