Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Groovy/Spock bug in Kubernetes module #9067

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static List<PropertyElement> resolveBeanProperties(PropertyElementQuery c
continue;
}
String methodName = methodElement.getName();
if (methodName.contains("$") || methodName.equals("getMetaClass")) {
if (methodName.equals("getMetaClass")) {
continue;
}
boolean isAccessor = canMethodBeUsedForAccess(methodElement, accessKinds, visibility);
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/io/micronaut/core/naming/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ public static boolean isWriterName(@NonNull String methodName, @NonNull String[]
int len = methodName.length();
int prefixLength = writePrefix.length();
if (len > prefixLength && methodName.startsWith(writePrefix)) {
isValid = Character.isUpperCase(methodName.charAt(prefixLength));
char nextChar = methodName.charAt(prefixLength);
isValid = isValidCharacterAfterReaderWriterPrefix(nextChar);
}

if (isValid) {
Expand Down Expand Up @@ -370,7 +371,7 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
int len = methodName.length();
if (len > prefixLength) {
char firstVarNameChar = methodName.charAt(prefixLength);
isValid = firstVarNameChar == '_' || firstVarNameChar == '$' || Character.isUpperCase(firstVarNameChar);
isValid = isValidCharacterAfterReaderWriterPrefix(firstVarNameChar);
}

if (isValid) {
Expand All @@ -381,6 +382,10 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
return isValid;
}

private static boolean isValidCharacterAfterReaderWriterPrefix(char c) {
return c == '_' || c == '$' || Character.isUpperCase(c);
}

/**
* Get the equivalent property name for the given getter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ class NameUtilsSpec extends Specification {
"isFoo" | ["get"] | true
"isfoo" | ["get"] | false
"getFoo" | ["get"] | true
'get$foo' | ["get"] | true
'get_foo' | ["get"] | true
"getfoo" | ["get"] | false
"a" | ["get"] | false
"foo" | ["with"] | false
Expand All @@ -322,6 +324,8 @@ class NameUtilsSpec extends Specification {
name | prefixes | isValid
"foo" | ["set"] | false
"setFoo" | ["set"] | true
'set$foo' | ["set"] | true
'set_foo' | ["set"] | true
"setfoo" | ["set"] | false
"a" | ["set"] | false
"foo" | ["with"] | false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public class GroovyClassElement extends AbstractGroovyElement implements Arrayab
private static final Predicate<FieldNode> JUNK_FIELD_FILTER = m -> {
String fieldName = m.getName();

return fieldName.startsWith("$") ||
fieldName.startsWith("__$") ||
return fieldName.startsWith("__$") ||
fieldName.contains("trait$") ||
fieldName.equals("metaClass") ||
m.getDeclaringClass().equals(ClassHelper.GROOVY_OBJECT_TYPE) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,13 @@ class SuccessfulTest extends AbstractExample {
def allFields = classElement.getEnclosedElements(ElementQuery.ALL_FIELDS)
then:
props.size() == 3
props[0].name == "ctx"
props[1].name.contains "dummy"
props[2].name.contains "sharedCtx"
props[0].name == '$spock_sharedField_sharedCtx'
props[1].name == "ctx"
props[2].name.contains "dummy"
allFields.size() == 3
allFields[0].name == '$spock_sharedField_sharedCtx'
allFields[1].name == "ctx"
allFields[2].name.contains "dummy"
}

void "test fields selection"() {
Expand Down
1 change: 1 addition & 0 deletions test-suite-groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
testImplementation project(":http-client")
testImplementation project(":http-client-jdk")
testImplementation project(":inject-groovy")
testImplementation project(":inject-groovy-test")
testImplementation project(":http-server-netty")
testImplementation project(":jackson-databind")
testImplementation project(":runtime")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.micronaut.inject.spock.another

import io.micronaut.ast.transform.test.AbstractBeanDefinitionSpec
import io.micronaut.inject.BeanDefinition

class InjectFromParentInAnotherPackageCompiledSpec extends AbstractBeanDefinitionSpec {

void "test compile spock specification that inherits from already compiled class"() {
given:
def definition = buildBeanDefinition('test.MySpockSpec', '''
package test

import io.micronaut.inject.spock.other.AbstractMicronautTestSpec
import io.micronaut.test.extensions.spock.annotation.MicronautTest

@MicronautTest
class MySpockSpec extends AbstractMicronautTestSpec {

}
''')
expect:
definition != null
definition.injectedFields.size() == 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.micronaut.inject.spock.another


import io.micronaut.core.convert.ConversionService
import io.micronaut.inject.spock.other.AbstractMicronautTestSpec
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Shared

@MicronautTest
class InjectFromParentInAnotherPackageSpec extends AbstractMicronautTestSpec {

@Inject
EmbeddedServer embeddedServer

@Inject
@Shared
ConversionService sharedTest

void "test parent injected"() {
expect:"parent and child beans are injected"
embeddedServer != null
sharedTest != null
sharedFromParent != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.micronaut.inject.spock.other

import io.micronaut.context.env.Environment
import jakarta.inject.Inject
import spock.lang.Shared
import spock.lang.Specification

abstract class AbstractMicronautTestSpec extends Specification {

@Inject
@Shared
Environment sharedFromParent
}