Skip to content

Commit

Permalink
fix exising tests and add a new test for endpoint usage detector
Browse files Browse the repository at this point in the history
  • Loading branch information
mshabarov committed Dec 19, 2024
1 parent e032017 commit 5e2e68c
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public boolean areEndpointsUsed(Options options) {
return Stream.concat(
classFinder.getAnnotatedClasses(Endpoint.class).stream(),
classFinder.getAnnotatedClasses(BrowserCallable.class).stream())
.anyMatch(annotatedClass -> !annotatedClass.isAnnotationPresent(InternalBrowserCallable.class));
.anyMatch(annotatedClass -> !annotatedClass
.isAnnotationPresent(InternalBrowserCallable.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.server.frontend.EndpointGeneratorTaskFactory;
import com.vaadin.flow.server.frontend.EndpointUsageDetector;
import com.vaadin.flow.server.frontend.NodeTasks;
import com.vaadin.flow.server.frontend.Options;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
Expand All @@ -31,6 +32,12 @@ public static class ConnectEndpointsForTesting {
@BeforeEach
public void setUp() throws IOException {
Lookup mockLookup = Mockito.mock(Lookup.class);
EndpointUsageDetector endpointUsageDetector = Mockito
.mock(EndpointUsageDetector.class);
Mockito.when(endpointUsageDetector
.areEndpointsUsed(Mockito.any(Options.class))).thenReturn(true);
Mockito.doReturn(endpointUsageDetector).when(mockLookup)
.lookup(EndpointUsageDetector.class);
Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl())
.when(mockLookup).lookup(EndpointGeneratorTaskFactory.class);
Mockito.doReturn(new DefaultClassFinder(Set.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testBrowserCallableAndEndpointAnnotationsMarkedAsInternal() {
if (!clazz.getName().contains(".test.") && !clazz
.isAnnotationPresent(InternalBrowserCallable.class)) {
Assertions.fail("Class " + clazz.getName()
+ " is annotated with @BrowserCallable or @Endpoint, but missing @InternalBrowserCallable.");
+ " is annotated with @BrowserCallable or @Endpoint, but missing @InternalBrowserCallable.");
}
}
}
Expand All @@ -47,7 +47,7 @@ public void testInternalBrowserCallableHasBrowserCallableOrEndpoint()
if (!clazz.isAnnotationPresent(BrowserCallable.class)
&& !clazz.isAnnotationPresent(Endpoint.class)) {
Assertions.fail("Class " + clazz.getName()
+ " is annotated with @InternalBrowserCallable and must be annotated with @BrowserCallable or @Endpoint.");
+ " is annotated with @InternalBrowserCallable and must be annotated with @BrowserCallable or @Endpoint.");
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions packages/java/tests/spring/no-endpoints/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.vaadin</groupId>
<artifactId>tests-spring</artifactId>
<version>24.6-SNAPSHOT</version>
</parent>
<artifactId>tests-spring-no-endpoints</artifactId>
<name>ITs for No Endpoints cases</name>
<packaging>jar</packaging>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<formatter.basedir>${project.parent.parent.parent.basedir}</formatter.basedir>
</properties>

<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>hilla</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>hilla-maven-plugin</artifactId>
<configuration>
<parser>
<packages>
<package>com.example.application</package>
</packages>
</parser>
</configuration>
<executions>
<execution>
<goals>
<!-- The test here assumes the build creates frontend files -->
<goal>build-frontend</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* The entry point of the Spring Boot application.
*/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.application;

import com.vaadin.hilla.Endpoint;
import com.vaadin.hilla.InternalBrowserCallable;

@Endpoint
@InternalBrowserCallable
public class InternalBrowserCallableExample {
public String exampleMethod() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.application;

import com.vaadin.hilla.Endpoint;
import com.vaadin.hilla.InternalBrowserCallable;

@Endpoint
@InternalBrowserCallable
public class InternalEndpointExample {
public String exampleMethod() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server.port=8888
vaadin.devmode.liveReload.enabled=false
logging.level.org.atmosphere = warn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.application;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class InternalEndpointIT {
Path frontendDir = Paths.get("frontend", "generated");

@Test
public void shouldNotRunEndpointGeneratorForOnlyInternalEndpointsPresent() {
assertFalse(Files.exists(frontendDir.resolve("InternalBrowserCallableExample.ts")));
assertFalse(Files.exists(frontendDir.resolve("InternalEndpointExample.ts")));
assertFalse(Files.exists(frontendDir
.resolve("com/example/application/InternalBrowserCallableExample.ts")));
assertFalse(Files.exists(frontendDir
.resolve("com/example/application/InternalEndpointExample.ts")));
}


}

0 comments on commit 5e2e68c

Please sign in to comment.