View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck;
19  
20  import java.io.BufferedInputStream;
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.util.zip.ZipEntry;
26  import java.util.zip.ZipInputStream;
27  import org.junit.Before;
28  import org.owasp.dependencycheck.BaseTest;
29  import org.owasp.dependencycheck.utils.Settings;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * An abstract database test case that is used to ensure the H2 DB exists prior to performing tests that utilize the data
35   * contained within.
36   *
37   * @author Jeremy Long
38   */
39  public abstract class BaseDBTestCase extends BaseTest {
40  
41      protected final static int BUFFER_SIZE = 2048;
42  
43      private final static Logger LOGGER = LoggerFactory.getLogger(BaseDBTestCase.class);
44  
45      @Before
46      public void setUp() throws Exception {
47          ensureDBExists();
48      }
49  
50      public static void ensureDBExists() throws Exception {
51  
52          java.io.File dataPath = Settings.getDataDirectory();
53          String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME);
54          LOGGER.trace("DB file name {}", fileName);
55          java.io.File dataFile = new File(dataPath, fileName);
56          LOGGER.trace("Ensuring {} exists", dataFile.toString());
57          if (!dataPath.exists() || !dataFile.exists()) {
58              LOGGER.trace("Extracting database to {}", dataPath.toString());
59              dataPath.mkdirs();
60              FileInputStream fis = null;
61              ZipInputStream zin = null;
62              try {
63                  File path = new File(BaseDBTestCase.class.getClassLoader().getResource("data.zip").getPath());
64                  fis = new FileInputStream(path);
65                  zin = new ZipInputStream(new BufferedInputStream(fis));
66                  ZipEntry entry;
67                  while ((entry = zin.getNextEntry()) != null) {
68                      if (entry.isDirectory()) {
69                          final File d = new File(dataPath, entry.getName());
70                          d.mkdir();
71                          continue;
72                      }
73                      FileOutputStream fos = null;
74                      BufferedOutputStream dest = null;
75                      try {
76                          File o = new File(dataPath, entry.getName());
77                          o.createNewFile();
78                          fos = new FileOutputStream(o, false);
79                          dest = new BufferedOutputStream(fos, BUFFER_SIZE);
80                          byte data[] = new byte[BUFFER_SIZE];
81                          int count;
82                          while ((count = zin.read(data, 0, BUFFER_SIZE)) != -1) {
83                              dest.write(data, 0, count);
84                          }
85                      } catch (Throwable ex) {
86                          LOGGER.error("", ex);
87                      } finally {
88                          try {
89                              if (dest != null) {
90                                  dest.flush();
91                                  dest.close();
92                              }
93                          } catch (Throwable ex) {
94                              LOGGER.trace("", ex);
95                          }
96                          try {
97                              if (fos != null) {
98                                  fos.close();
99                              }
100                         } catch (Throwable ex) {
101                             LOGGER.trace("", ex);
102                         }
103                     }
104                 }
105             } finally {
106                 try {
107                     if (zin != null) {
108                         zin.close();
109                     }
110                 } catch (Throwable ex) {
111                     LOGGER.trace("", ex);
112                 }
113                 try {
114                     if (fis != null) {
115                         fis.close();
116                     }
117                 } catch (Throwable ex) {
118                     LOGGER.trace("", ex);
119                 }
120             }
121         }
122     }
123 }