|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Operations: a framework for reusable algorithms. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2016 - 2019 SciJava Ops developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.ops.types; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +import java.lang.reflect.Type; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Observer; |
| 38 | + |
| 39 | +import org.junit.After; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.scijava.Context; |
| 43 | +import org.scijava.convert.ConvertService; |
| 44 | +import org.scijava.convert.Converter; |
| 45 | +import org.scijava.util.Types; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests {@link NilConverter}. |
| 49 | + * |
| 50 | + * @author Curtis Rueden |
| 51 | + */ |
| 52 | +public class NilConverterTest { |
| 53 | + |
| 54 | + private ConvertService convert; |
| 55 | + |
| 56 | + /** Sets up a SciJava context and injects needed services. */ |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + convert = new Context(ConvertService.class).service(ConvertService.class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Disposes of the {@link Context} that was initialized in {@link #setUp()}. |
| 64 | + */ |
| 65 | + @After |
| 66 | + public synchronized void cleanUp() { |
| 67 | + if (convert != null) { |
| 68 | + convert.context().dispose(); |
| 69 | + convert = null; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testCanConvert() { |
| 75 | + final Nil<?> nil = Nil.of(Observer.class); |
| 76 | + assertCanConvert(nil, Observer.class); |
| 77 | + assertCanConvert(nil, List.class); |
| 78 | + assertCanConvert(nil, new Nil<Map<?, ?>>() {}.getType()); |
| 79 | + // TODO: Enable after ConvertService is rewritten to fully |
| 80 | + // support generic types. Right now, CastingConverter steals this. |
| 81 | +// assertCanConvert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType()); |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings("cast") |
| 85 | + @Test |
| 86 | + public void testConvert() { |
| 87 | + final Nil<?> nil = Nil.of(Observer.class); |
| 88 | + assertConvert(nil, Observer.class); |
| 89 | + assertConvert(nil, List.class); |
| 90 | + assertConvert(nil, new Nil<Map<?, ?>>() {}.getType()); |
| 91 | + // TODO: Enable after ConvertService is rewritten to fully |
| 92 | + // support generic types. Right now, CastingConverter steals this. |
| 93 | +// assertConvert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType()); |
| 94 | + final Converter<?, ?> converter = convert.getHandler(nil, Observer.class); |
| 95 | + assertTrue(converter instanceof NilConverter); |
| 96 | + // TODO: Enable after Nil proxying is improved to support non-interfaces. |
| 97 | +// assertTrue(converter.convert(nil, String.class) instanceof String); |
| 98 | + assertTrue(converter.convert(nil, List.class) instanceof List); |
| 99 | + assertTrue(converter.convert(nil, new Nil<Map<?, ?>>() {}.getType()) instanceof Map); |
| 100 | + // TODO: Enable after ConvertService is rewritten to fully |
| 101 | + // support generic types. Right now, CastingConverter steals this. |
| 102 | +// assertTrue(converter.convert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType()) instanceof Nil); |
| 103 | + } |
| 104 | + |
| 105 | + private void assertCanConvert(final Nil<?> nil, final Type destType) { |
| 106 | + final Converter<?, ?> converter = convert.getHandler(nil, destType); |
| 107 | + assertTrue(converter instanceof NilConverter); |
| 108 | + assertTrue(converter.canConvert(nil, destType)); |
| 109 | + } |
| 110 | + |
| 111 | + private void assertConvert(final Nil<?> nil, final Type destType) { |
| 112 | + final Converter<?, ?> converter = convert.getHandler(nil, destType); |
| 113 | + assertTrue(converter instanceof NilConverter); |
| 114 | + final Object o = converter.convert(nil, destType); |
| 115 | + assertTrue(Types.raw(destType).isAssignableFrom(o.getClass())); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments