You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultFileLockManagerTimeoutTest.java 2.9KB

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