View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.data.nvdcve;
    20
    21  import org.owasp.dependencycheck.data.cpe.*;
    22  import java.io.BufferedInputStream;
    23  import java.io.BufferedOutputStream;
    24  import java.io.File;
    25  import java.io.FileInputStream;
    26  import java.io.FileOutputStream;
    27  import java.io.IOException;
    28  import java.util.logging.Level;
    29  import java.util.logging.Logger;
    30  import java.util.zip.ZipEntry;
    31  import java.util.zip.ZipInputStream;
    32  import junit.framework.TestCase;
    33  import org.owasp.dependencycheck.data.update.DataStoreMetaInfo;
    34  import org.owasp.dependencycheck.utils.Settings;
    35
    36  /**
    37   *
    38   * @author Jeremy Long (jeremy.long@owasp.org)
    39   */
    40  public abstract class BaseDBTestCase extends TestCase {
    41
    42      protected final static int BUFFER_SIZE = 2048;
    43
    44      @Override
    45      protected void setUp() throws Exception {
    46          super.setUp();
    47          ensureDBExists();
    48      }
    49
    50      public static void ensureDBExists() throws Exception {
    51
    52          java.io.File dataPath = Settings.getFile(Settings.KEYS.DATA_DIRECTORY);
    53          if (!dataPath.exists() || (dataPath.isDirectory() && dataPath.listFiles().length < 3)) {
    54              dataPath.mkdirs();
    55              FileInputStream fis = null;
    56              ZipInputStream zin = null;
    57              try {
    58                  File path = new File(BaseDBTestCase.class.getClassLoader().getResource("data.zip").getPath());
    59                  fis = new FileInputStream(path);
    60                  zin = new ZipInputStream(new BufferedInputStream(fis));
    61                  ZipEntry entry;
    62                  while ((entry = zin.getNextEntry()) != null) {
    63                      if (entry.isDirectory()) {
    64                          final File d = new File(dataPath, entry.getName());
    65                          d.mkdir();
    66                          continue;
    67                      }
    68                      FileOutputStream fos = null;
    69                      BufferedOutputStream dest = null;
    70                      try {
    71                          File o = new File(dataPath, entry.getName());
    72                          o.createNewFile();
    73                          fos = new FileOutputStream(o, false);
    74                          dest = new BufferedOutputStream(fos, BUFFER_SIZE);
    75                          byte data[] = new byte[BUFFER_SIZE];
    76                          int count;
    77                          while ((count = zin.read(data, 0, BUFFER_SIZE)) != -1) {
    78                              dest.write(data, 0, count);
    79                          }
    80                      } catch (Exception ex) {
    81                          Logger.getLogger(BaseDBTestCase.class.getName()).log(Level.SEVERE, null, ex);
    82                      } finally {
    83                          try {
    84                              if (dest != null) {
    85                                  dest.flush();
    86                                  dest.close();
    87                              }
    88                          } catch (Throwable ex) {
    89                              Logger.getLogger(BaseDBTestCase.class.getName()).log(Level.FINEST, null, ex);
    90                          }
    91                          try {
    92                              if (fos != null) {
    93                                  fos.close();
    94                              }
    95                          } catch (Throwable ex) {
    96                              Logger.getLogger(BaseDBTestCase.class.getName()).log(Level.FINEST, null, ex);
    97                          }
    98                      }
    99                  }
    100             } finally {
    101                 try {
    102                     if (zin != null) {
    103                         zin.close();
    104                     }
    105                 } catch (Throwable ex) {
    106                     Logger.getLogger(BaseDBTestCase.class.getName()).log(Level.FINEST, null, ex);
    107                 }
    108                 try {
    109                     if (fis != null) {
    110                         fis.close();
    111                     }
    112                 } catch (Throwable ex) {
    113                     Logger.getLogger(BaseDBTestCase.class.getName()).log(Level.FINEST, null, ex);
    114                 }
    115             }
    116         }
    117     }
    118 }