|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.parse; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertFalse; |
| 36 | +import static org.junit.Assert.assertNull; |
| 37 | +import static org.junit.Assert.assertSame; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +import org.junit.After; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Test; |
| 46 | +import org.scijava.Context; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests {@link ParseService}. |
| 50 | + * |
| 51 | + * @author Curtis Rueden |
| 52 | + */ |
| 53 | +public class ParseServiceTest { |
| 54 | + |
| 55 | + private ParseService parser; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + final Context context = new Context(ParseService.class); |
| 60 | + parser = context.service(ParseService.class); |
| 61 | + } |
| 62 | + |
| 63 | + @After |
| 64 | + public void tearDown() { |
| 65 | + parser.getContext().dispose(); |
| 66 | + } |
| 67 | + |
| 68 | + /** Tests {@link ParseService#parse(String)}. */ |
| 69 | + @Test |
| 70 | + public void testEmpty() { |
| 71 | + final Items items = parser.parse(""); |
| 72 | + assertTrue(items.isEmpty()); |
| 73 | + assertTrue(items.isList()); |
| 74 | + assertTrue(items.isMap()); |
| 75 | + assertMapCorrect(items); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testList() { |
| 80 | + final Items items = parser.parse("1,2,3,4,5"); |
| 81 | + assertEquals(5, items.size()); |
| 82 | + assertTrue(items.isList()); |
| 83 | + assertFalse(items.isMap()); |
| 84 | + assertSame(1, items.get(0).value()); |
| 85 | + assertSame(2, items.get(1).value()); |
| 86 | + assertSame(3, items.get(2).value()); |
| 87 | + assertSame(4, items.get(3).value()); |
| 88 | + assertSame(5, items.get(4).value()); |
| 89 | + assertNull(items.get(0).name()); |
| 90 | + assertNull(items.get(1).name()); |
| 91 | + assertNull(items.get(2).name()); |
| 92 | + assertNull(items.get(3).name()); |
| 93 | + assertNull(items.get(4).name()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testMap() { |
| 98 | + final Items items = parser.parse( |
| 99 | + "foo='bar', animal='Quick brown fox', colors={'red', 'green', 'blue'}"); |
| 100 | + assertEquals(3, items.size()); |
| 101 | + assertFalse(items.isList()); |
| 102 | + assertTrue(items.isMap()); |
| 103 | + assertEquals("foo", items.get(0).name()); |
| 104 | + assertEquals("bar", items.get(0).value()); |
| 105 | + assertEquals("animal", items.get(1).name()); |
| 106 | + assertEquals("Quick brown fox", items.get(1).value()); |
| 107 | + assertEquals("colors", items.get(2).name()); |
| 108 | + final Object colors = items.get(2).value(); |
| 109 | + assertTrue(colors instanceof List); |
| 110 | + final List<?> colorsList = (List<?>) colors; |
| 111 | + assertEquals(3, colorsList.size()); |
| 112 | + assertEquals("red", colorsList.get(0)); |
| 113 | + assertEquals("green", colorsList.get(1)); |
| 114 | + assertEquals("blue", colorsList.get(2)); |
| 115 | + |
| 116 | + assertMapCorrect(items); |
| 117 | + } |
| 118 | + |
| 119 | + // -- Helper methods -- |
| 120 | + |
| 121 | + private void assertMapCorrect(final Items items) { |
| 122 | + final Map<String, Object> map = items.asMap(); |
| 123 | + assertEquals(items.size(), map.size()); |
| 124 | + |
| 125 | + // test that map contents match |
| 126 | + for (final Item item : items) { |
| 127 | + assertSame(item.value(), map.get(item.name())); |
| 128 | + } |
| 129 | + |
| 130 | + // test that map iteration order is the same |
| 131 | + int index = 0; |
| 132 | + for (final Object value : map.values()) { |
| 133 | + assertSame("" + index + ":", items.get(index++).value(), value); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments