|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + * |
| 4 | + * Copyright (C) 2003-2015 JNode.org |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License as published |
| 8 | + * by the Free Software Foundation; either version 2.1 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 | + * License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this library; If not, write to the Free Software Foundation, Inc., |
| 18 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + */ |
| 20 | + |
| 21 | +package org.jnode.fs.service.def; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.jnode.driver.Device; |
| 27 | +import org.jnode.fs.FileSystem; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static junit.framework.Assert.assertEquals; |
| 33 | +import static junit.framework.Assert.assertFalse; |
| 34 | +import static junit.framework.Assert.assertTrue; |
| 35 | +import static junit.framework.Assert.fail; |
| 36 | +import static org.mockito.Mockito.doThrow; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for FileSystemAPIImpl.unmount(). |
| 43 | + */ |
| 44 | +public class FileSystemAPIImplTest { |
| 45 | + |
| 46 | + private FileSystemAPIImpl api; |
| 47 | + private VirtualFS vfs; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() throws Exception { |
| 51 | + FileSystemManager fsm = new FileSystemManager(); |
| 52 | + Device device = mock(Device.class); |
| 53 | + when(device.getShortDescription()).thenReturn("mock-device"); |
| 54 | + vfs = new VirtualFS(device); |
| 55 | + api = new FileSystemAPIImpl(fsm, vfs); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void tearDown() throws Exception { |
| 60 | + api = null; |
| 61 | + vfs = null; |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testUnmountNonExistentMountPointThrowsIllegalArgument() { |
| 66 | + try { |
| 67 | + api.unmount("/nonexistent"); |
| 68 | + fail("Expected IllegalArgumentException"); |
| 69 | + } catch (IllegalArgumentException e) { |
| 70 | + assertTrue(e.getMessage().contains("Not a mount point")); |
| 71 | + } catch (IOException e) { |
| 72 | + fail("Expected IllegalArgumentException, got IOException"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testUnmountWithoutLeadingSlash() throws Exception { |
| 78 | + Device device = mock(Device.class); |
| 79 | + FileSystem<?> fs = mock(FileSystem.class); |
| 80 | + when(fs.getDevice()).thenReturn(device); |
| 81 | + when(fs.isReadOnly()).thenReturn(true); |
| 82 | + |
| 83 | + api.mount("/mnt", fs, null); |
| 84 | + assertTrue(api.getMountPoints().containsKey("/mnt")); |
| 85 | + |
| 86 | + api.unmount("mnt"); |
| 87 | + assertFalse(api.getMountPoints().containsKey("/mnt")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUnmountReadOnlyFsClosesAndUnregisters() throws Exception { |
| 92 | + Device device = mock(Device.class); |
| 93 | + FileSystem<?> fs = mock(FileSystem.class); |
| 94 | + when(fs.getDevice()).thenReturn(device); |
| 95 | + when(fs.isReadOnly()).thenReturn(true); |
| 96 | + |
| 97 | + api.mount("/mnt", fs, null); |
| 98 | + api.unmount("/mnt"); |
| 99 | + |
| 100 | + verify(fs).close(); |
| 101 | + assertFalse(api.getMountPoints().containsKey("/mnt")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testUnmountRollbackOnCloseFailure() throws Exception { |
| 106 | + Device device = mock(Device.class); |
| 107 | + FileSystem<?> fs = mock(FileSystem.class); |
| 108 | + when(fs.getDevice()).thenReturn(device); |
| 109 | + when(fs.isReadOnly()).thenReturn(false); |
| 110 | + |
| 111 | + api.mount("/mnt", fs, null); |
| 112 | + assertTrue(api.getMountPoints().containsKey("/mnt")); |
| 113 | + |
| 114 | + IOException closeException = new IOException("Close failed"); |
| 115 | + doThrow(closeException).when(fs).close(); |
| 116 | + |
| 117 | + try { |
| 118 | + api.unmount("/mnt"); |
| 119 | + fail("Expected IOException"); |
| 120 | + } catch (IOException e) { |
| 121 | + assertEquals("Close failed", e.getMessage()); |
| 122 | + } |
| 123 | + |
| 124 | + Map<String, FileSystem<?>> mountPoints = api.getMountPoints(); |
| 125 | + assertTrue("Mount point should be restored after rollback", |
| 126 | + mountPoints.containsKey("/mnt")); |
| 127 | + assertEquals(fs, mountPoints.get("/mnt")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testUnmountRemovesMountPoint() throws Exception { |
| 132 | + Device device = mock(Device.class); |
| 133 | + FileSystem<?> fs = mock(FileSystem.class); |
| 134 | + when(fs.getDevice()).thenReturn(device); |
| 135 | + when(fs.isReadOnly()).thenReturn(true); |
| 136 | + |
| 137 | + api.mount("/mnt", fs, null); |
| 138 | + assertTrue(api.getMountPoints().containsKey("/mnt")); |
| 139 | + |
| 140 | + api.unmount("/mnt"); |
| 141 | + assertFalse(api.getMountPoints().containsKey("/mnt")); |
| 142 | + assertEquals(0, api.getMountPoints().size()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testUnmountMultipleMountPoints() throws Exception { |
| 147 | + Device device1 = mock(Device.class); |
| 148 | + Device device2 = mock(Device.class); |
| 149 | + FileSystem<?> fs1 = mock(FileSystem.class); |
| 150 | + FileSystem<?> fs2 = mock(FileSystem.class); |
| 151 | + when(fs1.getDevice()).thenReturn(device1); |
| 152 | + when(fs2.getDevice()).thenReturn(device2); |
| 153 | + when(fs1.isReadOnly()).thenReturn(true); |
| 154 | + when(fs2.isReadOnly()).thenReturn(true); |
| 155 | + |
| 156 | + api.mount("/mnt1", fs1, null); |
| 157 | + api.mount("/mnt2", fs2, null); |
| 158 | + assertEquals(2, api.getMountPoints().size()); |
| 159 | + |
| 160 | + api.unmount("/mnt1"); |
| 161 | + assertEquals(1, api.getMountPoints().size()); |
| 162 | + assertTrue(api.getMountPoints().containsKey("/mnt2")); |
| 163 | + |
| 164 | + api.unmount("/mnt2"); |
| 165 | + assertEquals(0, api.getMountPoints().size()); |
| 166 | + } |
| 167 | +} |
0 commit comments