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) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.concurrency;
    20
    21  import java.io.File;
    22  import java.net.URL;
    23  import org.junit.After;
    24  import org.junit.AfterClass;
    25  import org.junit.Before;
    26  import org.junit.BeforeClass;
    27  import org.junit.Test;
    28  import static org.junit.Assert.*;
    29
    30  /**
    31   *
    32   * @author Jeremy Long (jeremy.long@owasp.org)
    33   */
    34  public class DirectorySpinLockTest {
    35
    36      public DirectorySpinLockTest() {
    37      }
    38
    39      @BeforeClass
    40      public static void setUpClass() {
    41      }
    42
    43      @AfterClass
    44      public static void tearDownClass() {
    45      }
    46
    47      @Before
    48      public void setUp() {
    49      }
    50
    51      @After
    52      public void tearDown() {
    53      }
    54
    55      /**
    56       * Test of obtainSharedLock method, of class DirectorySpinLock.
    57       * Specifically, this test uses the SpinLockTask to obtain an exclusive lock
    58       * that is held for 5 seconds. We then try to obtain a shared lock while
    59       * that task is running. It should take longer then 5 seconds to obtain the
    60       * shared lock.
    61       */
    62      @Test
    63      public void testObtainSharedLock_withContention() throws Exception {
    64          URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    65          File directory = new File(location.getFile());
    66          DirectorySpinLock instance = new DirectorySpinLock(directory);
    67          SpinLockTask task = new SpinLockTask(directory, 5000, false, 2);
    68          long start = System.currentTimeMillis();
    69          task.run();
    70          instance.obtainSharedLock();
    71          long end = System.currentTimeMillis();
    72          instance.close();
    73          if (task.getException() != null) {
    74              throw task.getException();
    75          }
    76          long timeElapsed = end - start;
    77          assertTrue("no lock contention occured?", timeElapsed >= 5000);
    78          //no exceptions means everything worked.
    79      }
    80
    81      /**
    82       * Test of obtainSharedLock method, of class DirectorySpinLock. This method
    83       * obtains two shared locks by using the SpinLockTask to obtain a lock in
    84       * another thread.
    85       */
    86      @Test
    87      public void testObtainSharedLock() throws Exception {
    88          URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    89          File directory = new File(location.getFile());
    90          DirectorySpinLock instance = new DirectorySpinLock(directory);
    91          SpinLockTask task = new SpinLockTask(directory, 1000, true, 2);
    92          task.run();
    93          instance.obtainSharedLock();
    94          instance.close();
    95          if (task.getException() != null) {
    96              throw task.getException();
    97          }
    98          //no exceptions means everything worked.
    99      }
    100
    101     /**
    102      * Test of obtainExclusiveLock method, of class DirectorySpinLock.
    103      */
    104     @Test
    105     public void testObtainExclusiveLock() throws Exception {
    106         URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    107         File directory = new File(location.getFile());
    108         DirectorySpinLock instance = new DirectorySpinLock(directory);
    109         SpinLockTask task = new SpinLockTask(directory, 1000, true, 1);
    110         instance.obtainExclusiveLock();
    111         task.run();
    112         instance.close();
    113         assertNotNull("No exception thrown due to exclusive lock failure?", task.getException());
    114         assertEquals("Incorrect exception when obtaining exclusive lock", "Unable to obtain lock", task.getException().getMessage());
    115     }
    116 }