groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emil...@apache.org
Subject [groovy] branch master updated: minor refactor
Date Tue, 15 Oct 2019 18:30:09 GMT
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 98312a1  minor refactor
98312a1 is described below

commit 98312a1e1b9e68e9ba464497ef17999a73a2987b
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Tue Oct 15 13:28:45 2019 -0500

    minor refactor
---
 .../org/codehaus/groovy/ast/VariableScope.java     | 216 ++++++-----
 src/test/groovy/util/logging/Slf4jTest.groovy      | 416 +++++++++++----------
 2 files changed, 327 insertions(+), 305 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/VariableScope.java b/src/main/java/org/codehaus/groovy/ast/VariableScope.java
index c0dba87..9a32e8e 100644
--- a/src/main/java/org/codehaus/groovy/ast/VariableScope.java
+++ b/src/main/java/org/codehaus/groovy/ast/VariableScope.java
@@ -28,132 +28,96 @@ import java.util.Map;
  * across method and closure boundaries.
  */
 public class VariableScope  {
+
+    private VariableScope parent;
+    private ClassNode classScope;
+    private boolean inStaticContext;
+    private boolean resolvesDynamic;
+
     private Map<String, Variable> declaredVariables = Collections.emptyMap();
     private Map<String, Variable> referencedLocalVariables = Collections.emptyMap();
     private Map<String, Variable> referencedClassVariables = Collections.emptyMap();
- 
-    private boolean inStaticContext = false;
-    private boolean resolvesDynamic = false; 
-    // Non-null iff this scope corresponds to a class, as opposed to a method, "if" statement,
-    // block statement, etc.
-    private ClassNode clazzScope;
-    private VariableScope parent;
 
     public VariableScope() {
+        super();
     }
+
     public VariableScope(VariableScope parent) {
         this.parent = parent;
     }
 
-    public Variable getDeclaredVariable(String name) {
-        return declaredVariables.get(name);
-    }
-
-    public boolean isReferencedLocalVariable(String name) {
-        return referencedLocalVariables.containsKey(name);
-    }
-    
-    public boolean isReferencedClassVariable(String name) {
-        return referencedClassVariables.containsKey(name);
-    }
     public VariableScope getParent() {
         return parent;
     }
 
-    public boolean isInStaticContext() {
-        return inStaticContext;
-    }
-
-    public void setInStaticContext(boolean inStaticContext) {
-        this.inStaticContext = inStaticContext;
+    public boolean isRoot() {
+        return parent == null;
     }
 
-    public void setClassScope(ClassNode node) {
-        this.clazzScope = node;
-    }
-    
     /**
-     * Non-null iff this scope corresponds to a class; as opposed to a method, "if" statement,
-     * block statement, etc.
+     * Non-null iff this scope corresponds to a class; as opposed to a method, "if" statement,
block statement, etc.
      */
-    public ClassNode getClassScope(){
-        return clazzScope;
+    public ClassNode getClassScope() {
+        return classScope;
     }
-    
+
     /**
-     * Returns true iff this scope corresponds to a class; as opposed to a method, "if" statement,
-     * block statement, etc.
+     * Returns true iff this scope corresponds to a class; as opposed to a method, "if" statement,
block statement, etc.
      */
-    public boolean isClassScope(){
-        return clazzScope!=null;
-    }
-    
-    public boolean isRoot() {
-        return parent==null;
+    public boolean isClassScope() {
+        return classScope != null;
     }
-    
-    public VariableScope copy() {
-        VariableScope copy = new VariableScope();
-        copy.clazzScope = clazzScope;
-        if (!declaredVariables.isEmpty()) {
-          copy.declaredVariables = new LinkedHashMap<String, Variable>(declaredVariables);
-        }
-        copy.inStaticContext = inStaticContext;
-        copy.parent = parent;
-        if (!referencedClassVariables.isEmpty()) {
-            copy.referencedClassVariables = new LinkedHashMap<String, Variable>(referencedClassVariables);
-        }
-        if (!referencedLocalVariables.isEmpty()) {
-            copy.referencedLocalVariables = new LinkedHashMap<String, Variable>(referencedLocalVariables);
-        }
-        copy.resolvesDynamic = resolvesDynamic;
-        return copy;
+
+    public void setClassScope(ClassNode classScope) {
+        this.classScope = classScope;
     }
 
-    public void putDeclaredVariable(Variable var) {
-        if (declaredVariables == Collections.EMPTY_MAP)
-          declaredVariables = new LinkedHashMap<String, Variable>();
-        declaredVariables.put(var.getName(), var);
+    public boolean isInStaticContext() {
+        return inStaticContext;
     }
 
-    public Iterator<Variable> getReferencedLocalVariablesIterator() {
-        return referencedLocalVariables.values().iterator();
+    public void setInStaticContext(boolean inStaticContext) {
+        this.inStaticContext = inStaticContext;
     }
 
-    public int getReferencedLocalVariablesCount() {
-        return referencedLocalVariables.size();
+    //
+
+    public Variable getDeclaredVariable(String name) {
+        return declaredVariables.get(name);
     }
 
     public Variable getReferencedLocalVariable(String name) {
         return referencedLocalVariables.get(name);
     }
 
-    public void putReferencedLocalVariable(Variable var) {
-        if (referencedLocalVariables == Collections.EMPTY_MAP)
-          referencedLocalVariables = new LinkedHashMap<String, Variable>();
-        referencedLocalVariables.put(var.getName(), var);
+    public Variable getReferencedClassVariable(String name) {
+        return referencedClassVariables.get(name);
     }
 
-    public void putReferencedClassVariable(Variable var) {
-        if (referencedClassVariables == Collections.EMPTY_MAP)
-          referencedClassVariables = new LinkedHashMap<String, Variable>();
-        referencedClassVariables.put(var.getName(), var);
+    public boolean isReferencedLocalVariable(String name) {
+        return referencedLocalVariables.containsKey(name);
     }
 
-    public Variable getReferencedClassVariable(String name) {
-        return referencedClassVariables.get(name);
+    public boolean isReferencedClassVariable(String name) {
+        return referencedClassVariables.containsKey(name);
     }
 
-    public Object removeReferencedClassVariable(String name) {
-        if (referencedClassVariables == Collections.EMPTY_MAP)
-          return null;
-        else
-          return referencedClassVariables.remove(name);
+    /**
+     * Gets a map containing the variables declared in this scope. This map cannot be modified.
+     *
+     * @return a map containing the declared variable references
+     */
+    public Map<String, Variable> getDeclaredVariables() {
+        if (declaredVariables == Collections.EMPTY_MAP) {
+            return declaredVariables;
+        } else {
+            return Collections.unmodifiableMap(declaredVariables);
+        }
     }
-    
+
     /**
-     * Gets a map containing the class variables referenced 
-     * by this scope. This not can not be modified.
+     * Gets a map containing the class variables referenced by this scope. This not can not
be modified.
+     *
      * @return a map containing the class variable references
      */
     public Map<String, Variable> getReferencedClassVariables() {
@@ -163,35 +127,81 @@ public class VariableScope  {
             return Collections.unmodifiableMap(referencedClassVariables);
         }
     }
-    
+
+    public int getReferencedLocalVariablesCount() {
+        return referencedLocalVariables.size();
+    }
+
+    /**
+     * Gets an iterator for the declared class variables. The remove operation is not supported.
+     *
+     * @return an iterator for the declared variables
+     */
+    public Iterator<Variable> getDeclaredVariablesIterator() {
+        return getDeclaredVariables().values().iterator();
+    }
+
     /**
-     * Gets an iterator for the referenced class variables. The
-     * remove operation is not supported.
+     * Gets an iterator for the referenced local variables. The remove operation *is* supported.
+     *
+     * @return an iterator for the referenced local variables
+     */
+    public Iterator<Variable> getReferencedLocalVariablesIterator() {
+        return referencedLocalVariables.values().iterator();
+    }
+
+    /**
+     * Gets an iterator for the referenced class variables. The remove operation is not supported.
+     *
      * @return an iterator for the referenced class variables
      */
     public Iterator<Variable> getReferencedClassVariablesIterator() {
         return getReferencedClassVariables().values().iterator();
     }
 
-    /**
-     * Gets a map containing the variables declared in this scope.
-     * This map cannot be modified.
-     * @return a map containing the declared variable references
-     */
-    public Map<String, Variable> getDeclaredVariables() {
-        if (declaredVariables == Collections.EMPTY_MAP) {
-            return declaredVariables;
+    public void putDeclaredVariable(Variable var) {
+        if (declaredVariables == Collections.EMPTY_MAP)
+            declaredVariables = new LinkedHashMap<>();
+        declaredVariables.put(var.getName(), var);
+    }
+
+    public void putReferencedLocalVariable(Variable var) {
+        if (referencedLocalVariables == Collections.EMPTY_MAP)
+            referencedLocalVariables = new LinkedHashMap<>();
+        referencedLocalVariables.put(var.getName(), var);
+    }
+
+    public void putReferencedClassVariable(Variable var) {
+        if (referencedClassVariables == Collections.EMPTY_MAP)
+            referencedClassVariables = new LinkedHashMap<>();
+        referencedClassVariables.put(var.getName(), var);
+    }
+
+    public Object removeReferencedClassVariable(String name) {
+        if (referencedClassVariables.isEmpty()) {
+            return null;
         } else {
-            return Collections.unmodifiableMap(declaredVariables);
+            return referencedClassVariables.remove(name);
         }
     }
 
-    /**
-     * Gets an iterator for the declared class variables. The remove
-     * operation is not supported.
-     * @return an iterator for the declared variables
-     */
-    public Iterator<Variable> getDeclaredVariablesIterator() {
-        return getDeclaredVariables().values().iterator();
+    //
+
+    // TODO: implement Cloneable and override Object.clone()
+    public VariableScope copy() {
+        VariableScope copy = new VariableScope(parent);
+        copy.classScope = classScope;
+        copy.inStaticContext = inStaticContext;
+        copy.resolvesDynamic = resolvesDynamic;
+        if (!declaredVariables.isEmpty()) {
+            copy.declaredVariables = new LinkedHashMap<>(declaredVariables);
+        }
+        if (!referencedLocalVariables.isEmpty()) {
+            copy.referencedLocalVariables = new LinkedHashMap<>(referencedLocalVariables);
+        }
+        if (!referencedClassVariables.isEmpty()) {
+            copy.referencedClassVariables = new LinkedHashMap<>(referencedClassVariables);
+        }
+        return copy;
     }
 }
diff --git a/src/test/groovy/util/logging/Slf4jTest.groovy b/src/test/groovy/util/logging/Slf4jTest.groovy
index af114bc..880463c 100644
--- a/src/test/groovy/util/logging/Slf4jTest.groovy
+++ b/src/test/groovy/util/logging/Slf4jTest.groovy
@@ -24,229 +24,253 @@ 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 groovy.test.GroovyTestCase
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
 import org.slf4j.LoggerFactory
 
-import java.lang.reflect.Field
 import java.lang.reflect.Modifier
 
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
+
 /**
- * Tests for Slf4j AST transformation
+ * Tests for {@link groovy.util.logging.Slf4j Slf4j} AST transformation.
  */
+final class Slf4jTest {
 
-class Slf4jTest extends GroovyTestCase {
+    private static class LogbackInterceptingAppender<E> extends OutputStreamAppender<E>
{
 
-    LogbackInterceptingAppender appender
-    Logger logger
+        private List<LoggingEvent> events = []
 
-    protected void setUp() {
-        super.setUp()
-        logger = LoggerFactory.getLogger("MyClass")
+        List<LoggingEvent> getEvents() {
+            return events
+        }
 
+        protected void append(E event) {
+            if (event instanceof LoggingEvent) {
+                events.add(event)
+                super.append(event)
+            } else {
+                throw new RuntimeException('Unable to intercept logging events - probably
API has changed')
+            }
+        }
+    }
+
+    private LogbackInterceptingAppender appender
+    private Logger logger
+
+    @Before
+    void setUp() {
         appender = new LogbackInterceptingAppender()
-        appender.setOutputStream(new ByteArrayOutputStream())
-        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory()
-        appender.setContext(lc)
-        appender.setName("MyAppender")
-        appender.setLayout(new EchoLayout())
+        appender.outputStream = new ByteArrayOutputStream()
+        appender.context = (LoggerContext) LoggerFactory.getILoggerFactory()
+        appender.layout = new EchoLayout()
+        appender.name = 'MyAppender'
         appender.start()
 
+        logger = LoggerFactory.getLogger('MyClass')
         logger.addAppender(appender)
-        logger.setLevel(Level.ALL)
+        logger.level = Level.ALL
     }
 
-    protected void tearDown() {
-        super.tearDown()
+    @After
+    void tearDown() {
         logger.detachAppender(appender)
     }
 
+    @Test
     void testPrivateFinalStaticLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
-                @groovy.util.logging.Slf4j
-                class MyClass {
-                } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "log" &&
-                    Modifier.isPrivate(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            @groovy.util.logging.Slf4j
+            class MyClass {
+            }
+        ''')
+
+        clazz.getDeclaredField('log').modifiers.with { int modifiers ->
+            assert Modifier.isPrivate(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testExplicitPrivateFinalStaticLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
             import static groovy.transform.options.Visibility.*
             @groovy.transform.VisibilityOptions(value = PRIVATE)
             @groovy.util.logging.Slf4j
             class MyClass {
-            } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "log" &&
-                    Modifier.isPrivate(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            }
+        ''')
+
+        clazz.getDeclaredField('log').modifiers.with { int modifiers ->
+            assert Modifier.isPrivate(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testPackagePrivateFinalStaticLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
             import static groovy.transform.options.Visibility.*
             @groovy.transform.VisibilityOptions(value = PACKAGE_PRIVATE)
             @groovy.util.logging.Slf4j
             class MyClass {
-            } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "log" &&
-                    !Modifier.isPrivate(field.getModifiers()) &&
-                    !Modifier.isProtected(field.getModifiers()) &&
-                    !Modifier.isPublic(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            }
+        ''')
+
+        clazz.getDeclaredField('log').modifiers.with { int modifiers ->
+            assert !Modifier.isPrivate(modifiers)
+            assert !Modifier.isProtected(modifiers)
+            assert !Modifier.isPublic(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testProtectedFinalStaticLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
             import static groovy.transform.options.Visibility.*
             @groovy.transform.VisibilityOptions(value = PROTECTED)
             @groovy.util.logging.Slf4j
             class MyClass {
-            } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "log" &&
-                    Modifier.isProtected(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            }
+        ''')
+
+        clazz.getDeclaredField('log').modifiers.with { int modifiers ->
+            assert Modifier.isProtected(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testPublicFinalStaticLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
             import static groovy.transform.options.Visibility.*
             @groovy.transform.VisibilityOptions(value = PUBLIC)
             @groovy.util.logging.Slf4j
             class MyClass {
-            } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "log" &&
-                    Modifier.isPublic(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            }
+        ''')
+
+        clazz.getDeclaredField('log').modifiers.with { int modifiers ->
+            assert Modifier.isPublic(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testPrivateFinalStaticNamedLogFieldAppears() {
         Class clazz = new GroovyClassLoader().parseClass('''
-                @groovy.util.logging.Slf4j('logger')
-                class MyClass {
-                } ''')
-
-        assert clazz.declaredFields.find { Field field ->
-            field.name == "logger" &&
-                    Modifier.isPrivate(field.getModifiers()) &&
-                    Modifier.isStatic(field.getModifiers()) &&
-                    Modifier.isTransient(field.getModifiers()) &&
-                    Modifier.isFinal(field.getModifiers())
+            @groovy.util.logging.Slf4j('logger')
+            class MyClass {
+            }
+        ''')
+
+        clazz.getDeclaredField('logger').modifiers.with { int modifiers ->
+            assert Modifier.isPrivate(modifiers)
+            assert Modifier.isStatic(modifiers)
+            assert Modifier.isTransient(modifiers)
+            assert Modifier.isFinal(modifiers)
         }
     }
 
+    @Test
     void testClassAlreadyHasLogField() {
-        shouldFail {
-            Class clazz = new GroovyClassLoader().parseClass('''
-                @groovy.util.logging.Slf4j
-                class MyClass {
-                    String log
-                } ''')
-
-            assert clazz.newInstance()
-        }
+        shouldFail '''
+            @groovy.util.logging.Slf4j
+            class MyClass {
+                String log
+            }
+        '''
     }
 
+    @Test
     void testClassAlreadyHasNamedLogField() {
-        shouldFail {
-            Class clazz = new GroovyClassLoader().parseClass('''
-                @groovy.util.logging.Slf4j('logger')
-                class MyClass {
-                    String logger
-                } ''')
-
-            assert clazz.newInstance()
-        }
+        shouldFail '''
+            @groovy.util.logging.Slf4j('logger')
+            class MyClass {
+                String logger
+            }
+        '''
     }
 
+    @Test
     void testLogInfo() {
         Class clazz = new GroovyClassLoader().parseClass('''
-          @groovy.util.logging.Slf4j
-          class MyClass {
-
-              def loggingMethod() {
-                  log.error ("error called")
-                  log.warn  ("warn called")
-                  log.info  ("info called")
-                  log.debug ("debug called")
-                  log.trace ("trace called")
-              }
-          }
-          new MyClass().loggingMethod() ''')
+            @groovy.util.logging.Slf4j
+            class MyClass {
+                def loggingMethod() {
+                    log.error ('error called')
+                    log.warn  ('warn called')
+                    log.info  ('info called')
+                    log.debug ('debug called')
+                    log.trace ('trace called')
+                }
+            }
+            new MyClass().loggingMethod()
+        ''')
 
         Script s = (Script) clazz.newInstance()
         s.run()
 
-        def events = appender.getEvents()
+        def events = appender.events
         int ind = 0
         assert events.size() == 5
         assert events[ind].level == Level.ERROR
-        assert events[ind].message == "error called"
+        assert events[ind].message == 'error called'
         assert events[++ind].level == Level.WARN
-        assert events[ind].message == "warn called"
+        assert events[ind].message == 'warn called'
         assert events[++ind].level == Level.INFO
-        assert events[ind].message == "info called"
+        assert events[ind].message == 'info called'
         assert events[++ind].level == Level.DEBUG
-        assert events[ind].message == "debug called"
+        assert events[ind].message == 'debug called'
         assert events[++ind].level == Level.TRACE
-        assert events[ind].message == "trace called"
+        assert events[ind].message == 'trace called'
     }
 
+    @Test
     void testLogFromStaticMethods() {
-        Class clazz = new GroovyClassLoader().parseClass("""
+        Class clazz = new GroovyClassLoader().parseClass('''
             @groovy.util.logging.Slf4j
             class MyClass {
                 static loggingMethod() {
-                  log.info   ("(static) info called")
+                    log.info('(static) info called')
                 }
             }
-            MyClass.loggingMethod()""")
+            MyClass.loggingMethod()
+        ''')
 
         Script s = (Script) clazz.newInstance()
         s.run()
 
-        def events = appender.getEvents()
+        def events = appender.events
         assert events.size() == 1
         assert events[0].level == Level.INFO
-        assert events[0].message == "(static) info called"
+        assert events[0].message == '(static) info called'
     }
 
+    @Test
     void testLogInfoWithNamedLogger() {
         Class clazz = new GroovyClassLoader().parseClass('''
           @groovy.util.logging.Slf4j('logger')
           class MyClass {
-
               def loggingMethod() {
-                  logger.error ("error called")
-                  logger.warn  ("warn called")
-                  logger.info  ("info called")
-                  logger.debug ("debug called")
-                  logger.trace ("trace called")
+                  logger.error ('error called')
+                  logger.warn  ('warn called')
+                  logger.info  ('info called')
+                  logger.debug ('debug called')
+                  logger.trace ('trace called')
               }
           }
           new MyClass().loggingMethod() ''')
@@ -258,18 +282,49 @@ class Slf4jTest extends GroovyTestCase {
         int ind = 0
         assert events.size() == 5
         assert events[ind].level == Level.ERROR
-        assert events[ind].message == "error called"
+        assert events[ind].message == 'error called'
         assert events[++ind].level == Level.WARN
-        assert events[ind].message == "warn called"
+        assert events[ind].message == 'warn called'
         assert events[++ind].level == Level.INFO
-        assert events[ind].message == "info called"
+        assert events[ind].message == 'info called'
         assert events[++ind].level == Level.DEBUG
-        assert events[ind].message == "debug called"
+        assert events[ind].message == 'debug called'
         assert events[++ind].level == Level.TRACE
-        assert events[ind].message == "trace called"
+        assert events[ind].message == 'trace called'
     }
 
-    void testLogTransformInteractionWithAIC_groovy6834() {
+    @Test // GROOVY-6373
+    void testLogWithInnerClasses() {
+        Class clazz = new GroovyClassLoader().parseClass('''
+            @groovy.util.logging.Slf4j('logger')
+            class MyClass {
+                def loggingMethod() {
+                    logger.info('outer called')
+                }
+                static class MyInnerClass {
+                    def loggingMethod() {
+                        logger.info('inner called')
+                    }
+                }
+            }
+            new MyClass().loggingMethod()
+            new MyClass.MyInnerClass().loggingMethod()
+        ''')
+
+        Script s = (Script) clazz.newInstance()
+        s.run()
+
+        def events = appender.events
+        int ind = 0
+        assert events.size() == 2
+        assert events[ind].level == Level.INFO
+        assert events[ind].message == 'outer called'
+        assert events[++ind].level == Level.INFO
+        assert events[ind].message == 'inner called'
+    }
+
+    @Test // GROOVY-6834
+    void testLogTransformInteractionWithAnonInnerClass() {
         assertScript '''
             @groovy.util.logging.Slf4j
             class MyClass {
@@ -289,35 +344,30 @@ class Slf4jTest extends GroovyTestCase {
         '''
     }
 
-    void testLogWithInnerClasses_groovy6373() {
+    @Test // GROOVY-6873
+    void testLogTransformInteractionWithAnonInnerClass2() {
         Class clazz = new GroovyClassLoader().parseClass('''
-            @groovy.util.logging.Slf4j('logger')
-            class MyClass {
-                def loggingMethod() {
-                    logger.info  ("outer called")
-                }
-                static class MyInnerClass {
-                    def loggingMethod() {
-                        logger.info  ("inner called")
+            @groovy.util.logging.Slf4j
+            class Channel {
+                private void someMethod(String folder)  {
+                    final includeHidden = false
+                    new Runnable() {
+                        @Override
+                        public void run() {
+                            if (includeHidden) {
+                            }
+                        }
                     }
                 }
+
+                void otherMethod() {
+                    def folder // "The current scope already contains a variable of the name
folder"
+                }
             }
-            new MyClass().loggingMethod()
-            new MyClass.MyInnerClass().loggingMethod()
         ''')
-
-        Script s = (Script) clazz.newInstance()
-        s.run()
-
-        def events = appender.getEvents()
-        int ind = 0
-        assert events.size() == 2
-        assert events[ind].level == Level.INFO
-        assert events[ind].message == "outer called"
-        assert events[++ind].level == Level.INFO
-        assert events[ind].message == "inner called"
     }
 
+    @Test
     void testLogGuard() {
         Class clazz = new GroovyClassLoader().parseClass('''
            @groovy.util.logging.Slf4j
@@ -329,90 +379,52 @@ class Slf4jTest extends GroovyTestCase {
                     return isSet
                 }
             }
-            new MyClass().loggingMethod() ''')
+            new MyClass().loggingMethod()
+        ''')
 
         Script s = (Script) clazz.newInstance()
         assert s.run() == false
     }
 
+    @Test
     void testDefaultCategory() {
-        Class clazz = new GroovyClassLoader().parseClass("""
+        Class clazz = new GroovyClassLoader().parseClass('''
             @groovy.util.logging.Slf4j
             class MyClass {
                 static loggingMethod() {
-                  log.info("info called")
+                  log.info('info called')
                 }
-            }""")
+            }
+        ''')
 
         def s = clazz.newInstance()
         s.loggingMethod()
 
-        assert appender.getEvents().size() == 1
-    }
-
-    void testGroovy6873Regression() {
-        Class clazz = new GroovyClassLoader().parseClass("""
-            @groovy.util.logging.Slf4j
-            class Channel {
-
-                private void someMethod(String folder)  {
-                  final includeHidden = false
-                   new Runnable() {
-
-                            @Override
-                            public void run() {
-                                if (includeHidden) {
-                                }
-                            }
-
-                        }
-                }
-
-                void otherMethod() {
-                    def folder
-                }
-            }""")
+        assert appender.events.size() == 1
     }
 
+    @Test
     void testCustomCategory() {
         LogbackInterceptingAppender appenderForCustomCategory = new LogbackInterceptingAppender()
-        appenderForCustomCategory.setOutputStream(new ByteArrayOutputStream())
-        appenderForCustomCategory.setLayout(new EchoLayout())
+        appenderForCustomCategory.outputStream = new ByteArrayOutputStream()
+        appenderForCustomCategory.layout = new EchoLayout()
         appenderForCustomCategory.start()
 
-        Logger loggerForCustomCategory = LoggerFactory.getLogger("customCategory")
+        Logger loggerForCustomCategory = LoggerFactory.getLogger('customCategory')
         loggerForCustomCategory.addAppender(appenderForCustomCategory)
 
-        Class clazz = new GroovyClassLoader().parseClass("""
+        Class clazz = new GroovyClassLoader().parseClass('''
             @groovy.util.logging.Slf4j(category='customCategory')
             class MyClass {
                 static loggingMethod() {
-                  log.error("error called")
+                  log.error('error called')
                 }
-            }""")
+            }
+        ''')
         def s = clazz.newInstance()
-
         s.loggingMethod()
 
-        assert appenderForCustomCategory.getEvents().size() == 1
-        assert appender.getEvents().size() == 0
-    }
-}
-
-class LogbackInterceptingAppender<E> extends OutputStreamAppender<E> {
-
-    private List<LoggingEvent> events = new ArrayList<LoggingEvent>()
-
-    List<LoggingEvent> getEvents() {
-        return events
-    }
-
-    protected void append(E event) {
-        if (event instanceof LoggingEvent) {
-            events.add(event)
-        } else {
-            throw new RuntimeException("Unable to intercept logging events - probably API
has changed")
-        }
-        super.append(event)
+        assert appenderForCustomCategory.events.size() == 1
+        assert appender.events.isEmpty()
     }
 }


Mime
View raw message