<commit_before>/*******************************************************************************
 * Copyright (c) 2015 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.common.tests;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import de.dentrassi.pm.common.lm.LockContext;
import de.dentrassi.pm.common.lm.LockManager;

public class MultiLockManagerTest
{
    private final static Logger logger = LoggerFactory.getLogger ( MultiLockManagerTest.class );

    private static AtomicReference<String> global1 = new AtomicReference<> ();

    private static AtomicReference<String> global2 = new AtomicReference<> ();

    private final AtomicInteger readOps = new AtomicInteger ();

    private final AtomicInteger writeOps = new AtomicInteger ();

    private int readerGroups = 0;

    private int writerGroups = 0;

    @BeforeClass
    public static void setup ()
    {
        final LoggerContext ctx = (LoggerContext)LoggerFactory.getILoggerFactory ();

        final PatternLayoutEncoder encoder = new PatternLayoutEncoder ();

        encoder.setPattern ( "%date %level [%thread] %logger{10} [%file:%line] %msg%n" );
        encoder.setContext ( ctx );
        encoder.start ();

        final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<> ();
        appender.setEncoder ( encoder );
        appender.start ();
        appender.setContext ( ctx );

        final ch.qos.logback.classic.Logger rootLogger = ctx.getLogger ( Logger.ROOT_LOGGER_NAME );
        rootLogger.detachAppender ( "console" );
        rootLogger.addAppender ( appender );
        if ( Boolean.getBoolean ( "trace" ) )
        {
            rootLogger.setLevel ( Level.TRACE );
        }
        else
        {
            rootLogger.setLevel ( Level.WARN );
        }
    }

    @Test ( timeout = 2_000 )
    public void test1 ()
    {
        MultiLockManagerTest.logger.info ( "test1" );

        try ( LockManager lm = new LockManager () )
        {
            final List<TestThread> threads = new LinkedList<> ();

            startLoopers ( "test1-", threads, 1, () -> {
                lm.run ( () -> {
                    LockContext.access ( "a" );
                    Assert.assertNull ( global1.get () );
                    sleep ( 100 );
                } );
            } );

            sleep ( 1_000 );<commit_msg>fix unit test to handle non-logback based logger system<commit_after>/*******************************************************************************
 * Copyright (c) 2015 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.common.tests;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import de.dentrassi.pm.common.lm.LockContext;
import de.dentrassi.pm.common.lm.LockManager;

public class MultiLockManagerTest
{
    private final static Logger logger = LoggerFactory.getLogger ( MultiLockManagerTest.class );

    private static AtomicReference<String> global1 = new AtomicReference<> ();

    private static AtomicReference<String> global2 = new AtomicReference<> ();

    private final AtomicInteger readOps = new AtomicInteger ();

    private final AtomicInteger writeOps = new AtomicInteger ();

    private int readerGroups = 0;

    private int writerGroups = 0;

    @BeforeClass
    public static void setup ()
    {
        final ILoggerFactory factory = LoggerFactory.getILoggerFactory ();
        if ( factory instanceof LoggerContext )
        {
            final LoggerContext ctx = (LoggerContext)factory;

            final PatternLayoutEncoder encoder = new PatternLayoutEncoder ();

            encoder.setPattern ( "%date %level [%thread] %logger{10} [%file:%line] %msg%n" );
            encoder.setContext ( ctx );
            encoder.start ();

            final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<> ();
            appender.setEncoder ( encoder );
            appender.start ();
            appender.setContext ( ctx );

            final ch.qos.logback.classic.Logger rootLogger = ctx.getLogger ( Logger.ROOT_LOGGER_NAME );
            rootLogger.detachAppender ( "console" );
            rootLogger.addAppender ( appender );
            if ( Boolean.getBoolean ( "trace" ) )
            {
                rootLogger.setLevel ( Level.TRACE );
            }
            else
            {
                rootLogger.setLevel ( Level.WARN );
            }
        }
    }

    @Test ( timeout = 2_000 )
    public void test1 ()
    {
        MultiLockManagerTest.logger.info ( "test1" );

        try ( LockManager lm = new LockManager () )
        {
            final List<TestThread> threads = new LinkedList<> ();

            startLoopers ( "test1-", threads, 1, () -> {
                lm.run ( () -> {
                    LockContext.access ( "a" );
                    Assert.assertNull ( global1.get () );
                    sleep ( 100 );
                } );
            } );

            sleep ( 1_000 );
{'fraction_non_alphanumeric': 0.14407668114422645, 'fraction_numerical': 0.00973491088812341, 'mean_word_length': 2.7453729669097027, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501847', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1743, 'n_words': 424}