|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.maven; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | + |
| 27 | +import org.codehaus.plexus.resource.ResourceManager; |
| 28 | +import org.junit.Test; |
| 29 | +import org.mockito.ArgumentCaptor; |
| 30 | + |
| 31 | +public class FileLocatorTest { |
| 32 | + |
| 33 | + private final ResourceManager resourceManager = mock(ResourceManager.class); |
| 34 | + private final FileLocator fileLocator = new FileLocator(resourceManager); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void locateEmptyString() { |
| 38 | + assertNull(fileLocator.locateFile("")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void locateNull() { |
| 43 | + assertNull(fileLocator.locateFile(null)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void locateValidFile() throws Exception { |
| 48 | + File file = new File("test-config.xml"); |
| 49 | + when(resourceManager.getResourceAsFile(any(), any())).thenReturn(file); |
| 50 | + |
| 51 | + File locatedFile = fileLocator.locateFile("/tmp/configs/my-config.xml"); |
| 52 | + |
| 53 | + assertEquals(file, locatedFile); |
| 54 | + |
| 55 | + ArgumentCaptor<String> argCaptor = ArgumentCaptor.forClass(String.class); |
| 56 | + verify(resourceManager).getResourceAsFile(eq("/tmp/configs/my-config.xml"), argCaptor.capture()); |
| 57 | + assertThat(argCaptor.getValue()).startsWith("my-config").endsWith(".xml"); |
| 58 | + } |
| 59 | +} |
0 commit comments