Home / Class/ StartupInfoLoggerTests Class — spring-boot Architecture

StartupInfoLoggerTests Class — spring-boot Architecture

Architecture documentation for the StartupInfoLoggerTests class in StartupInfoLoggerTests.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java lines 40–164

class StartupInfoLoggerTests {

	private final Log log = mock(Log.class);

	private MockEnvironment environment;

	@BeforeEach
	void setUp() {
		this.environment = new MockEnvironment();
		this.environment.setProperty("spring.application.version", "1.2.3");
		this.environment.setProperty("spring.application.pid", "42");
	}

	@Test
	void startingFormat() {
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
		then(this.log).should()
			.info(assertArg(
					(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
							+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
							+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
	}

	@Test
	void startingFormatWhenVersionIsNotAvailable() {
		this.environment.setProperty("spring.application.version", "");
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
		then(this.log).should()
			.info(assertArg(
					(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
							+ " using Java " + System.getProperty("java.version") + " with PID 42 (started by "
							+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
	}

	@Test
	void startingFormatWhenPidIsNotAvailable() {
		this.environment.setProperty("spring.application.pid", "");
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
		then(this.log).should()
			.info(assertArg(
					(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
							+ " v1.2.3 using Java " + System.getProperty("java.version") + " (started by "
							+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
	}

	@Test
	void startingFormatInAotMode() {
		System.setProperty("spring.aot.enabled", "true");
		try {
			given(this.log.isInfoEnabled()).willReturn(true);
			new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
			then(this.log).should()
				.info(assertArg((message) -> assertThat(message.toString())
					.contains("Starting AOT-processed " + getClass().getSimpleName() + " v1.2.3 using Java "
							+ System.getProperty("java.version") + " with PID 42 (started by "
							+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));

		}
		finally {
			System.clearProperty("spring.aot.enabled");
		}
	}

	@Test
	void startedFormat() {
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(1345L, "Started"));
		then(this.log).should()
			.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
					+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
	}

	@Test
	void startedWithoutUptimeFormat() {
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Started"));
		then(this.log).should()
			.info(assertArg((message) -> assertThat(message.toString())
				.matches("Started " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
	}

	@Test
	void restoredFormat() {
		given(this.log.isInfoEnabled()).willReturn(true);
		new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Restored"));
		then(this.log).should()
			.info(assertArg((message) -> assertThat(message.toString())
				.matches("Restored " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
	}

	static class TestStartup extends Startup {

		private final long startTime = System.currentTimeMillis();

		private final @Nullable Long uptime;

		private final String action;

		TestStartup(@Nullable Long uptime, String action) {
			this.uptime = uptime;
			this.action = action;
			started();
		}

		@Override
		protected long startTime() {
			return this.startTime;
		}

		@Override
		protected @Nullable Long processUptime() {
			return this.uptime;
		}

		@Override
		protected String action() {
			return this.action;
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free