View Javadoc
1   package org.opentrafficsim.base.parameters;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotNull;
5   import static org.junit.Assert.assertTrue;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import org.opentrafficsim.base.modelproperties.PickList;
11  import org.opentrafficsim.base.modelproperties.PickListItem;
12  
13  /**
14   * Test the PickList class and closely related classes.
15   * <p>
16   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Aug 16, 2017 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public class PickTest
25  {
26  
27      /**
28       * Test the PickList class.
29       * @throws ParameterException if this happens uncaught; this test has failed
30       */
31      // TODO: The testPickList unit test giver errors!!
32      // @Test
33      public final void testPickList() throws ParameterException
34      {
35          PickListItem<String> item1 = new PickListItem<>("id1", "description1", "long description 1");
36          assertEquals("can retrieve id", "id1", item1.getId());
37          assertEquals("can retrieve display text", "description1", item1.getDisplayText());
38          assertEquals("can retrieve description", "long description 1", item1.getDescription());
39          assertTrue("toString returns something with PickListItem in it", item1.toString().indexOf("PickListItem") >= 0);
40          PickList<String> pl = new PickList<>("id", "description", item1);
41          assertEquals("can retrieve id", "id", pl.getId());
42          assertEquals("can retrieve description", "description", pl.getDescription());
43          assertTrue("toString returns something with PickList in it", pl.toString().indexOf("PickList") >= 0);
44          PickListItem<String> item2 = new PickListItem<>("id1", "description2"); // Same id!
45          assertEquals("using 2 arg constructor sets description to display text", "description2", item2.getDisplayText());
46          assertEquals("using 2 arg constructor sets description to display text", "description2", item2.getDescription());
47          try
48          {
49              pl.addItem(item2);
50          }
51          catch (ParameterException pe)
52          {
53              // Ignore expected exception
54          }
55          item2 = new PickListItem<>("id2", "description2", "long description 2");
56          pl.addItem(item2); // should not throw an exception
57          String constraintDescription = pl.getConstraint().toString();
58          assertNotNull("constraint toString may not be null", constraintDescription);
59          assertTrue("constraint toString contains id1", constraintDescription.indexOf("id1") >= 0);
60          assertTrue("constraint toString contains id2", constraintDescription.indexOf("id2") >= 0);
61  
62          pl = new PickList<>("id", "description", item1, item2);
63          List<PickListItem<String>> list = new ArrayList<>();
64          try
65          {
66              new PickList<>("idXX", "descriptionXX", list);
67          }
68          catch (ParameterException pe)
69          {
70              // Ignore expected exception
71          }
72          list.add(item1);
73          pl = new PickList<>("idXX", "descriptionXX", list);
74          assertEquals("can retrieve id", "idXX", pl.getId());
75          assertEquals("can retrieve description", "descriptionXX", pl.getDescription());
76          assertTrue("toString returns something with PickList in it", pl.toString().indexOf("PickList") >= 0);
77      }
78  }