/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Basic test for Blocks SAM utils * @author Mike Duigou */ import java.io.*; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.functions.*; public class BlocksTest { private static void testNop() { Blocks.nop().apply(null); Blocks.nop().apply(BlocksTest.class); pass(); } private static void testNullCheckSuccess() { Blocks.requireNonNull().apply(BlocksTest.class); pass(); } private static void testNullCheckFailure() { Blocks.requireNonNull().apply(null); pass(); } private static void testChain2() { final boolean results[] = new boolean[2]; Block block1 = #{ Boolean b -> results[0] = b;}; Block block2 = #{ Boolean b -> results[1] = b;}; Blocks.chain(block1, block2).apply(true); check(results[0] && results[1]); } private static void testChainVarArgs() { final boolean results[] = new boolean[3]; Block blocks[] = (Block[]) new Block[]{ (Block) #{ Boolean b -> results[0] = b;}, (Block) #{ Boolean b -> results[1] = b;}, (Block) #{Boolean b -> results[2] = b;} }; Blocks.chain( blocks ).apply(true); check(results[0] && results[1] && results[2]); } private static void testChain2VarArgs() { final boolean results[] = new boolean[3]; Block block0 = #{ Boolean b -> results[0] = b;}; Block block1 = #{ Boolean b -> results[1] = b;}; Block block2 = #{ Boolean b -> results[2] = b;}; Block blocks[] = (Block[]) new Block[]{ block1, block2}; Blocks.chain(block0, blocks).apply(true); check(results[0] && results[1] && results[2]); } private static void testChainIterable() { final boolean results[] = new boolean[3]; final Block blocks[] = (Block[]) new Block[] { (Block) #{ Boolean b -> results[0] = b;}, (Block) #{ Boolean b -> results[1] = b;}, (Block) #{ Boolean b -> results[2] = b;}, }; Blocks.chain(Arrays.asList(blocks)).apply(true); check(results[0] && results[1] && results[2]); } private static void testChainIterable2() { final boolean results[] = new boolean[3]; Block blocks[] = new Block[]{ (Block) #{ Boolean b -> results[1] = b;}, (Block) #{ Boolean b -> results[2] = b;} }; List> listed = Arrays.asList(blocks); Blocks.chain( (Block) #{ Boolean b -> results[0] = b;}, listed).apply(true); check(results[0] && results[1] && results[2]); } private static void testRepeatTimes() { final int results[] = new int[] { 0 }; Blocks.repeat( (Block) #{ Integer x -> results[0] += x;}, 5).apply(1); check(results[0] == 5); } private static void testRepeatPredicate() { final int results[] = new int[] { 0 }; Blocks.repeatWhile( (Block) #{ Integer x -> results[0] += x;}, (Predicate) #{ Integer x -> results[0] < 5 }).apply(1); check(results[0] == 5); results[0] = 0; Blocks.repeatUntil( (Block) #{ Integer x -> results[0] += x;}, (Predicate) #{ Integer x -> results[0] < 5 }).apply(1); check(results[0] == 5); } public static void RealMain(String ... args) { testNop(); testNullCheckSuccess(); THROWS( NullPointerException.class, #{ testNullCheckFailure(); fail();} ); testChain2(); testChainVarArgs(); testChain2VarArgs(); testChainIterable(); testChainIterable2(); testRepeatTimes(); testRepeatPredicate(); } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0; static void pass() { passed++; } static void fail() { failed++; (new Exception("stack dump")).printStackTrace(System.err);} static void fail(String msg) { System.out.println(msg); fail(); } static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } static void check(boolean cond) { if (cond) pass(); else fail(); } static void equal(Object x, Object y) { if (Objects.equals(x,y)) pass(); else {System.out.println(x + " not equal to " + y); fail();}} static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} public static void main(String[] args) throws Throwable { try { RealMain(args); } catch (Throwable t) { unexpected(t); } System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); if (failed > 0) throw new Exception("Some tests failed"); } private interface Fun {void f() throws Throwable;} static void THROWS(Class k, Fun... fs) { for (Fun f : fs) try { f.f(); fail("Expected " + k.getName() + " not thrown"); } catch (Throwable t) { if (k.isAssignableFrom(t.getClass())) pass(); else unexpected(t);}} static byte[] serializedForm(Object obj) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(obj); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); }} static Object readObject(byte[] bytes) throws IOException, ClassNotFoundException { InputStream is = new ByteArrayInputStream(bytes); return new ObjectInputStream(is).readObject();} @SuppressWarnings("unchecked") static T serialClone(T obj) { try { return (T) readObject(serializedForm(obj)); } catch (Exception e) { throw new RuntimeException(e); }} }