View Javadoc
1   /*
2    * Copyright 2014 OWASP.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.owasp.dependencycheck.utils;
17  
18  import java.io.File;
19  import java.net.URISyntaxException;
20  import org.junit.After;
21  import org.junit.Assume;
22  import org.junit.Before;
23  
24  /**
25   *
26   * @author Jeremy Long
27   */
28  public abstract class BaseTest {
29  
30      /**
31       * The configured settings.
32       */
33      private Settings settings;
34  
35      /**
36       * Initialize the {@link Settings}.
37       */
38      @Before
39      public void setUp() {
40          settings = new Settings();
41      }
42  
43      /**
44       * Clean the {@link Settings}.
45       */
46      @After
47      public void tearDown() {
48          settings.cleanup(true);
49      }
50  
51      /**
52       * Returns the settings for the test cases.
53       *
54       * @return
55       */
56      protected Settings getSettings() {
57          return settings;
58      }
59      
60      /**
61       * Returns the given resource as a File using the object's class loader. The
62       * org.junit.Assume API is used so that test cases are skipped if the
63       * resource is not available.
64       *
65       * @param o the object used to obtain a reference to the class loader
66       * @param resource the name of the resource to load
67       * @return the resource as an File
68       */
69      public static File getResourceAsFile(Object o, String resource) {
70          try {
71              File f = new File(o.getClass().getClassLoader().getResource(resource).toURI().getPath());
72              Assume.assumeTrue(String.format("%n%n[SEVERE] Unable to load resource for test case: %s%n%n", resource), f.exists());
73              return f;
74          } catch (URISyntaxException e) {
75              throw new UnsupportedOperationException(e);
76          }
77      }
78  }