View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *  http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.any23.plugin;
    19
    20  import static org.junit.Assert.assertTrue;
    21
    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.Iterator;
    29  import java.util.zip.ZipEntry;
    30  import java.util.zip.ZipInputStream;
    31
    32  import org.apache.any23.extractor.ExtractorFactory;
    33  import org.junit.After;
    34  import org.junit.Before;
    35  import org.junit.Test;
    36
    37  /**
    38   * Test case for {@link Any23PluginManager}.
    39   *
    40   * @author Michele Mostarda (mostarda@fbk.eu)
    41   */
    42  public class Any23PluginManagerTest {
    43
    44      private Any23PluginManager manager;
    45
    46      @Before
    47      public void before() {
    48          manager = Any23PluginManager.getInstance();
    49      }
    50
    51      @After
    52      public void after() {
    53          manager = null;
    54      }
    55
    56      @Test
    57      public void testGetPlugins() throws IOException {
    58          Iterator<ExtractorFactory> extractorPlugins = manager.getExtractors();
    59          assertTrue(extractorPlugins.hasNext());
    60      }
    61
    62      // TODO: move in FileUtils
    63      private void decompressJar(File jarFile, File destination) throws IOException {
    64          final int BUFFER = 1024 * 1024;
    65          BufferedOutputStream dest = null;
    66          FileOutputStream fos = null;
    67          ZipInputStream zis = null;
    68          final byte data[] = new byte[BUFFER];
    69          try {
    70              FileInputStream fis = new FileInputStream(jarFile);
    71              zis = new ZipInputStream(new BufferedInputStream(fis));
    72              ZipEntry entry;
    73              while ((entry = zis.getNextEntry()) != null) {
    74                  int count;
    75                  final File destinationFile = new File(destination, entry.getName());
    76                  if (entry.getName().endsWith("/")) {
    77                      destinationFile.mkdirs();
    78                  } else {
    79                      fos = new FileOutputStream(destinationFile);
    80                      dest = new BufferedOutputStream(fos, BUFFER);
    81                      while ((count = zis.read(data, 0, BUFFER)) != -1) {
    82                          dest.write(data, 0, count);
    83                          dest.flush();
    84                      }
    85                      dest.close();
    86                      fos.close();
    87                  }
    88              }
    89          } finally {
    90              if (zis != null)
    91                  zis.close();
    92              if (dest != null)
    93                  dest.close();
    94              if (fos != null)
    95                  fos.close();
    96          }
    97      }
    98
    99  }