-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3288 from DamnClin/remove-mockito-inline
Remove mockito inline
- Loading branch information
Showing
29 changed files
with
755 additions
and
997 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
src/main/java/tech/jhipster/lite/ApplicationStartupTraces.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package tech.jhipster.lite; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.env.Environment; | ||
import tech.jhipster.lite.common.domain.Generated; | ||
import tech.jhipster.lite.error.domain.Assert; | ||
|
||
final class ApplicationStartupTraces { | ||
|
||
private static final String SEPARATOR = "-".repeat(58); | ||
private static final String BREAK = "\n"; | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ApplicationStartupTraces.class); | ||
|
||
private ApplicationStartupTraces() {} | ||
|
||
static String of(Environment environment) { | ||
Assert.notNull("environment", environment); | ||
|
||
return new ApplicationStartupTracesBuilder() | ||
.append(BREAK) | ||
.appendSeparator() | ||
.append(applicationRunningTrace(environment)) | ||
.append(localUrl(environment)) | ||
.append(externalUrl(environment)) | ||
.append(profilesTrace(environment)) | ||
.appendSeparator() | ||
.append(configServer(environment)) | ||
.build(); | ||
} | ||
|
||
private static String applicationRunningTrace(Environment environment) { | ||
String applicationName = environment.getProperty("spring.application.name"); | ||
|
||
if (StringUtils.isBlank(applicationName)) { | ||
return "Application is running!"; | ||
} | ||
|
||
return new StringBuilder().append("Application '").append(applicationName).append("' is running!").toString(); | ||
} | ||
|
||
private static String localUrl(Environment environment) { | ||
return url("Local", "localhost", environment); | ||
} | ||
|
||
private static String externalUrl(Environment environment) { | ||
return url("External", hostAddress(), environment); | ||
} | ||
|
||
private static String url(String type, String host, Environment environment) { | ||
if (notWebEnvironment(environment)) { | ||
return null; | ||
} | ||
|
||
return new StringBuilder() | ||
.append(type) | ||
.append(": \t") | ||
.append(protocol(environment)) | ||
.append("://") | ||
.append(host) | ||
.append(":") | ||
.append(port(environment)) | ||
.append(contextPath(environment)) | ||
.toString(); | ||
} | ||
|
||
private static boolean notWebEnvironment(Environment environment) { | ||
return StringUtils.isBlank(environment.getProperty("server.port")); | ||
} | ||
|
||
private static String protocol(Environment environment) { | ||
if (noKeyStore(environment)) { | ||
return "http"; | ||
} | ||
|
||
return "https"; | ||
} | ||
|
||
private static boolean noKeyStore(Environment environment) { | ||
return StringUtils.isBlank(environment.getProperty("server.ssl.key-store")); | ||
} | ||
|
||
private static String port(Environment environment) { | ||
return environment.getProperty("server.port"); | ||
} | ||
|
||
private static String profilesTrace(Environment environment) { | ||
String[] profiles = environment.getActiveProfiles(); | ||
|
||
if (ArrayUtils.isEmpty(profiles)) { | ||
return null; | ||
} | ||
|
||
return new StringBuilder().append("Profile(s): \t").append(Stream.of(profiles).collect(Collectors.joining(", "))).toString(); | ||
} | ||
|
||
@Generated(reason = "Hard to test implement detail error management") | ||
private static String hostAddress() { | ||
try { | ||
return InetAddress.getLocalHost().getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
log.warn("The host name could not be determined, using `localhost` as fallback"); | ||
} | ||
|
||
return "localhost"; | ||
} | ||
|
||
private static String contextPath(Environment environment) { | ||
String contextPath = environment.getProperty("server.servlet.context-path"); | ||
|
||
if (StringUtils.isBlank(contextPath)) { | ||
return "/"; | ||
} | ||
|
||
return contextPath; | ||
} | ||
|
||
private static String configServer(Environment environment) { | ||
String configServer = environment.getProperty("configserver.status"); | ||
|
||
if (StringUtils.isBlank(configServer)) { | ||
return null; | ||
} | ||
|
||
return new StringBuilder().append("Config Server: ").append(configServer).append(BREAK).append(SEPARATOR).append(BREAK).toString(); | ||
} | ||
|
||
private static class ApplicationStartupTracesBuilder { | ||
|
||
private static final String SPACER = " "; | ||
|
||
private final StringBuilder trace = new StringBuilder(); | ||
|
||
public ApplicationStartupTracesBuilder appendSeparator() { | ||
trace.append(SEPARATOR).append(BREAK); | ||
|
||
return this; | ||
} | ||
|
||
public ApplicationStartupTracesBuilder append(String line) { | ||
if (line == null) { | ||
return this; | ||
} | ||
|
||
trace.append(SPACER).append(line).append(BREAK); | ||
|
||
return this; | ||
} | ||
|
||
public String build() { | ||
return trace.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.