-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration option to disable usage of JSqlParser for native qu…
…eries. This commit introduces the spring.data.jpa.query.native.parser property that allows to switch native query parsing to the default internal parser for scenarios where JSqlParser is on the classpath but should not be used by spring-data. Closes #2989 Original pull request: #3623
- Loading branch information
1 parent
f474af7
commit c56851c
Showing
7 changed files
with
450 additions
and
17 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
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
45 changes: 45 additions & 0 deletions
45
spring-data-jpa/src/test/java/org/springframework/data/jpa/util/ClassPathExclusions.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,45 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.jpa.util; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
/** | ||
* Annotation used to exclude entries from the classpath. Simplified version of <a href= | ||
* "https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ClassPathExclusions.java">ClassPathExclusions</a>. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Documented | ||
@ExtendWith(ClassPathExclusionsExtension.class) | ||
public @interface ClassPathExclusions { | ||
|
||
/** | ||
* One or more packages that should be excluded from the classpath. | ||
* | ||
* @return the excluded packages | ||
*/ | ||
String[] packages(); | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
...ata-jpa/src/test/java/org/springframework/data/jpa/util/ClassPathExclusionsExtension.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,130 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.jpa.util; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.InvocationInterceptor; | ||
import org.junit.jupiter.api.extension.ReflectiveInvocationContext; | ||
import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
import org.junit.platform.launcher.Launcher; | ||
import org.junit.platform.launcher.LauncherDiscoveryRequest; | ||
import org.junit.platform.launcher.TestPlan; | ||
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; | ||
import org.junit.platform.launcher.core.LauncherFactory; | ||
import org.junit.platform.launcher.listeners.SummaryGeneratingListener; | ||
import org.junit.platform.launcher.listeners.TestExecutionSummary; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* Simplified version of <a href= | ||
* "https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java">ModifiedClassPathExtension</a>. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
class ClassPathExclusionsExtension implements InvocationInterceptor { | ||
|
||
@Override | ||
public void interceptBeforeAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
intercept(invocation, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptBeforeEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
intercept(invocation, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptAfterEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
intercept(invocation, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptAfterAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
intercept(invocation, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, | ||
ExtensionContext extensionContext) throws Throwable { | ||
interceptMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptTestTemplateMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
private void interceptMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, | ||
ExtensionContext extensionContext) throws Throwable { | ||
|
||
if (isModifiedClassPathClassLoader(extensionContext)) { | ||
invocation.proceed(); | ||
return; | ||
} | ||
|
||
Class<?> testClass = extensionContext.getRequiredTestClass(); | ||
Method testMethod = invocationContext.getExecutable(); | ||
PackageExcludingClassLoader modifiedClassLoader = PackageExcludingClassLoader.get(testClass, testMethod); | ||
if (modifiedClassLoader == null) { | ||
invocation.proceed(); | ||
return; | ||
} | ||
invocation.skip(); | ||
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); | ||
Thread.currentThread().setContextClassLoader(modifiedClassLoader); | ||
try { | ||
runTest(extensionContext.getUniqueId()); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(originalClassLoader); | ||
} | ||
} | ||
|
||
private void runTest(String testId) throws Throwable { | ||
|
||
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() | ||
.selectors(DiscoverySelectors.selectUniqueId(testId)).build(); | ||
Launcher launcher = LauncherFactory.create(); | ||
TestPlan testPlan = launcher.discover(request); | ||
SummaryGeneratingListener listener = new SummaryGeneratingListener(); | ||
launcher.registerTestExecutionListeners(listener); | ||
launcher.execute(testPlan); | ||
TestExecutionSummary summary = listener.getSummary(); | ||
if (!CollectionUtils.isEmpty(summary.getFailures())) { | ||
throw summary.getFailures().get(0).getException(); | ||
} | ||
} | ||
|
||
private void intercept(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable { | ||
if (isModifiedClassPathClassLoader(extensionContext)) { | ||
invocation.proceed(); | ||
return; | ||
} | ||
invocation.skip(); | ||
} | ||
|
||
private boolean isModifiedClassPathClassLoader(ExtensionContext extensionContext) { | ||
Class<?> testClass = extensionContext.getRequiredTestClass(); | ||
ClassLoader classLoader = testClass.getClassLoader(); | ||
return classLoader.getClass().getName().equals(PackageExcludingClassLoader.class.getName()); | ||
} | ||
} |
Oops, something went wrong.