Skip to content
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
45 changes: 37 additions & 8 deletions src/test/groovy/groovy/util/logging/Slf4jTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.spi.LoggingEvent
import ch.qos.logback.core.OutputStreamAppender
import ch.qos.logback.core.layout.EchoLayout
import org.junit.After
import org.junit.Before
import org.junit.Test
import groovy.test.NotYetImplemented
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.slf4j.LoggerFactory

import java.lang.reflect.Modifier
Expand Down Expand Up @@ -60,7 +63,7 @@ final class Slf4jTest {
private LogbackInterceptingAppender appender
private Logger logger

@Before
@BeforeEach
void setUp() {
appender = new LogbackInterceptingAppender()
appender.outputStream = new ByteArrayOutputStream()
Expand All @@ -74,7 +77,7 @@ final class Slf4jTest {
logger.level = Level.ALL
}

@After
@AfterEach
void tearDown() {
logger.detachAppender(appender)
}
Expand Down Expand Up @@ -293,7 +296,8 @@ final class Slf4jTest {
assert events[ind].message == 'trace called'
}

@Test // GROOVY-6373
// GROOVY-6373
@Test
void testLogWithInnerClasses() {
Class clazz = new GroovyClassLoader().parseClass('''
@groovy.util.logging.Slf4j('logger')
Expand Down Expand Up @@ -323,7 +327,8 @@ final class Slf4jTest {
assert events[ind].message == 'inner called'
}

@Test // GROOVY-6834
// GROOVY-6834
@Test
void testLogTransformInteractionWithAnonInnerClass() {
assertScript '''
@groovy.util.logging.Slf4j
Expand All @@ -344,7 +349,8 @@ final class Slf4jTest {
'''
}

@Test // GROOVY-6873
// GROOVY-6873
@Test
void testLogTransformInteractionWithAnonInnerClass2() {
Class clazz = new GroovyClassLoader().parseClass('''
@groovy.util.logging.Slf4j
Expand All @@ -367,6 +373,29 @@ final class Slf4jTest {
''')
}

// GROOVY-7439
@NotYetImplemented
@ParameterizedTest
@ValueSource(strings=[
'log', // Cannot find matching method Object#debug(String)
'((org.slf4j.Logger) log)' // No such property: log for class: C
])
void testLogTrait(String log) {
assertScript """
@groovy.transform.CompileStatic
@groovy.util.logging.Slf4j
trait T {
void test() {
${log}.debug('trace')
}
}
class C implements T {
}

new C().test()
"""
}

@Test
void testLogGuard() {
Class clazz = new GroovyClassLoader().parseClass('''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2509,43 +2509,37 @@ final class TraitASTTransformationTest {
@Test
void testUseStaticFieldInTraitBody() {
assertScript shell, '''
import java.util.logging.Logger

trait Loggable {

static def LOGGER = Logger.getLogger(this.class.name)

private static LOGGER = java.util.logging.Logger.getLogger(this.class.name)
void info(String msg) {
LOGGER.info(msg)
}
}
class C implements Loggable {
}

class Test implements Loggable {}

def t = new Test()
t.info('foo')
def pogo = new C()
pogo.info('foo')
'''
}

@Test
void testUpdateStaticFieldInTraitBody() {
assertScript shell, '''
trait Loggable {

static int CALLS = 0

int call() {
CALLS += 1
CALLS
}
}
class C implements Loggable {
}

class Test implements Loggable {}

def t = new Test()
assert t.call() == 1
assert t.call() == 2
assert Test.CALLS == 2
def pogo = new C()
assert pogo.call() == 1
assert pogo.call() == 2
assert pogo.CALLS == 2
'''
}

Expand Down