<commit_before>/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 <NAME>
   Copyright (c) 2017-2019 <NAME>.

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the LICENSE file with this program.
*/

package org.freedesktop.dbus.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.reflect.Type;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusAsyncReply;
import org.freedesktop.dbus.DBusMatchRule;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.Marshalling;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnection.DBusBusType;
import org.freedesktop.dbus.errors.ServiceUnknown;
import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.interfaces.CallbackHandler;
import org.freedesktop.dbus.interfaces.Introspectable;
import org.freedesktop.dbus.interfaces.Local;
import org.freedesktop.dbus.interfaces.Peer;
import org.freedesktop.dbus.interfaces.Properties;
import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.test.helper.SampleClass;
import org.freedesktop.dbus.test.helper.SampleException;
import org.freedesktop.dbus.test.helper.SampleNewInterfaceClass;
import org.freedesktop.dbus.test.helper.SampleSerializable;
import org.freedesktop.dbus.test.helper.callbacks.handler.CallbackHandlerImpl;
import org.freedesktop.dbus.test.helper.interfaces.SampleNewInterface;
import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface;
import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2;
import org.freedesktop.dbus.test.helper.signals.SampleSignals;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestArraySignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestEmptySignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestPathSignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestRenamedSignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestSignal;
import org.freedesktop.dbus.test.helper.signals.handler.ArraySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.BadArraySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.DisconnectHandler;
import org.freedesktop.dbus.test.helper.signals.handler.EmptySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.GenericHandlerWithDecode;
import org.freedesktop.dbus.test.helper.signals.handler.GenericSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.ObjectSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.PathSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.RenamedSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.SignalHandler;
import org.freedesktop.dbus.test.helper.structs.IntStruct;
import org.freedesktop.dbus.test.helper.structs.SampleStruct;
import org.freedesktop.dbus.test.helper.structs.SampleStruct2;
import org.freedesktop.dbus.test.helper.structs.SampleStruct3;
import org.freedesktop.dbus.test.helper.structs.SampleStruct4;
import org.freedesktop.dbus.test.helper.structs.SampleTuple;
import org.freedesktop.dbus.types.UInt16;
import org.freedesktop.dbus.types.UInt32;
import org.freedesktop.dbus.types.UInt64;
import org.freedesktop.dbus.types.Variant;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.hypfvieh.util.TimeMeasure;

/**
 * This is a test program which sends and recieves a signal, implements, exports and calls a remote method.
 */
// CHECKSTYLE:OFF
public class TestAll {

    public static final String TEST_OBJECT_PATH = "/TestAll";

    // CHECKSTYLE:OFF
    private DBusConnection serverconn = null;
    private DBusConnection clientconn = null;
    private SampleClass tclass;
    // CHECKSTYLE:ON

    @BeforeEach
    public void setUp() throws DBusException {
        serverconn = DBusConnection.getConnection(DBusBusType.SESSION);
        clientconn = DBusConnection.getConnection(DBusBusType.SESSION);
        serverconn.setWeakReferences(true);
        clientconn.setWeakReferences(true);
        serverconn.requestBusName("foo.bar.Test");

        tclass = new SampleClass(serverconn);

        /** This exports an instance of the test class as the object /Test. */
        serverconn.exportObject(TEST_OBJECT_PATH, tclass);
        serverconn.addFallback("/FallbackTest", tclass);
    }

    @AfterEach
    public void tearDown() {
        System.out.println("Checking for outstanding errors");
        DBusExecutionException dbee = serverconn.getError();
        if (null != dbee) {
            throw dbee;
        }
        dbee = clientconn.getError();
        if (null != dbee) {
            throw dbee;
        }

        System.out.println("Disconnecting");
        /** Disconnect from the bus. */
        clientconn.disconnect();
        serverconn.disconnect();
    }

    @Test
    public void testSignalHandlers() throws DBusException, InterruptedException {
        SignalHandler sigh = new SignalHandler(1, new UInt32(42), "Bar");
        RenamedSignalHandler rsh = new RenamedSignalHandler(1, new UInt32(42), "Bar");
        EmptySignalHandler esh = new EmptySignalHandler(1);

        ArraySignalHandler ash = new ArraySignalHandler(1);

        /** This gets a remote object matching our bus name and exported object path. */
        Peer peer = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, Peer.class);

        DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class);

        System.out.print("Listening for signals...");
        /** This registers an instance of the test class as the signal handler for the TestSignal class. */

        clientconn.addSigHandler(SampleSignals.TestEmptySignal.class, esh);
        clientconn.addSigHandler(SampleSignals.TestSignal.class, sigh);
        clientconn.addSigHandler(SampleSignals.TestRenamedSignal.class, rsh);

        clientconn.addSigHandler(Local.Disconnected.class, new DisconnectHandler(clientconn, rsh));

        String source = dbus.GetNameOwner("foo.bar.Test");<commit_msg>Improved tests: TestAll now passing<commit_after>/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 <NAME>
   Copyright (c) 2017-2019 <NAME>.

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the LICENSE file with this program.
*/

package org.freedesktop.dbus.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.reflect.Type;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusAsyncReply;
import org.freedesktop.dbus.DBusMatchRule;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.Marshalling;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnection.DBusBusType;
import org.freedesktop.dbus.errors.ServiceUnknown;
import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.interfaces.CallbackHandler;
import org.freedesktop.dbus.interfaces.Introspectable;
import org.freedesktop.dbus.interfaces.Local;
import org.freedesktop.dbus.interfaces.Peer;
import org.freedesktop.dbus.interfaces.Properties;
import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.test.helper.SampleClass;
import org.freedesktop.dbus.test.helper.SampleException;
import org.freedesktop.dbus.test.helper.SampleNewInterfaceClass;
import org.freedesktop.dbus.test.helper.SampleSerializable;
import org.freedesktop.dbus.test.helper.callbacks.handler.CallbackHandlerImpl;
import org.freedesktop.dbus.test.helper.interfaces.SampleNewInterface;
import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface;
import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2;
import org.freedesktop.dbus.test.helper.signals.SampleSignals;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestArraySignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestEmptySignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestPathSignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestRenamedSignal;
import org.freedesktop.dbus.test.helper.signals.SampleSignals.TestSignal;
import org.freedesktop.dbus.test.helper.signals.handler.ArraySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.BadArraySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.DisconnectHandler;
import org.freedesktop.dbus.test.helper.signals.handler.EmptySignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.GenericHandlerWithDecode;
import org.freedesktop.dbus.test.helper.signals.handler.GenericSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.ObjectSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.PathSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.RenamedSignalHandler;
import org.freedesktop.dbus.test.helper.signals.handler.SignalHandler;
import org.freedesktop.dbus.test.helper.structs.IntStruct;
import org.freedesktop.dbus.test.helper.structs.SampleStruct;
import org.freedesktop.dbus.test.helper.structs.SampleStruct2;
import org.freedesktop.dbus.test.helper.structs.SampleStruct3;
import org.freedesktop.dbus.test.helper.structs.SampleStruct4;
import org.freedesktop.dbus.test.helper.structs.SampleTuple;
import org.freedesktop.dbus.types.UInt16;
import org.freedesktop.dbus.types.UInt32;
import org.freedesktop.dbus.types.UInt64;
import org.freedesktop.dbus.types.Variant;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.hypfvieh.util.TimeMeasure;

/**
 * This is a test program which sends and recieves a signal, implements, exports and calls a remote method.
 */
// CHECKSTYLE:OFF
public class TestAll {

    public static final String TEST_OBJECT_PATH = "/TestAll";

    // CHECKSTYLE:OFF
    private DBusConnection serverconn = null;
    private DBusConnection clientconn = null;
    private SampleClass tclass;
    // CHECKSTYLE:ON

    @BeforeEach
    public void setUp() throws DBusException {
        serverconn = DBusConnection.getConnection(DBusBusType.SESSION);
        clientconn = DBusConnection.getConnection(DBusBusType.SESSION);
        serverconn.setWeakReferences(true);
        clientconn.setWeakReferences(true);
        serverconn.requestBusName("foo.bar.Test");

        tclass = new SampleClass(serverconn);

        /** This exports an instance of the test class as the object /Test. */
        serverconn.exportObject(TEST_OBJECT_PATH, tclass);
        serverconn.addFallback("/FallbackTest", tclass);
    }

    @AfterEach
    public void tearDown() throws Exception {
        System.out.println("Checking for outstanding errors");
        DBusExecutionException dbee = serverconn.getError();
        if (null != dbee) {
            throw dbee;
        }
        dbee = clientconn.getError();
        if (null != dbee) {
            throw dbee;
        }

        System.out.println("Disconnecting");
        /** Disconnect from the bus. */
        clientconn.disconnect();
        serverconn.releaseBusName("foo.bar.Test");
        serverconn.disconnect();
    }

    @Test
    public void testSignalHandlers() throws DBusException, InterruptedException {
        SignalHandler sigh = new SignalHandler(1, new UInt32(42), "Bar");
        RenamedSignalHandler rsh = new RenamedSignalHandler(1, new UInt32(42), "Bar");
        EmptySignalHandler esh = new EmptySignalHandler(1);

        ArraySignalHandler ash = new ArraySignalHandler(1);

        /** This gets a remote object matching our bus name and exported object path. */
        Peer peer = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, Peer.class);

        DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class);

        System.out.print("Listening for signals...");
        /** This registers an instance of the test class as the signal handler for the TestSignal class. */

        clientconn.addSigHandler(SampleSignals.TestEmptySignal.class, esh);
        clientconn.addSigHandler(SampleSignals.TestSignal.class, sigh);
        clientconn.addSigHandler(SampleSignals.TestRenamedSignal.class, rsh);

        clientconn.addSigHandler(Local.Disconnected.class, new DisconnectHandler(clientconn, rsh));

        String source = dbus.GetNameOwner("foo.bar.Test");
{'fraction_non_alphanumeric': 0.0997440636997014, 'fraction_numerical': 0.005829660173467937, 'mean_word_length': 4.597692001591723, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '373288', 'n_tokens_mistral': 4474, 'n_tokens_neox': 4242, 'n_words': 780}