15
15
*/
16
16
package org .springframework .data .jpa .repository .aot ;
17
17
18
- import static org .assertj .core .api .Assertions .assertThat ;
19
- import static org .mockito . ArgumentMatchers . eq ;
20
- import static org .mockito .Mockito .when ;
18
+ import static org .assertj .core .api .Assertions .* ;
19
+ import static org .assertj . core . api . InstanceOfAssertFactories .* ;
20
+ import static org .mockito .Mockito .* ;
21
21
22
+ import jakarta .persistence .Entity ;
22
23
import jakarta .persistence .EntityManagerFactory ;
24
+ import jakarta .persistence .Id ;
25
+
26
+ import java .lang .reflect .Method ;
27
+ import java .util .Collection ;
28
+ import java .util .Collections ;
23
29
24
- import org .assertj .core .api .InstanceOfAssertFactories ;
25
30
import org .junit .jupiter .api .BeforeEach ;
26
31
import org .junit .jupiter .api .Test ;
27
- import org .mockito .Mockito ;
28
- import org .springframework .core .annotation .MergedAnnotation ;
32
+
33
+ import org .springframework .core .annotation .MergedAnnotations ;
34
+ import org .springframework .data .jpa .provider .QueryExtractor ;
29
35
import org .springframework .data .jpa .repository .Query ;
30
- import org .springframework .data .jpa .repository .query .JpaEntityMetadata ;
31
36
import org .springframework .data .jpa .repository .query .JpaQueryMethod ;
32
37
import org .springframework .data .jpa .repository .query .QueryEnhancerSelector ;
33
- import org .springframework .data .jpa .repository .support .JpaEntityInformation ;
34
38
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
39
+ import org .springframework .data .repository .Repository ;
40
+ import org .springframework .data .repository .config .AotRepositoryInformation ;
35
41
import org .springframework .data .repository .config .RepositoryConfigurationSource ;
36
42
import org .springframework .data .repository .core .RepositoryInformation ;
37
- import org .springframework .data .repository .query . ReturnedType ;
43
+ import org .springframework .data .repository .core . support . AbstractRepositoryMetadata ;
38
44
39
45
/**
40
46
* Unit tests for {@link QueriesFactory}.
41
47
*
42
48
* @author Christoph Strobl
49
+ * @author Mark Paluch
43
50
*/
44
51
class QueriesFactoryUnitTests {
45
52
@@ -48,36 +55,42 @@ class QueriesFactoryUnitTests {
48
55
@ BeforeEach
49
56
void setUp () {
50
57
51
- RepositoryConfigurationSource configSource = Mockito . mock (RepositoryConfigurationSource .class );
52
- EntityManagerFactory entityManagerFactory = Mockito . mock (EntityManagerFactory .class );
58
+ RepositoryConfigurationSource configSource = mock (RepositoryConfigurationSource .class );
59
+ EntityManagerFactory entityManagerFactory = mock (EntityManagerFactory .class );
53
60
54
61
factory = new QueriesFactory (configSource , entityManagerFactory , this .getClass ().getClassLoader ());
55
62
}
56
63
57
64
@ Test // GH-4029
58
- @ SuppressWarnings ({ "rawtypes" , "unchecked" })
59
- void stringQueryShouldResolveEntityNameFromJakartaAnnotationIfPresent () {
60
-
61
- RepositoryInformation repositoryInformation = Mockito .mock (RepositoryInformation .class );
62
- JpaEntityMetadata <?> entityMetadata = Mockito .mock (JpaEntityInformation .class );
63
- when (entityMetadata .getEntityName ()).thenReturn ("CustomNamed" );
65
+ void stringQueryShouldResolveEntityNameFromJakartaAnnotationIfPresent () throws NoSuchMethodException {
64
66
65
- MergedAnnotation <Query > queryAnnotation = Mockito .mock (MergedAnnotation .class );
66
- when (queryAnnotation .isPresent ()).thenReturn (true );
67
- when (queryAnnotation .getString (eq ("value" ))).thenReturn ("select t from #{#entityName} t" );
68
- when (queryAnnotation .getBoolean (eq ("nativeQuery" ))).thenReturn (false );
69
- when (queryAnnotation .getString ("countQuery" )).thenReturn ("select count(t) from #{#entityName} t" );
67
+ RepositoryInformation repositoryInformation = new AotRepositoryInformation (
68
+ AbstractRepositoryMetadata .getMetadata (MyRepository .class ), MyRepository .class , Collections .emptyList ());
70
69
71
- JpaQueryMethod queryMethod = Mockito .mock (JpaQueryMethod .class );
72
- when (queryMethod .getEntityInformation ()).thenReturn ((JpaEntityMetadata ) entityMetadata );
70
+ Method method = MyRepository .class .getMethod ("someFind" );
71
+ JpaQueryMethod queryMethod = new JpaQueryMethod (method , repositoryInformation ,
72
+ new SpelAwareProxyProjectionFactory (), mock (QueryExtractor .class ));
73
73
74
74
AotQueries generatedQueries = factory .createQueries (repositoryInformation ,
75
- ReturnedType . of ( Object . class , Object . class , new SpelAwareProxyProjectionFactory ()) ,
76
- QueryEnhancerSelector . DEFAULT_SELECTOR , queryAnnotation , queryMethod );
75
+ queryMethod . getResultProcessor (). getReturnedType (), QueryEnhancerSelector . DEFAULT_SELECTOR ,
76
+ MergedAnnotations . from ( method ). get ( Query . class ) , queryMethod );
77
77
78
- assertThat (generatedQueries .result ()).asInstanceOf (InstanceOfAssertFactories . type (StringAotQuery .class ))
78
+ assertThat (generatedQueries .result ()).asInstanceOf (type (StringAotQuery .class ))
79
79
.extracting (StringAotQuery ::getQueryString ).isEqualTo ("select t from CustomNamed t" );
80
- assertThat (generatedQueries .count ()).asInstanceOf (InstanceOfAssertFactories . type (StringAotQuery .class ))
80
+ assertThat (generatedQueries .count ()).asInstanceOf (type (StringAotQuery .class ))
81
81
.extracting (StringAotQuery ::getQueryString ).isEqualTo ("select count(t) from CustomNamed t" );
82
82
}
83
+
84
+ interface MyRepository extends Repository <MyEntity , Long > {
85
+
86
+ @ Query ("select t from #{#entityName} t" )
87
+ Collection <MyEntity > someFind ();
88
+ }
89
+
90
+ @ Entity (name = "CustomNamed" )
91
+ static class MyEntity {
92
+
93
+ @ Id Long id ;
94
+
95
+ }
83
96
}
0 commit comments