|
16 | 16 |
|
17 | 17 | package com.google.cloud.tools.jib.frontend; |
18 | 18 |
|
| 19 | +import com.google.cloud.tools.jib.builder.BuildLogger; |
| 20 | +import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; |
19 | 21 | import com.google.common.io.Resources; |
20 | 22 | import java.io.IOException; |
21 | 23 | import java.net.URISyntaxException; |
22 | 24 | import java.nio.file.Path; |
23 | 25 | import java.nio.file.Paths; |
| 26 | +import java.util.Collections; |
24 | 27 | import java.util.List; |
25 | 28 | import org.junit.Assert; |
| 29 | +import org.junit.Before; |
26 | 30 | import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.ArgumentMatchers; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.mockito.junit.MockitoJUnitRunner; |
27 | 36 |
|
28 | 37 | /** Test for MainClassFinder. */ |
| 38 | +@RunWith(MockitoJUnitRunner.class) |
29 | 39 | public class MainClassFinderTest { |
30 | 40 |
|
| 41 | + @Mock private BuildLogger mockBuildLogger; |
| 42 | + @Mock private SourceFilesConfiguration mockSourceFilesConfiguration; |
| 43 | + @Mock private ProjectProperties mockProjectProperties; |
| 44 | + @Mock private HelpfulSuggestions mockHelpfulSuggestions; |
| 45 | + |
| 46 | + private final List<Path> fakeClassesPath = Collections.singletonList(Paths.get("a/b/c")); |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setup() { |
| 50 | + Mockito.when(mockProjectProperties.getLogger()).thenReturn(mockBuildLogger); |
| 51 | + Mockito.when(mockProjectProperties.getPluginName()).thenReturn("plugin"); |
| 52 | + Mockito.when(mockProjectProperties.getSourceFilesConfiguration()) |
| 53 | + .thenReturn(mockSourceFilesConfiguration); |
| 54 | + Mockito.when(mockProjectProperties.getMainClassHelpfulSuggestions(ArgumentMatchers.any())) |
| 55 | + .thenReturn(mockHelpfulSuggestions); |
| 56 | + Mockito.when(mockProjectProperties.getJarPluginName()).thenReturn("jar-plugin"); |
| 57 | + } |
| 58 | + |
31 | 59 | @Test |
32 | 60 | public void testFindMainClass_simple() throws URISyntaxException, IOException { |
33 | 61 | Path rootDirectory = Paths.get(Resources.getResource("class-finder-tests/simple").toURI()); |
@@ -60,4 +88,77 @@ public void testFindMainClass_multiple() throws URISyntaxException, IOException |
60 | 88 | Assert.assertTrue(mainClasses.contains("multi.layered.HelloMoon")); |
61 | 89 | Assert.assertTrue(mainClasses.contains("HelloWorld")); |
62 | 90 | } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testResolveMainClass() throws MainClassInferenceException { |
| 94 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("some.main.class"); |
| 95 | + Assert.assertEquals( |
| 96 | + "some.main.class", MainClassFinder.resolveMainClass(null, mockProjectProperties)); |
| 97 | + Assert.assertEquals( |
| 98 | + "configured", MainClassFinder.resolveMainClass("configured", mockProjectProperties)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testResolveMainClass_notValid() throws MainClassInferenceException { |
| 103 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); |
| 104 | + Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(fakeClassesPath); |
| 105 | + Assert.assertEquals( |
| 106 | + "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); |
| 107 | + Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testResolveMainClass_multipleInferredWithBackup() throws Throwable { |
| 112 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); |
| 113 | + Mockito.when(mockSourceFilesConfiguration.getClassesFiles()) |
| 114 | + .thenReturn( |
| 115 | + Collections.singletonList( |
| 116 | + Paths.get(Resources.getResource("class-finder-tests/multiple").toURI()))); |
| 117 | + |
| 118 | + Assert.assertEquals( |
| 119 | + "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); |
| 120 | + Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testResolveMainClass_multipleInferredWithoutBackup() throws URISyntaxException { |
| 125 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn(null); |
| 126 | + Mockito.when(mockSourceFilesConfiguration.getClassesFiles()) |
| 127 | + .thenReturn( |
| 128 | + Collections.singletonList( |
| 129 | + Paths.get(Resources.getResource("class-finder-tests/multiple").toURI()))); |
| 130 | + |
| 131 | + try { |
| 132 | + MainClassFinder.resolveMainClass(null, mockProjectProperties); |
| 133 | + Assert.fail(); |
| 134 | + } catch (MainClassInferenceException ex) { |
| 135 | + Mockito.verify(mockProjectProperties) |
| 136 | + .getMainClassHelpfulSuggestions( |
| 137 | + "Multiple valid main classes were found: HelloWorld, multi.layered.HelloMoon"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testResolveMainClass_noneInferredWithBackup() throws MainClassInferenceException { |
| 143 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn("${start-class}"); |
| 144 | + Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(fakeClassesPath); |
| 145 | + |
| 146 | + Assert.assertEquals( |
| 147 | + "${start-class}", MainClassFinder.resolveMainClass(null, mockProjectProperties)); |
| 148 | + Mockito.verify(mockBuildLogger).warn("'mainClass' is not a valid Java class : ${start-class}"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testResolveMainClass_noneInferredWithoutBackup() { |
| 153 | + Mockito.when(mockProjectProperties.getMainClassFromJar()).thenReturn(null); |
| 154 | + Mockito.when(mockSourceFilesConfiguration.getClassesFiles()).thenReturn(fakeClassesPath); |
| 155 | + |
| 156 | + try { |
| 157 | + MainClassFinder.resolveMainClass(null, mockProjectProperties); |
| 158 | + Assert.fail(); |
| 159 | + } catch (MainClassInferenceException ex) { |
| 160 | + Mockito.verify(mockProjectProperties) |
| 161 | + .getMainClassHelpfulSuggestions("Main class was not found"); |
| 162 | + } |
| 163 | + } |
63 | 164 | } |
0 commit comments