|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.jib.gradle; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import com.google.common.io.Resources; |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.URISyntaxException; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Set; |
| 30 | +import org.gradle.api.Project; |
| 31 | +import org.gradle.api.file.FileCollection; |
| 32 | +import org.gradle.api.internal.file.AbstractFileCollection; |
| 33 | +import org.gradle.api.plugins.Convention; |
| 34 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 35 | +import org.gradle.api.tasks.SourceSet; |
| 36 | +import org.gradle.api.tasks.SourceSetContainer; |
| 37 | +import org.gradle.api.tasks.SourceSetOutput; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.Mockito; |
| 43 | +import org.mockito.junit.MockitoJUnitRunner; |
| 44 | + |
| 45 | +/** Test for {@link GradleSourceFilesConfiguration}. */ |
| 46 | +@RunWith(MockitoJUnitRunner.class) |
| 47 | +public class GradleSourceFilesConfigurationTest { |
| 48 | + |
| 49 | + /** Implementation of {@link FileCollection} that just holds a set of {@link File}s. */ |
| 50 | + private static class TestFileCollection extends AbstractFileCollection { |
| 51 | + |
| 52 | + private final Set<File> files; |
| 53 | + |
| 54 | + private TestFileCollection(Set<File> files) { |
| 55 | + this.files = files; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDisplayName() { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Set<File> getFiles() { |
| 65 | + return files; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private GradleSourceFilesConfiguration testGradleSourceFilesConfiguration; |
| 70 | + |
| 71 | + @Before |
| 72 | + public void setUp() throws URISyntaxException, IOException { |
| 73 | + Project mockProject = Mockito.mock(Project.class); |
| 74 | + Convention mockConvention = Mockito.mock(Convention.class); |
| 75 | + JavaPluginConvention mockJavaPluginConvention = Mockito.mock(JavaPluginConvention.class); |
| 76 | + SourceSetContainer mockSourceSetContainer = Mockito.mock(SourceSetContainer.class); |
| 77 | + SourceSet mockMainSourceSet = Mockito.mock(SourceSet.class); |
| 78 | + SourceSetOutput mockMainSourceSetOutput = Mockito.mock(SourceSetOutput.class); |
| 79 | + |
| 80 | + Set<File> classesFiles = |
| 81 | + ImmutableSet.of(Paths.get(Resources.getResource("application/classes").toURI()).toFile()); |
| 82 | + FileCollection classesFileCollection = new TestFileCollection(classesFiles); |
| 83 | + File resourcesOutputDir = |
| 84 | + Paths.get(Resources.getResource("application/resources").toURI()).toFile(); |
| 85 | + |
| 86 | + Set<File> allFiles = new HashSet<>(classesFiles); |
| 87 | + allFiles.add(resourcesOutputDir); |
| 88 | + allFiles.add( |
| 89 | + Paths.get(Resources.getResource("application/dependencies/libraryB.jar").toURI()).toFile()); |
| 90 | + allFiles.add( |
| 91 | + Paths.get(Resources.getResource("application/dependencies/libraryA.jar").toURI()).toFile()); |
| 92 | + allFiles.add( |
| 93 | + Paths.get(Resources.getResource("application/dependencies/dependency-1.0.0.jar").toURI()) |
| 94 | + .toFile()); |
| 95 | + FileCollection runtimeFileCollection = new TestFileCollection(allFiles); |
| 96 | + |
| 97 | + Mockito.when(mockProject.getConvention()).thenReturn(mockConvention); |
| 98 | + Mockito.when(mockConvention.getPlugin(JavaPluginConvention.class)) |
| 99 | + .thenReturn(mockJavaPluginConvention); |
| 100 | + Mockito.when(mockJavaPluginConvention.getSourceSets()).thenReturn(mockSourceSetContainer); |
| 101 | + Mockito.when(mockSourceSetContainer.getByName("main")).thenReturn(mockMainSourceSet); |
| 102 | + Mockito.when(mockMainSourceSet.getOutput()).thenReturn(mockMainSourceSetOutput); |
| 103 | + Mockito.when(mockMainSourceSetOutput.getClassesDirs()).thenReturn(classesFileCollection); |
| 104 | + Mockito.when(mockMainSourceSetOutput.getResourcesDir()).thenReturn(resourcesOutputDir); |
| 105 | + Mockito.when(mockMainSourceSet.getRuntimeClasspath()).thenReturn(runtimeFileCollection); |
| 106 | + |
| 107 | + testGradleSourceFilesConfiguration = GradleSourceFilesConfiguration.getForProject(mockProject); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void test_correctFiles() throws URISyntaxException { |
| 112 | + List<Path> expectedDependenciesFiles = |
| 113 | + Arrays.asList( |
| 114 | + Paths.get( |
| 115 | + Resources.getResource("application/dependencies/dependency-1.0.0.jar").toURI()), |
| 116 | + Paths.get(Resources.getResource("application/dependencies/libraryA.jar").toURI()), |
| 117 | + Paths.get(Resources.getResource("application/dependencies/libraryB.jar").toURI())); |
| 118 | + List<Path> expectedResourcesFiles = |
| 119 | + Arrays.asList( |
| 120 | + Paths.get(Resources.getResource("application/resources").toURI()).resolve("resourceA"), |
| 121 | + Paths.get(Resources.getResource("application/resources").toURI()).resolve("resourceB"), |
| 122 | + Paths.get(Resources.getResource("application/resources").toURI()).resolve("world")); |
| 123 | + List<Path> expectedClassesFiles = |
| 124 | + Arrays.asList( |
| 125 | + Paths.get(Resources.getResource("application/classes").toURI()) |
| 126 | + .resolve("HelloWorld.class"), |
| 127 | + Paths.get(Resources.getResource("application/classes").toURI()).resolve("some.class")); |
| 128 | + |
| 129 | + Assert.assertEquals( |
| 130 | + expectedDependenciesFiles, testGradleSourceFilesConfiguration.getDependenciesFiles()); |
| 131 | + Assert.assertEquals( |
| 132 | + expectedResourcesFiles, testGradleSourceFilesConfiguration.getResourcesFiles()); |
| 133 | + Assert.assertEquals(expectedClassesFiles, testGradleSourceFilesConfiguration.getClassesFiles()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void test_correctPathsOnImage() { |
| 138 | + Assert.assertEquals( |
| 139 | + "/app/libs/", testGradleSourceFilesConfiguration.getDependenciesPathOnImage()); |
| 140 | + Assert.assertEquals( |
| 141 | + "/app/resources/", testGradleSourceFilesConfiguration.getResourcesPathOnImage()); |
| 142 | + Assert.assertEquals( |
| 143 | + "/app/classes/", testGradleSourceFilesConfiguration.getClassesPathOnImage()); |
| 144 | + } |
| 145 | +} |
0 commit comments