|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 the original author or authors. |
| 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 | + |
| 17 | +package org.springframework.boot.gradle; |
| 18 | + |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileReader; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.regex.Pattern; |
| 26 | + |
| 27 | +import org.gradle.tooling.ProjectConnection; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | +import static org.junit.Assert.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration tests for creating a fully executable jar with Gradle. |
| 36 | + * |
| 37 | + * @author Andy Wilkinson |
| 38 | + */ |
| 39 | +public class FullyExecutableJarTests { |
| 40 | + |
| 41 | + private static final String BOOT_VERSION = Versions.getBootVersion(); |
| 42 | + |
| 43 | + private static ProjectConnection project; |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void createProject() throws IOException { |
| 47 | + project = new ProjectCreator().createProject("executable-jar"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void jarIsNotExecutableByDefault() throws IOException { |
| 52 | + project.newBuild().forTasks("clean", "build") |
| 53 | + .withArguments("-PbootVersion=" + BOOT_VERSION).run(); |
| 54 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 55 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 56 | + assertThat(isFullyExecutable(executableJar), is(false)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void madeExecutableViaExtension() throws IOException { |
| 61 | + project.newBuild().forTasks("clean", "build").withArguments( |
| 62 | + "-PbootVersion=" + BOOT_VERSION, "-PextensionExecutable=true").run(); |
| 63 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 64 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 65 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void madeExecutableViaTask() throws IOException { |
| 70 | + project.newBuild().forTasks("clean", "build") |
| 71 | + .withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskExecutable=true") |
| 72 | + .run(); |
| 73 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 74 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 75 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void taskTakesPrecedenceForMakingJarExecutable() throws IOException { |
| 80 | + project.newBuild().forTasks("clean", "build") |
| 81 | + .withArguments("-PbootVersion=" + BOOT_VERSION, |
| 82 | + "-PextensionExecutable=false", "-PtaskExecutable=true") |
| 83 | + .run(); |
| 84 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 85 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 86 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void scriptPropertiesFromTask() throws IOException { |
| 91 | + project.newBuild().forTasks("clean", "build") |
| 92 | + .withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskProperties=true") |
| 93 | + .run(); |
| 94 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 95 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 96 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 97 | + assertThat(containsLine("# Provides:.*__task__", executableJar), is(true)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void scriptPropertiesFromExtension() throws IOException { |
| 102 | + project.newBuild().forTasks("clean", "build").withArguments( |
| 103 | + "-PbootVersion=" + BOOT_VERSION, "-PextensionProperties=true").run(); |
| 104 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 105 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 106 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 107 | + assertThat(containsLine("# Provides:.*__extension__", executableJar), is(true)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void taskTakesPrecedenceForScriptProperties() throws IOException { |
| 112 | + project.newBuild().forTasks("clean", "build") |
| 113 | + .withArguments("-PbootVersion=" + BOOT_VERSION, |
| 114 | + "-PextensionProperties=true", "-PtaskProperties=true") |
| 115 | + .run(); |
| 116 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 117 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 118 | + assertThat(isFullyExecutable(executableJar), is(true)); |
| 119 | + assertThat(containsLine("# Provides:.*__task__", executableJar), is(true)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void customScriptFromTask() throws IOException { |
| 124 | + project.newBuild().forTasks("clean", "build") |
| 125 | + .withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskScript=true") |
| 126 | + .run(); |
| 127 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 128 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 129 | + assertThat(containsLine("Custom task script", executableJar), is(true)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void customScriptFromExtension() throws IOException { |
| 134 | + project.newBuild().forTasks("clean", "build") |
| 135 | + .withArguments("-PbootVersion=" + BOOT_VERSION, "-PextensionScript=true") |
| 136 | + .run(); |
| 137 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 138 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 139 | + assertThat(containsLine("Custom extension script", executableJar), is(true)); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void taskTakesPrecedenceForCustomScript() throws IOException { |
| 144 | + project.newBuild().forTasks("clean", "build") |
| 145 | + .withArguments("-PbootVersion=" + BOOT_VERSION, "-PextensionScript=true", |
| 146 | + "-PtaskScript=true") |
| 147 | + .run(); |
| 148 | + File buildLibs = new File("target/executable-jar/build/libs"); |
| 149 | + File executableJar = new File(buildLibs, "executable-jar.jar"); |
| 150 | + assertThat(containsLine("Custom task script", executableJar), is(true)); |
| 151 | + } |
| 152 | + |
| 153 | + private boolean isFullyExecutable(File file) throws IOException { |
| 154 | + return containsLine("#!/bin/bash", file); |
| 155 | + } |
| 156 | + |
| 157 | + private boolean containsLine(String toMatch, File file) throws IOException { |
| 158 | + Pattern pattern = Pattern.compile(toMatch); |
| 159 | + for (String line : readLines(file)) { |
| 160 | + if (pattern.matcher(line).matches()) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + } |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + private List<String> readLines(File file) throws IOException { |
| 168 | + BufferedReader reader = new BufferedReader(new FileReader(file)); |
| 169 | + String line; |
| 170 | + List<String> lines = new ArrayList<String>(); |
| 171 | + try { |
| 172 | + while ((line = reader.readLine()) != null && lines.size() < 50) { |
| 173 | + lines.add(line); |
| 174 | + } |
| 175 | + } |
| 176 | + finally { |
| 177 | + reader.close(); |
| 178 | + } |
| 179 | + return lines; |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments