|
| 1 | +/* |
| 2 | + * Copyright 2000-2023 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 | + */ |
1 | 16 | package com.vaadin.base.devserver;
|
2 | 17 |
|
| 18 | +import com.vaadin.flow.internal.hilla.EndpointRequestUtil; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Rule; |
3 | 22 | import org.junit.Test;
|
| 23 | +import org.junit.rules.TemporaryFolder; |
| 24 | +import org.mockito.MockedStatic; |
| 25 | +import org.mockito.Mockito; |
4 | 26 |
|
5 |
| -import java.util.List; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URL; |
| 29 | +import java.net.URLClassLoader; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.util.LinkedList; |
6 | 33 |
|
7 |
| -import static org.junit.Assert.*; |
| 34 | +import static org.junit.Assert.assertEquals; |
8 | 35 |
|
9 | 36 | public class ServerInfoTest {
|
| 37 | + private ClassLoader oldContextClassLoader; |
| 38 | + |
| 39 | + @Rule |
| 40 | + public TemporaryFolder temporary = new TemporaryFolder(); |
| 41 | + |
| 42 | + private MockedStatic<EndpointRequestUtil> endpointRequestUtilMockedStatic; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void rememberContextClassLoader() throws Exception { |
| 46 | + oldContextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 47 | + fakePlatform(false, false); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void restoreContextClassLoader() { |
| 52 | + Thread.currentThread().setContextClassLoader(oldContextClassLoader); |
| 53 | + if (endpointRequestUtilMockedStatic != null) { |
| 54 | + endpointRequestUtilMockedStatic.close(); |
| 55 | + endpointRequestUtilMockedStatic = null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void fakePlatform(boolean vaadin, boolean hilla) |
| 60 | + throws IOException { |
| 61 | + if (endpointRequestUtilMockedStatic != null) { |
| 62 | + endpointRequestUtilMockedStatic.close(); |
| 63 | + endpointRequestUtilMockedStatic = null; |
| 64 | + } |
| 65 | + |
| 66 | + final LinkedList<URL> classpath = new LinkedList<>(); |
| 67 | + if (vaadin) { |
| 68 | + final Path vaadinJar = temporary.newFolder().toPath(); |
| 69 | + final Path pomProperties = vaadinJar.resolve( |
| 70 | + "META-INF/maven/com.vaadin/vaadin-core/pom.properties"); |
| 71 | + Files.createDirectories(pomProperties.getParent()); |
| 72 | + Files.writeString(pomProperties, "version=24.1.0"); |
| 73 | + classpath.add(vaadinJar.toUri().toURL()); |
| 74 | + } |
| 75 | + final ClassLoader classLoader = new URLClassLoader( |
| 76 | + classpath.toArray(new URL[0]), null); |
| 77 | + if (hilla) { |
| 78 | + endpointRequestUtilMockedStatic = Mockito |
| 79 | + .mockStatic(EndpointRequestUtil.class); |
| 80 | + endpointRequestUtilMockedStatic |
| 81 | + .when(EndpointRequestUtil::isHillaAvailable) |
| 82 | + .thenReturn(true); |
| 83 | + } |
| 84 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 85 | + } |
10 | 86 |
|
11 | 87 | @Test
|
12 |
| - public void testGetProductName() { |
| 88 | + public void testGetProductNameWithNoProducts() { |
13 | 89 | ServerInfo serverInfo = new ServerInfo();
|
14 |
| - var productNames = List.of("Vaadin", "Hilla"); |
15 |
| - // This test is more to prevent regressions |
16 |
| - assertTrue("Product name should be there by default", |
17 |
| - productNames.contains(serverInfo.getProductName())); |
| 90 | + assertEquals("", serverInfo.getProductName()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testGetProductNameWithVaadinOnClasspath() throws Exception { |
| 95 | + fakePlatform(true, false); |
| 96 | + ServerInfo serverInfo = new ServerInfo(); |
| 97 | + assertEquals("Vaadin", serverInfo.getProductName()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testGetProductNameWithHillaOnClasspath() throws Exception { |
| 102 | + fakePlatform(false, true); |
| 103 | + ServerInfo serverInfo = new ServerInfo(); |
| 104 | + assertEquals("Hilla", serverInfo.getProductName()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testGetProductNameWithBothVaadinAndHillaOnClasspath() |
| 109 | + throws Exception { |
| 110 | + fakePlatform(true, true); |
| 111 | + ServerInfo serverInfo = new ServerInfo(); |
| 112 | + assertEquals("Vaadin,Hilla", serverInfo.getProductName()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void hillaVersionIsDashWhenNoHillaOnClasspath() { |
| 117 | + final ServerInfo serverInfo = new ServerInfo(); |
| 118 | + assertEquals("-", serverInfo.getHillaVersion()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void vaadinVersionIsDashWhenNoVaadinOnClasspath() { |
| 123 | + final ServerInfo serverInfo = new ServerInfo(); |
| 124 | + assertEquals("-", serverInfo.getVaadinVersion()); |
18 | 125 | }
|
19 | 126 | }
|
0 commit comments