View Javadoc
1   package org.opentrafficsim.core.network.factory.xml.units;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   
6   import java.awt.Color;
7   
8   import org.junit.Test;
9   import org.opentrafficsim.core.network.NetworkException;
10  
11  /**
12   * Test that color parser works as expected.
13   * <p>
14   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 17, 2017 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   */
22  public class ColorParserTest
23  {
24  
25      /**
26       * Test the color parser.
27       * @throws NetworkException if that happens uncaught; this test has failed
28       */
29      @Test
30      public final void testColors() throws NetworkException
31      {
32          assertEquals("should be Red", Color.RED, Colors.parseColor("#ff0000"));
33          assertEquals("should be Green", Color.GREEN, Colors.parseColor("#00ff00"));
34          assertEquals("should be Blue", Color.BLUE, Colors.parseColor("#0000FF")); // Try caps as well
35          assertEquals("should be Black", Color.BLACK, Colors.parseColor("#0"));
36          assertEquals("should be White", Color.WHITE, Colors.parseColor("#ffffff"));
37  
38          assertEquals("should be Red", Color.RED, Colors.parseColor("RGB ( 255 , 0 , 0 ) ")); // lots of extra spaces
39          assertEquals("should be Green", Color.GREEN, Colors.parseColor("RGB(0,255,0)"));
40          assertEquals("should be Blue", Color.BLUE, Colors.parseColor("RGB(0,0,255)"));
41          assertEquals("should be Black", Color.BLACK, Colors.parseColor("RGB(0,0,0)"));
42          assertEquals("should be White", Color.WHITE, Colors.parseColor("RGB(255,255,255)"));
43  
44          assertEquals("name should be recognized", Color.BLACK, Colors.parseColor("BLACK"));
45          assertEquals("name should be recognized", Color.BLUE, Colors.parseColor("BLUE"));
46          assertEquals("name should be recognized", Color.CYAN, Colors.parseColor("CYAN"));
47          assertEquals("name should be recognized", Color.DARK_GRAY, Colors.parseColor("DARK_GRAY"));
48          assertEquals("name should be recognized", Color.GRAY, Colors.parseColor("GRAY"));
49          assertEquals("name should be recognized", Color.GREEN, Colors.parseColor("GREEN"));
50          assertEquals("name should be recognized", Color.LIGHT_GRAY, Colors.parseColor("LIGHT_GRAY"));
51          assertEquals("name should be recognized", Color.MAGENTA, Colors.parseColor("MAGENTA"));
52          assertEquals("name should be recognized", Color.ORANGE, Colors.parseColor("ORANGE"));
53          assertEquals("name should be recognized", Color.PINK, Colors.parseColor("PINK"));
54          assertEquals("name should be recognized", Color.RED, Colors.parseColor("RED"));
55          assertEquals("name should be recognized", Color.WHITE, Colors.parseColor("WHITE"));
56          assertEquals("name should be recognized", Color.YELLOW, Colors.parseColor("YELLOW"));
57          try
58          {
59              Colors.parseColor("SOMEWHAT_YELLOWISH");
60              fail("unknown color should have thrown a NetworkException");
61          }
62          catch (NetworkException ne)
63          {
64              // Ignore expected exception
65          }
66          try
67          {
68              Colors.parseColor("#cdefgh");
69              fail("Bad hex digit should have thrown a NumberFormatException");
70          }
71          catch (NumberFormatException nfe)
72          {
73              // Ignore expected exception
74          }
75          try
76          {
77              Colors.parseColor("RGB(1,2)");
78              fail("Too few values should have thrown an IndexOutOfBoundsException");
79          }
80          catch (IndexOutOfBoundsException ioobe)
81          {
82              // Ignore expected Exception
83          }
84          try
85          {
86              Colors.parseColor("RGB(1,2,)");
87              fail("empty last field should have thrown an ArrayIndexOutOfBoundsException");
88          }
89          catch (ArrayIndexOutOfBoundsException aioobe)
90          {
91              // Ignore expected exception
92          }
93          try
94          {
95              Colors.parseColor("RGB(1,,3)");
96              fail("emty middle field should have thrown a NumberFormatException");
97          }
98          catch (NumberFormatException nfe)
99          {
100             // Ignore expected exception
101         }
102         // FIXME: parser gracefully accepts RGB strings without parentheses
103         try
104         {
105             Colors.parseColor("RGB(256, 0, 0)");
106             fail("R value > 255 should have thrown an IllegalArgumentException");
107         }
108         catch (IllegalArgumentException nfe)
109         {
110             // Ignore expected exception
111         }
112         try
113         {
114             Colors.parseColor("RGB(-1, 0, 0)");
115             fail("R value < 0 should have thrown an IllegalArgumentException");
116         }
117         catch (IllegalArgumentException nfe)
118         {
119             // Ignore expected exception
120         }
121     }
122 }