--- /dev/null 2011-08-08 08:40:49.685541141 -0700 +++ new/test/java/util/functions/Mapper/MappersTest.java 2011-08-09 10:55:32.000000000 -0700 @@ -0,0 +1,175 @@ +/* + * 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 Mappers SAM utils + * @author Mike Duigou + */ + +import java.io.*; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Objects; +import java.util.functions.*; + +public class MappersTest { + + private static void testIdentity() { + check(null == Mappers.identity().map(null)); + check(MappersTest.class == Mappers.identity().map(MappersTest.class)); + } + + private static void testToString() { + check("null".equals(Mappers.string().map(null))); + check("1".equals(Mappers.string().map(Integer.valueOf(1)))); + check("surprise!".equals(Mappers.string().map(new Object(){public String toString(){return "surprise!";}}))); + } + + private static void testConstant() { + check(MappersTest.class == Mappers.constant(MappersTest.class).map(null)); + check(MappersTest.class == Mappers.constant(MappersTest.class).map("1")); + } + + public static class Goomba { + public final int value; + public Goomba(Integer x) { + value = x; + } + } + + private static void testInstantiate() { + check(1 == Mappers.instantiate(Integer.class, Goomba.class).map(1).value); + check(2 == Mappers.instantiate(Integer.class, Goomba.class).map(2).value); + } + + private static void testForPredicate() { + Mapper mapper = Mappers.forPredicate(#{ Integer x -> x != 0}, "true", "false"); + + check("false".equals(mapper.map(0))); + check("true".equals(mapper.map(1))); + } + + private static void testForMap() { + Map truths = new HashMap<>(); + truths.put(Integer.valueOf(0), "sky"); + truths.put(Integer.valueOf(1), "ice"); + + Mapper mapper = Mappers.forMap(truths); + + check("sky".equals(mapper.map(0))); + check("ice".equals(mapper.map(1))); + THROWS(IllegalArgumentException.class, #{ String result = mapper.map(999); fail();}); + } + + private static void testForMapDefault() { + Map truths = new HashMap<>(); + truths.put(Integer.valueOf(0), "sky"); + truths.put(Integer.valueOf(1), "ice"); + + Mapper mapper = Mappers.forMap(truths, "fire"); + + check("sky".equals(mapper.map(0))); + check("ice".equals(mapper.map(1))); + check("fire".equals(mapper.map(999))); + check("fire".equals(mapper.map(Integer.MAX_VALUE))); + } + + private static void testChain() { + Mapper first = Mappers.forPredicate(#{ Integer x -> x != 0}, true, false); + Mapper second = Mappers.forPredicate(#{ Boolean x -> !x}, "false", "true"); + Mapper mapper = Mappers.chain(first,second); + + check("false".equals(mapper.map(0))); + check("true".equals(mapper.map(1))); + } + + private static void testSubstitute() { + Mapper ints = Mappers.substitute(Integer.valueOf(3),Integer.valueOf(999)); + Mapper strings = Mappers.substitute("hello","bonjour"); + Mapper stringNullIn = Mappers.substitute(null, "default"); + Mapper stringNullOut = Mappers.substitute("default", null); + + check(1 == ints.map(1)); + check(999 == ints.map(3)); + check("gutentag".equals(strings.map("gutentag"))); + check("bonjour".equals(strings.map("hello"))); + check("gutentag".equals(stringNullIn.map("gutentag"))); + check("default".equals(stringNullIn.map(null))); + check("gutentag".equals(stringNullOut.map("gutentag"))); + check(null == stringNullOut.map("default")); + } + + public static void RealMain(String ... args) { + testIdentity(); + testToString(); + testConstant(); + testInstantiate(); + testForPredicate(); + testForMap(); + testForMapDefault(); + testChain(); + testSubstitute(); + } + + //--------------------- 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); }} +}