|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 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 | +package com.vaadin.flow.spring; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.springframework.boot.SpringApplication; |
| 23 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 24 | + |
| 25 | +public class SpringDevToolsPortHandlerTest { |
| 26 | + |
| 27 | + private SpringDevToolsPortHandler handler; |
| 28 | + private ConfigurableEnvironment environment; |
| 29 | + private SpringApplication application; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUp() { |
| 33 | + handler = new SpringDevToolsPortHandler(); |
| 34 | + environment = Mockito.mock(ConfigurableEnvironment.class); |
| 35 | + application = Mockito.mock(SpringApplication.class); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void liveReloadEnabled_portIsAssigned() { |
| 40 | + // Arrange: livereload enabled (default), no port set |
| 41 | + Mockito.when(environment.getProperty( |
| 42 | + "spring.devtools.livereload.enabled", Boolean.class, true)) |
| 43 | + .thenReturn(true); |
| 44 | + Mockito.when(environment.getProperty("spring.devtools.livereload.port")) |
| 45 | + .thenReturn(null); |
| 46 | + |
| 47 | + // Act |
| 48 | + handler.postProcessEnvironment(environment, application); |
| 49 | + |
| 50 | + // Assert: port should be set via System.setProperty |
| 51 | + String portProperty = System |
| 52 | + .getProperty("spring.devtools.livereload.port"); |
| 53 | + Assert.assertNotNull("Port should be set when livereload is enabled", |
| 54 | + portProperty); |
| 55 | + |
| 56 | + // Verify it's a valid port number |
| 57 | + int port = Integer.parseInt(portProperty); |
| 58 | + Assert.assertTrue("Port should be positive", port > 0); |
| 59 | + Assert.assertTrue("Port should be in valid range", port <= 65535); |
| 60 | + |
| 61 | + // Clean up |
| 62 | + System.clearProperty("spring.devtools.livereload.port"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void liveReloadDisabled_portIsNotAssigned() { |
| 67 | + // Arrange: livereload explicitly disabled, no port set |
| 68 | + Mockito.when(environment.getProperty( |
| 69 | + "spring.devtools.livereload.enabled", Boolean.class, true)) |
| 70 | + .thenReturn(false); |
| 71 | + Mockito.when(environment.getProperty("spring.devtools.livereload.port")) |
| 72 | + .thenReturn(null); |
| 73 | + |
| 74 | + // Clear any previously set system property |
| 75 | + System.clearProperty("spring.devtools.livereload.port"); |
| 76 | + |
| 77 | + // Act |
| 78 | + handler.postProcessEnvironment(environment, application); |
| 79 | + |
| 80 | + // Assert: port should NOT be set |
| 81 | + String portProperty = System |
| 82 | + .getProperty("spring.devtools.livereload.port"); |
| 83 | + Assert.assertNull("Port should not be set when livereload is disabled", |
| 84 | + portProperty); |
| 85 | + } |
| 86 | +} |
0 commit comments