|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.bookkeeper.util; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.UUID; |
| 23 | +import org.apache.commons.io.FileUtils; |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | + |
| 30 | +public class TestHardLink { |
| 31 | + |
| 32 | + private File tempDir; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() throws IOException { |
| 36 | + // Create at least one file so that target disk will never be empty |
| 37 | + tempDir = IOUtils.createTempDir("TestHardLink", "test-hardlink"); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void tearDown() throws IOException { |
| 42 | + FileUtils.deleteDirectory(tempDir); |
| 43 | + } |
| 44 | + |
| 45 | + private void verifyHardLink(File origin, File linkedOrigin) throws IOException { |
| 46 | + Assert.assertTrue(origin.exists()); |
| 47 | + Assert.assertFalse(linkedOrigin.exists()); |
| 48 | + |
| 49 | + HardLink.createHardLink(origin, linkedOrigin); |
| 50 | + |
| 51 | + Assert.assertTrue(origin.exists()); |
| 52 | + Assert.assertTrue(linkedOrigin.exists()); |
| 53 | + |
| 54 | + // when delete origin file it should be success and not exist. |
| 55 | + origin.delete(); |
| 56 | + Assert.assertFalse(origin.exists()); |
| 57 | + Assert.assertTrue(linkedOrigin.exists()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testHardLink() throws IOException { |
| 62 | + String uuidSuffix = UUID.randomUUID().toString(); |
| 63 | + |
| 64 | + // prepare file |
| 65 | + File origin = new File(tempDir, "originFile." + uuidSuffix); |
| 66 | + File linkedOrigin = new File(tempDir, "linkedOrigin." + uuidSuffix); |
| 67 | + origin.createNewFile(); |
| 68 | + |
| 69 | + // disable jdk api link first |
| 70 | + HardLink.enableJdkLinkApi(false); |
| 71 | + verifyHardLink(origin, linkedOrigin); |
| 72 | + |
| 73 | + // prepare file |
| 74 | + File jdkorigin = new File(tempDir, "jdkoriginFile." + uuidSuffix); |
| 75 | + File jdklinkedOrigin = new File(tempDir, "jdklinkedOrigin." + uuidSuffix); |
| 76 | + jdkorigin.createNewFile(); |
| 77 | + |
| 78 | + // enable jdk api link |
| 79 | + HardLink.enableJdkLinkApi(true); |
| 80 | + verifyHardLink(jdkorigin, jdklinkedOrigin); |
| 81 | + } |
| 82 | +} |
0 commit comments