View Javadoc
1   package org.djutils.traceverifier;
2   
3   import static org.junit.Assert.assertTrue;
4   import static org.junit.Assert.fail;
5   
6   import java.io.File;
7   import java.io.IOException;
8   
9   import org.junit.Rule;
10  import org.junit.Test;
11  import org.junit.rules.TemporaryFolder;
12  
13  /**
14   * Test the TraceVerifier class. <br>
15   * <br>
16   * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
18   * distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   */
23  public class TraceVerifierTest
24  {
25  
26      /** Temporary directory that should be deleted by Junit at end of test. */
27      @Rule
28      public TemporaryFolder testDir = new TemporaryFolder();
29  
30      /**
31       * Test the TraceVerifier class.
32       * @throws IOException if that happens uncaught, this test has failed.
33       */
34      @SuppressWarnings("resource")
35      @Test
36      public void testTraceVerifier() throws IOException
37      {
38          try
39          {
40              new TraceVerifier("/Non/Existent/Path/And/File/Name");
41              fail("non existent path should have thrown an exception");
42          }
43          catch (IOException ioe)
44          {
45              // Ignore expected exception
46          }
47  
48          // System.out.println("testdir is " + this.testDir.getRoot());
49          String traceFileName = this.testDir.getRoot().getCanonicalPath() + File.separator + "traceFile.txt";
50  
51          TraceVerifier tv = new TraceVerifier(traceFileName);
52          assertTrue("toString returns something descriptive", tv.toString().startsWith("TraceVerifier"));
53          // Trace some things
54          tv.sample("sample 1", "state 1");
55          tv.sample("sample 2", "state 2");
56          tv.sample("sample 3", "state 3");
57          tv.close();
58  
59          tv = new TraceVerifier(traceFileName); // Now it should be open for verify
60          try
61          {
62              tv.sample("sample 1", "not state 1");
63              fail("tv.sample(...) should have raised an exception");
64          }
65          catch (TraceVerifierException re)
66          {
67              // exception expected
68              assertTrue("Got wrong message: " + re.getMessage() + " of exception " + re.getClass(),
69                      re.getMessage().startsWith("Discrepancy found"));
70          }
71          try
72          {
73              tv.sample("not sample 2", "state 2");
74              fail("tv.sample(...) should have raised an exception");
75          }
76          catch (TraceVerifierException re)
77          {
78              // exception expected
79              assertTrue("Got wrong message: " + re.getMessage() + " of exception " + re.getClass(),
80                      re.getMessage().startsWith("Discrepancy found"));
81          }
82          tv.sample("sample 3", "state 3");
83          tv.close();
84      }
85  
86  }