1   package net.objectlab.qalab.m2.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.FileNotFoundException;
6   
7   import java.util.StringTokenizer;
8   
9   import junit.framework.TestCase;
10  
11  /**
12   * Test of the <code>Utils</code>.
13   * @author Dave Sag
14   */
15  public class UtilsTest extends TestCase {
16  
17      /**
18       * tests that passing a null file to checkFile throws a
19       * FileExecutionException.
20       */
21      public void testCheckNullFileThrowsFileNotFoundException() {
22      
23          try {
24              Utils.checkFile(null, "");
25              fail("expected an exception given a null file");
26          } catch (IOException ioex) {
27              assertTrue("wrong exception thrown.",
28                      ioex instanceof FileNotFoundException);
29          }
30      
31      }
32  
33      /**
34       * tests that passing an unknown file to checkFile throws a
35       * FileExecutionException.
36       */
37      public void testCheckBogusFileThrowsFileNotFoundException() {
38      
39          try {
40              Utils.checkFile(new File("some/bogus/path"), "test");
41              fail("expected an exception given a bogus file");
42          } catch (IOException ioex) {
43              assertTrue("wrong exception thrown.",
44                      ioex instanceof FileNotFoundException);
45          }
46      
47      }
48  
49      /**
50       * tests that passing a known file to checkFile throws no Exception.
51       */
52      public void testCheckValidFileDoesNotThrowIOException() {
53      
54          try {
55              Utils.checkFile(new File("pom.xml"), "test");
56          } catch (IOException ioex) {
57              fail("expected no exception given a real file, but got "
58                      + ioex);
59          }
60      
61      }
62  
63      public void testExtractAsInputStreamFromNullThrowsAssertionError() {
64          try {
65              Utils.extractAsInputStream(null);
66          } catch (IOException ioex) {
67              fail("expected assertion error not " + ioex);
68          } catch (AssertionError aer) {
69              // hooray.
70          }
71      }
72  
73      public void testExtractAsInputStreamFromEmptyPathThrowsAssertionError() {
74          try {
75              Utils.extractAsInputStream("");
76          } catch (IOException ioex) {
77              fail("expected assertion error not " + ioex);
78          } catch (AssertionError aer) {
79              // hooray.
80          }
81      }
82  
83      public void testExtractAsInputStreamFromUnknownPathThrowsFileNotFoundException() {
84          try {
85              Utils.extractAsInputStream("some/bogus/path");
86          } catch (IOException ioex) {
87              assertTrue("wrong exception thrown.",
88                      ioex instanceof FileNotFoundException);
89          }
90      }
91  
92      public void testExtractAsInputStreamFromKnownPathThrowsNoException() {
93          String path = "UtilsTest"
94                  + File.separator + "test.txt";
95                  
96          try {
97              Utils.extractAsInputStream(path);
98          } catch (IOException ioex) {
99              fail("expected no exception given a real file, but got "
100                     + ioex);
101         }
102     }
103 
104     public void testFormatDateBasedOnReasonableOffset() {
105         String date = Utils.formatDateBasedOnOffset("48");
106         assertEquals("the length is wrong", 10, date.length());
107         StringTokenizer st = new StringTokenizer(date, "-");
108         assertEquals("the format looks wrong", 3, st.countTokens());
109         String yyyy = st.nextToken();
110         assertEquals("the year is not 4 digits.", 4, yyyy.length());
111         String mm = st.nextToken();
112         assertEquals("the month is not 2 digits.", 2, mm.length());
113         String dd = st.nextToken();
114         assertEquals("the day is not 2 digits.", 2, dd.length());
115     }
116 
117     public void testFormatDateBasedOnNullOffsetThrowsAssertionError() {
118         try {
119             String date = Utils.formatDateBasedOnOffset(null);
120             fail("expected an assertion error for null input.");
121         } catch (AssertionError aer) {
122             // yay.
123         }
124     }
125 
126     public void testFormatDateBasedOnNonNumericOffsetThrowsNumberFormatException() {
127         try {
128             String date = Utils.formatDateBasedOnOffset("a test");
129             fail("expected a number format exception for non numeric input.");
130         } catch (NumberFormatException nfex) {
131             // yay.
132         }
133     }
134 }