|
| 1 | +/* |
| 2 | + * Copyright 2000-2024 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 | + |
| 17 | +package com.vaadin.flow.server; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.TemporaryFolder; |
| 26 | + |
| 27 | +public class AbstractConfigurationTest { |
| 28 | + |
| 29 | + @Rule |
| 30 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 31 | + |
| 32 | + AbstractConfiguration configuration = new AbstractConfiguration() { |
| 33 | + @Override |
| 34 | + public boolean isProductionMode() { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getStringProperty(String name, String defaultValue) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean getBooleanProperty(String name, boolean defaultValue) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + @Test |
| 50 | + public void getProjectFolder_mavenProject_detected() throws IOException { |
| 51 | + assertProjectFolderDetected("pom.xml"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void getProjectFolder_gradleProject_detected() throws IOException { |
| 56 | + assertProjectFolderDetected("build.gradle"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getProjectFolder_gradleKotlinProject_detected() |
| 61 | + throws IOException { |
| 62 | + assertProjectFolderDetected("build.gradle.kts"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void getProjectFolder_unknownProject_throws() throws IOException { |
| 67 | + withTemporaryUserDir(() -> { |
| 68 | + IllegalStateException exception = Assert.assertThrows( |
| 69 | + IllegalStateException.class, |
| 70 | + configuration::getProjectFolder); |
| 71 | + Assert.assertTrue(exception.getMessage().contains( |
| 72 | + "Failed to determine project directory for dev mode")); |
| 73 | + Assert.assertTrue(exception.getMessage() |
| 74 | + .contains(temporaryFolder.getRoot().getAbsolutePath())); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private void assertProjectFolderDetected(String projectFile) |
| 79 | + throws IOException { |
| 80 | + temporaryFolder.newFile(projectFile); |
| 81 | + withTemporaryUserDir(() -> { |
| 82 | + File projectFolder = configuration.getProjectFolder(); |
| 83 | + Assert.assertEquals(temporaryFolder.getRoot(), projectFolder); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + private void withTemporaryUserDir(Runnable test) throws IOException { |
| 88 | + String userDir = System.getProperty("user.dir"); |
| 89 | + try { |
| 90 | + System.setProperty("user.dir", |
| 91 | + temporaryFolder.getRoot().getAbsolutePath()); |
| 92 | + test.run(); |
| 93 | + } finally { |
| 94 | + if (userDir != null) { |
| 95 | + System.setProperty("user.dir", userDir); |
| 96 | + } else { |
| 97 | + System.clearProperty("user.dir"); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments