|
| 1 | +package org.flossware.filetransfer; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.lang.reflect.Method; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * Comprehensive tests for SmbFileTransferClient to achieve 100% coverage. |
| 14 | + * Note: Most methods require jcifs connection which needs a live SMB server or complex mocking. |
| 15 | + * These tests focus on builder validation, configuration, and URL resolution logic. |
| 16 | + */ |
| 17 | +class SmbFileTransferClientTest { |
| 18 | + |
| 19 | + private SmbFileTransferClient client; |
| 20 | + |
| 21 | + @AfterEach |
| 22 | + void tearDown() throws Exception { |
| 23 | + if (client != null) { |
| 24 | + try { |
| 25 | + client.close(); |
| 26 | + } catch (Exception e) { |
| 27 | + // Ignore close errors in tearDown |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("Should support builder chaining") |
| 34 | + void testBuilderChaining() { |
| 35 | + SmbFileTransferClient.Builder builder = SmbFileTransferClient.builder(); |
| 36 | + assertSame(builder, builder.server("smb://server.com/share/")); |
| 37 | + assertSame(builder, builder.domain("DOMAIN")); |
| 38 | + assertSame(builder, builder.username("user")); |
| 39 | + assertSame(builder, builder.password("pass")); |
| 40 | + assertSame(builder, builder.credentials("user", "pass")); |
| 41 | + assertSame(builder, builder.credentials("DOMAIN", "user", "pass")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("Should throw NullPointerException when server is null in builder") |
| 46 | + void testBuilderNullServer() { |
| 47 | + assertThrows(NullPointerException.class, |
| 48 | + () -> SmbFileTransferClient.builder() |
| 49 | + .username("user") |
| 50 | + .password("pass") |
| 51 | + .build()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("Should build client with domain authentication") |
| 56 | + void testBuildWithDomain() throws Exception { |
| 57 | + client = SmbFileTransferClient.builder() |
| 58 | + .server("smb://server.com/share/") |
| 59 | + .domain("DOMAIN") |
| 60 | + .username("user") |
| 61 | + .password("pass") |
| 62 | + .build(); |
| 63 | + |
| 64 | + assertNotNull(client); |
| 65 | + String description = client.getDescription(); |
| 66 | + assertTrue(description.contains("SMB[")); |
| 67 | + assertTrue(description.contains("DOMAIN\\")); |
| 68 | + assertTrue(description.contains("user@")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("Should build client with workgroup authentication") |
| 73 | + void testBuildWithWorkgroup() throws Exception { |
| 74 | + client = SmbFileTransferClient.builder() |
| 75 | + .server("smb://192.168.1.100/Public/") |
| 76 | + .username("guest") |
| 77 | + .password("") |
| 78 | + .build(); |
| 79 | + |
| 80 | + assertNotNull(client); |
| 81 | + String description = client.getDescription(); |
| 82 | + assertTrue(description.contains("SMB[")); |
| 83 | + assertTrue(description.contains("guest@")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("Should build client without authentication") |
| 88 | + void testBuildWithoutAuth() throws Exception { |
| 89 | + client = SmbFileTransferClient.builder() |
| 90 | + .server("smb://server.com/share/") |
| 91 | + .build(); |
| 92 | + |
| 93 | + assertNotNull(client); |
| 94 | + String description = client.getDescription(); |
| 95 | + assertTrue(description.contains("SMB[")); |
| 96 | + assertTrue(description.contains("smb://server.com/share/")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("Should add trailing slash to server URL if missing") |
| 101 | + void testServerTrailingSlash() throws Exception { |
| 102 | + client = SmbFileTransferClient.builder() |
| 103 | + .server("smb://server.com/share") |
| 104 | + .build(); |
| 105 | + |
| 106 | + String description = client.getDescription(); |
| 107 | + assertTrue(description.contains("smb://server.com/share/")); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("Should not add extra trailing slash if already present") |
| 112 | + void testServerWithTrailingSlash() throws Exception { |
| 113 | + client = SmbFileTransferClient.builder() |
| 114 | + .server("smb://server.com/share/") |
| 115 | + .build(); |
| 116 | + |
| 117 | + String description = client.getDescription(); |
| 118 | + assertFalse(description.contains("smb://server.com/share//")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @DisplayName("Should resolve URL without credentials") |
| 123 | + void testResolveUrlWithoutCredentials() throws Exception { |
| 124 | + client = SmbFileTransferClient.builder() |
| 125 | + .server("smb://server.com/share/") |
| 126 | + .build(); |
| 127 | + |
| 128 | + Method resolveUrl = SmbFileTransferClient.class.getDeclaredMethod("resolveUrl", String.class); |
| 129 | + resolveUrl.setAccessible(true); |
| 130 | + |
| 131 | + String result = (String) resolveUrl.invoke(client, "test.txt"); |
| 132 | + assertEquals("smb://server.com/share/test.txt", result); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @DisplayName("Should resolve URL with path") |
| 137 | + void testResolveUrlWithPath() throws Exception { |
| 138 | + client = SmbFileTransferClient.builder() |
| 139 | + .server("smb://server.com/share/") |
| 140 | + .build(); |
| 141 | + |
| 142 | + Method resolveUrl = SmbFileTransferClient.class.getDeclaredMethod("resolveUrl", String.class); |
| 143 | + resolveUrl.setAccessible(true); |
| 144 | + |
| 145 | + String result = (String) resolveUrl.invoke(client, "documents/report.pdf"); |
| 146 | + assertEquals("smb://server.com/share/documents/report.pdf", result); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @DisplayName("Should return description with domain and username") |
| 151 | + void testGetDescriptionWithDomainAndUsername() throws Exception { |
| 152 | + client = SmbFileTransferClient.builder() |
| 153 | + .server("smb://server.com/share/") |
| 154 | + .domain("DOMAIN") |
| 155 | + .username("user") |
| 156 | + .password("pass") |
| 157 | + .build(); |
| 158 | + |
| 159 | + String description = client.getDescription(); |
| 160 | + assertTrue(description.contains("SMB[")); |
| 161 | + assertTrue(description.contains("DOMAIN\\user@")); |
| 162 | + assertTrue(description.contains("smb://server.com/share/")); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @DisplayName("Should return description with username only") |
| 167 | + void testGetDescriptionWithUsernameOnly() throws Exception { |
| 168 | + client = SmbFileTransferClient.builder() |
| 169 | + .server("smb://server.com/share/") |
| 170 | + .username("user") |
| 171 | + .password("pass") |
| 172 | + .build(); |
| 173 | + |
| 174 | + String description = client.getDescription(); |
| 175 | + assertTrue(description.contains("SMB[")); |
| 176 | + assertTrue(description.contains("user@")); |
| 177 | + assertFalse(description.contains("\\")); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + @DisplayName("Should return description without credentials") |
| 182 | + void testGetDescriptionNoCredentials() throws Exception { |
| 183 | + client = SmbFileTransferClient.builder() |
| 184 | + .server("smb://server.com/share/") |
| 185 | + .build(); |
| 186 | + |
| 187 | + String description = client.getDescription(); |
| 188 | + assertTrue(description.contains("SMB[")); |
| 189 | + assertTrue(description.contains("smb://server.com/share/")); |
| 190 | + assertFalse(description.contains("@")); |
| 191 | + assertFalse(description.contains("\\")); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + @DisplayName("Should close without error") |
| 196 | + void testClose() throws Exception { |
| 197 | + client = SmbFileTransferClient.builder() |
| 198 | + .server("smb://server.com/share/") |
| 199 | + .build(); |
| 200 | + |
| 201 | + assertDoesNotThrow(() -> client.close()); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + @DisplayName("Should throw NullPointerException when constructor receives null server") |
| 206 | + void testConstructorNullServer() throws Exception { |
| 207 | + java.lang.reflect.Constructor<SmbFileTransferClient> constructor = |
| 208 | + SmbFileTransferClient.class.getDeclaredConstructor( |
| 209 | + String.class, String.class, String.class, String.class); |
| 210 | + constructor.setAccessible(true); |
| 211 | + |
| 212 | + java.lang.reflect.InvocationTargetException exception = assertThrows( |
| 213 | + java.lang.reflect.InvocationTargetException.class, |
| 214 | + () -> constructor.newInstance(null, null, null, null)); |
| 215 | + |
| 216 | + assertTrue(exception.getCause() instanceof NullPointerException); |
| 217 | + assertTrue(exception.getCause().getMessage().contains("server cannot be null")); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + @DisplayName("Should handle null domain in constructor") |
| 222 | + void testConstructorNullDomain() throws Exception { |
| 223 | + client = createTestClient("smb://server.com/share/", null, null, null); |
| 224 | + assertNotNull(client); |
| 225 | + String description = client.getDescription(); |
| 226 | + assertFalse(description.contains("\\")); |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + @DisplayName("Should handle null username and password in constructor") |
| 231 | + void testConstructorNullCredentials() throws Exception { |
| 232 | + client = createTestClient("smb://server.com/share/", null, null, null); |
| 233 | + assertNotNull(client); |
| 234 | + String description = client.getDescription(); |
| 235 | + assertFalse(description.contains("@")); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + @DisplayName("Should use credentials builder method with username and password") |
| 240 | + void testCredentialsBuilderTwoArgs() throws Exception { |
| 241 | + client = SmbFileTransferClient.builder() |
| 242 | + .server("smb://server.com/share/") |
| 243 | + .credentials("user", "pass") |
| 244 | + .build(); |
| 245 | + |
| 246 | + String description = client.getDescription(); |
| 247 | + assertTrue(description.contains("user@")); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + @DisplayName("Should use credentials builder method with domain, username, and password") |
| 252 | + void testCredentialsBuilderThreeArgs() throws Exception { |
| 253 | + client = SmbFileTransferClient.builder() |
| 254 | + .server("smb://server.com/share/") |
| 255 | + .credentials("DOMAIN", "user", "pass") |
| 256 | + .build(); |
| 257 | + |
| 258 | + String description = client.getDescription(); |
| 259 | + assertTrue(description.contains("DOMAIN\\user@")); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + @DisplayName("Should verify DEFAULT_BUFFER_SIZE constant") |
| 264 | + void testConstantBufferSize() throws Exception { |
| 265 | + java.lang.reflect.Field bufferSize = SmbFileTransferClient.class.getDeclaredField("DEFAULT_BUFFER_SIZE"); |
| 266 | + bufferSize.setAccessible(true); |
| 267 | + assertEquals(8192, bufferSize.get(null)); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + @DisplayName("Should handle server with IP address") |
| 272 | + void testServerWithIpAddress() throws Exception { |
| 273 | + client = SmbFileTransferClient.builder() |
| 274 | + .server("smb://192.168.1.100/Public/") |
| 275 | + .build(); |
| 276 | + |
| 277 | + assertNotNull(client); |
| 278 | + String description = client.getDescription(); |
| 279 | + assertTrue(description.contains("192.168.1.100")); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + @DisplayName("Should handle server with custom port") |
| 284 | + void testServerWithCustomPort() throws Exception { |
| 285 | + client = SmbFileTransferClient.builder() |
| 286 | + .server("smb://server.com:445/share/") |
| 287 | + .build(); |
| 288 | + |
| 289 | + assertNotNull(client); |
| 290 | + String description = client.getDescription(); |
| 291 | + assertTrue(description.contains(":445")); |
| 292 | + } |
| 293 | + |
| 294 | + @Test |
| 295 | + @DisplayName("Should handle empty password") |
| 296 | + void testEmptyPassword() throws Exception { |
| 297 | + client = SmbFileTransferClient.builder() |
| 298 | + .server("smb://server.com/share/") |
| 299 | + .username("guest") |
| 300 | + .password("") |
| 301 | + .build(); |
| 302 | + |
| 303 | + assertNotNull(client); |
| 304 | + String description = client.getDescription(); |
| 305 | + assertTrue(description.contains("guest@")); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + @DisplayName("Should handle username with domain specified separately") |
| 310 | + void testUsernameWithSeparateDomain() throws Exception { |
| 311 | + client = SmbFileTransferClient.builder() |
| 312 | + .server("smb://server.com/share/") |
| 313 | + .domain("WORKGROUP") |
| 314 | + .username("user") |
| 315 | + .password("pass") |
| 316 | + .build(); |
| 317 | + |
| 318 | + String description = client.getDescription(); |
| 319 | + assertTrue(description.contains("WORKGROUP\\")); |
| 320 | + assertTrue(description.contains("user@")); |
| 321 | + } |
| 322 | + |
| 323 | + private SmbFileTransferClient createTestClient(String server, String domain, |
| 324 | + String username, String password) throws Exception { |
| 325 | + java.lang.reflect.Constructor<SmbFileTransferClient> constructor = |
| 326 | + SmbFileTransferClient.class.getDeclaredConstructor( |
| 327 | + String.class, String.class, String.class, String.class); |
| 328 | + constructor.setAccessible(true); |
| 329 | + |
| 330 | + return constructor.newInstance(server, domain, username, password); |
| 331 | + } |
| 332 | +} |
0 commit comments