View Javadoc
1   package org.apache.archiva.common.filelock;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.test.context.ContextConfiguration;
28  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29  
30  import javax.inject.Inject;
31  import javax.inject.Named;
32  import java.io.File;
33  import java.io.IOException;
34  import java.nio.file.FileSystemException;
35  import java.nio.file.Files;
36  import java.nio.file.StandardCopyOption;
37  
38  /**
39   * @author Olivier Lamy
40   */
41  @RunWith(SpringJUnit4ClassRunner.class)
42  @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
43  public class DefaultFileLockManagerTimeoutTest
44  {
45  
46      final Logger logger = LoggerFactory.getLogger( getClass() );
47  
48      @Inject
49      @Named(value = "fileLockManager#default")
50      FileLockManager fileLockManager;
51  
52      @Before
53      public void initialize()
54      {
55          fileLockManager.setSkipLocking( false );
56  
57          fileLockManager.setTimeout( 5000 );
58  
59          fileLockManager.clearLockFiles();
60      }
61  
62      @Test(expected = FileLockTimeoutException.class)
63      public void testTimeout()
64          throws Throwable
65      {
66  
67              try {
68                  File file = new File(System.getProperty("buildDirectory"), "foo.txt");
69  
70                  Files.deleteIfExists(file.toPath());
71  
72                  File largeJar = new File(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
73  
74                  Lock lock = fileLockManager.writeFileLock(file);
75  
76                  try {
77                      Files.copy(largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
78                  } catch (IOException e) {
79                      logger.warn("Copy failed "+e.getMessage());
80                      // On windows a FileSystemException is thrown
81                      // We ignore this
82                  }
83  
84                  lock = fileLockManager.writeFileLock(file);
85              } catch (FileSystemException ex) {
86                  logger.error("Exception from filesystem "+ex.getMessage());
87                  ex.printStackTrace();
88                  throw ex;
89              }
90  
91      }
92  
93  }