Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feature flag for Hilla multi-module engine (#14289)
Fixes #14266
- Loading branch information
Showing
5 changed files
with
181 additions
and
139 deletions.
There are no files selected for viewing
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
173 changes: 173 additions & 0 deletions
173
flow-server/src/test/java/com/vaadin/flow/server/frontend/NodeTasksHillaTest.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,173 @@ | ||
/* | ||
* Copyright 2000-2022 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.server.frontend; | ||
|
||
import com.vaadin.experimental.FeatureFlags; | ||
import com.vaadin.flow.di.Lookup; | ||
import com.vaadin.flow.server.ExecutionFailedException; | ||
import com.vaadin.flow.server.frontend.NodeTasks.Builder; | ||
import com.vaadin.flow.server.frontend.scanner.ClassFinder; | ||
import com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.*; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static com.vaadin.flow.server.Constants.TARGET; | ||
import static com.vaadin.flow.server.frontend.FrontendUtils.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class NodeTasksHillaTest { | ||
|
||
@Rule | ||
public MockitoRule rule = MockitoJUnit.rule(); | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
private static final String USER_DIR = "user.dir"; | ||
|
||
private static String globalUserDirValue; | ||
private static String globalFrontendDirValue; | ||
private static String globalGeneratedDirValue; | ||
|
||
private String userDir; | ||
|
||
private File propertiesDir; | ||
|
||
@Mock | ||
private Lookup lookup; | ||
|
||
@Mock | ||
private EndpointGeneratorTaskFactory endpointGeneratorTaskFactory; | ||
|
||
@Mock | ||
private TaskGenerateOpenAPI taskGenerateOpenAPI; | ||
|
||
@Mock | ||
private TaskGenerateEndpoint taskGenerateEndpoint; | ||
|
||
@Mock | ||
private TaskGenerateHilla taskGenerateHilla; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
userDir = temporaryFolder.getRoot().getAbsolutePath(); | ||
System.setProperty(USER_DIR, userDir); | ||
System.clearProperty(PARAM_FRONTEND_DIR); | ||
System.clearProperty(PARAM_GENERATED_DIR); | ||
|
||
propertiesDir = temporaryFolder.newFolder(); | ||
|
||
Mockito.doReturn( | ||
new DefaultClassFinder(this.getClass().getClassLoader())) | ||
.when(lookup).lookup(ClassFinder.class); | ||
Mockito.doReturn(taskGenerateOpenAPI).when(endpointGeneratorTaskFactory) | ||
.createTaskGenerateOpenAPI(any(), any(), any(), any()); | ||
Mockito.doReturn(taskGenerateEndpoint) | ||
.when(endpointGeneratorTaskFactory) | ||
.createTaskGenerateEndpoint(any(), any(), any(), any()); | ||
Mockito.doReturn(endpointGeneratorTaskFactory).when(lookup) | ||
.lookup(EndpointGeneratorTaskFactory.class); | ||
|
||
Mockito.doReturn(taskGenerateHilla).when(lookup) | ||
.lookup(TaskGenerateHilla.class); | ||
} | ||
|
||
@BeforeClass | ||
public static void setupBeforeClass() { | ||
globalUserDirValue = System.getProperty(USER_DIR); | ||
globalFrontendDirValue = System.getProperty(PARAM_FRONTEND_DIR); | ||
globalGeneratedDirValue = System.getProperty(PARAM_GENERATED_DIR); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownAfterClass() { | ||
setPropertyIfPresent(USER_DIR, globalUserDirValue); | ||
setPropertyIfPresent(PARAM_FRONTEND_DIR, globalFrontendDirValue); | ||
setPropertyIfPresent(PARAM_GENERATED_DIR, globalGeneratedDirValue); | ||
} | ||
|
||
@Test | ||
public void should_useHillaGeneartor_whenEnabled() | ||
throws ExecutionFailedException, IOException { | ||
runEndpointTasks(true); | ||
verifyHillaTask(true); | ||
verifyOldGenerator(false); | ||
} | ||
|
||
@Test | ||
public void should_useOldGenerator_whenHillaGeneratorNotEnabled() | ||
throws ExecutionFailedException, IOException { | ||
runEndpointTasks(false); | ||
verifyHillaTask(false); | ||
verifyOldGenerator(true); | ||
} | ||
|
||
private void runEndpointTasks(boolean withHillaTask) | ||
throws ExecutionFailedException, IOException { | ||
FileUtils.write( | ||
new File(propertiesDir, FeatureFlags.PROPERTIES_FILENAME), | ||
String.format("com.vaadin.experimental.hillaEngine=%s\n", | ||
withHillaTask), | ||
StandardCharsets.UTF_8); | ||
|
||
Builder builder = new Builder(lookup, new File(userDir), TARGET) | ||
.enablePackagesUpdate(false).enableImportsUpdate(true) | ||
.runNpmInstall(false).withEmbeddableWebComponents(false) | ||
.withFlowResourcesFolder( | ||
new File(userDir, TARGET + "flow-frontend")) | ||
.withFrontendGeneratedFolder(new File(userDir)) | ||
.withEndpointSourceFolder(new File(userDir)) | ||
.withEndpointGeneratedOpenAPIFile(new File(userDir)) | ||
.setJavaResourceFolder(propertiesDir); | ||
|
||
builder.build().execute(); | ||
} | ||
|
||
private static void setPropertyIfPresent(String key, String value) { | ||
if (value != null) { | ||
System.setProperty(key, value); | ||
} | ||
} | ||
|
||
private void verifyHillaTask(boolean expected) | ||
throws ExecutionFailedException { | ||
Mockito.verify(taskGenerateHilla, expected ? times(1) : never()) | ||
.execute(); | ||
} | ||
|
||
private void verifyOldGenerator(boolean expected) | ||
throws ExecutionFailedException { | ||
Mockito.verify(endpointGeneratorTaskFactory, | ||
expected ? times(1) : never()) | ||
.createTaskGenerateEndpoint(any(), any(), any(), any()); | ||
Mockito.verify(endpointGeneratorTaskFactory, | ||
expected ? times(1) : never()) | ||
.createTaskGenerateOpenAPI(any(), any(), any(), any()); | ||
Mockito.verify(taskGenerateOpenAPI, expected ? times(1) : never()) | ||
.execute(); | ||
Mockito.verify(taskGenerateEndpoint, expected ? times(1) : never()) | ||
.execute(); | ||
} | ||
} |
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