<commit_before>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2002
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.component.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import junit.framework.TestCase;

import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;

import si.ijs.maci.Client;

import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
import alma.acs.concurrent.DaemonThreadFactory;
import alma.acs.container.AcsManagerProxy;
import alma.acs.container.CleaningDaemonThreadFactory;
import alma.acs.container.ContainerServices;
import alma.acs.container.ContainerServicesImpl;
import alma.acs.container.corba.AcsCorba;
import alma.acs.logging.ClientLogManager;
import alma.acs.logging.engine.LogReceiver;
import alma.acs.util.ACSPorts;

/**
 * Base class for writing JUnit test clients for ACS components. 
 * Takes care of the communication with the ACS manager (local or provided in property <code>ACS.manager</code>).
 *  
 * Provides the {@link ContainerServices}.
 * 
 * @author hsommer Nov 21, 2002 5:53:05 PM
 */
public class ComponentClientTestCase extends TestCase
{
	protected AcsCorba acsCorba;
	
	private ContainerServicesImpl m_containerServices;
    private CleaningDaemonThreadFactory m_threadFactory;
	
	/**
	 * Special tests that need to call directly the manager API could use this proxy object.
	 * To be used sparingly, as we need to exercise (and extend if necessary) the regular 
	 * classes such as ContainerServices.  
	 */
	protected AcsManagerProxy m_acsManagerProxy;
	
	protected Logger m_logger;
	private LogReceiver logReceiver;

	/** from property ACS.manager, or defaults to localhost */
	protected String m_managerLoc;

	// manager client object 
	private ManagerClient managerClientImpl;
	private Client m_managerClient;

	private String m_namePrefix;


	/**
	 * Subclasses must call this ctor.
	 * @param name  the name used for the test case, and to talk with the ACS manager
	 * @throws Exception
	 */
	public ComponentClientTestCase(String name) throws Exception
	{
		super(name);
		m_namePrefix = name;
	}
	
	
	/**
	 * Executes a single test method. 
	 * Stray exceptions are logged using the test logger, so that they show in system logs.
	 * @see junit.framework.TestCase#runTest()
	 * @since ACS 6.0
	 */
	protected void runTest() throws Throwable {
		try {
			super.runTest();			
		}
		catch (Throwable thr) {
			if (m_logger != null) {
				m_logger.log(Level.WARNING, "JUnit test error in " + getFullName(), thr);
			}
			throw thr;
		}
	}
	
	/**
	 * Starts CORBA in the client process and connects to the manager and logger.
     * <p>
     * <b>Subclasses that override this method must call <code>super.setUp()</code>,
     * likely before any other code in that method.</b>
	 *  
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception
	{
		m_logger = ClientLogManager.getAcsLogManager().getLoggerForApplication(getFullName(), true);

		m_logger.info("-------------------------------");
		m_logger.info("ComponentClientTestCase#setUp()");

		POA rootPOA = null;
		try
		{
    		acsCorba = new AcsCorba(m_logger);
    		rootPOA = acsCorba.initCorbaForClient(false);

			connectToManager();

			m_threadFactory = new CleaningDaemonThreadFactory(m_namePrefix, m_logger);
			// @TODO: harmonize usage of Logger and AcsLogger and then subclass the containerservices
			// so that getLogger() returns m_logger and not a new component logger.
			m_containerServices = new ContainerServicesImpl(m_acsManagerProxy, rootPOA, acsCorba,
										m_logger, m_acsManagerProxy.getManagerHandle(), 
										this.getClass().getName(), null, m_threadFactory);
			managerClientImpl.setContainerServices(m_containerServices);
			initRemoteLogging();
		}
		catch (Exception ex1)
		{
			m_logger.log(Level.SEVERE, "failed to initialize the ORB, or connect to the ACS Manager, " + 
										"or to set up the container services.", ex1);
            if (acsCorba != null) {
            	try {
	            	acsCorba.shutdownORB(true, false);
	            	acsCorba.doneCorba();
            	} catch (Exception ex2) {
            		// to JUnit we want to forward the original exception ex1, 
            		// not any other exception from cleaning up the orb 
            		// which we would not have done without the first exception.
            		// Thus we simply print ex2 but let ex1 fly
            		ex2.printStackTrace();
            	}
            }
			throw ex1;
		}
	}


	/**
	 * Connects to the ACS Manager using {@link AcsManagerProxy}.
	 * @throws Exception
	 */
	protected void connectToManager() throws Exception
	{<commit_msg>Internally uses an AcsLogger instead of plain Logger, which allows this base class to return this same AcsLogger in getContainerServices().getLogger() instead of using a separate AcsLogger there.<commit_after>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2002
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.component.client;

import java.util.logging.Level;

import junit.framework.TestCase;

import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;

import si.ijs.maci.Client;

import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
import alma.acs.concurrent.DaemonThreadFactory;
import alma.acs.container.AcsManagerProxy;
import alma.acs.container.CleaningDaemonThreadFactory;
import alma.acs.container.ContainerServices;
import alma.acs.container.ContainerServicesImpl;
import alma.acs.container.corba.AcsCorba;
import alma.acs.logging.AcsLogger;
import alma.acs.logging.ClientLogManager;
import alma.acs.logging.engine.LogReceiver;
import alma.acs.util.ACSPorts;

/**
 * Base class for writing JUnit test clients for ACS components. 
 * Takes care of the communication with the ACS manager (local or provided in property <code>ACS.manager</code>).
 *  
 * Provides the {@link ContainerServices}.
 * 
 * @author hsommer Nov 21, 2002 5:53:05 PM
 */
public class ComponentClientTestCase extends TestCase
{
	protected AcsCorba acsCorba;
	
	private ContainerServicesImpl m_containerServices;
    private CleaningDaemonThreadFactory m_threadFactory;
	
	/**
	 * Special tests that need to call directly the manager API could use this proxy object.
	 * To be used sparingly, as we need to exercise (and extend if necessary) the regular 
	 * classes such as ContainerServices.  
	 */
	protected AcsManagerProxy m_acsManagerProxy;
	
	protected AcsLogger m_logger;
	private LogReceiver logReceiver;

	/** from property ACS.manager, or defaults to localhost */
	protected String m_managerLoc;

	// manager client object 
	private ManagerClient managerClientImpl;
	private Client m_managerClient;

	private String m_namePrefix;


	/**
	 * Subclasses must call this ctor.
	 * @param name  the name used for the test case, and to talk with the ACS manager
	 * @throws Exception
	 */
	public ComponentClientTestCase(String name) throws Exception
	{
		super(name);
		m_namePrefix = name;
	}
	
	
	/**
	 * Executes a single test method. 
	 * Stray exceptions are logged using the test logger, so that they show in system logs.
	 * @see junit.framework.TestCase#runTest()
	 * @since ACS 6.0
	 */
	protected void runTest() throws Throwable {
		try {
			super.runTest();			
		}
		catch (Throwable thr) {
			if (m_logger != null) {
				m_logger.log(Level.WARNING, "JUnit test error in " + getFullName(), thr);
			}
			throw thr;
		}
	}
	
	/**
	 * Starts CORBA in the client process and connects to the manager and logger.
     * <p>
     * <b>Subclasses that override this method must call <code>super.setUp()</code>,
     * likely before any other code in that method.</b>
	 *  
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception
	{
		m_logger = ClientLogManager.getAcsLogManager().getLoggerForApplication(getFullName(), true);

		m_logger.info("-------------------------------");
		m_logger.info("ComponentClientTestCase#setUp()");

		POA rootPOA = null;
		try
		{
			acsCorba = new AcsCorba(m_logger);
			rootPOA = acsCorba.initCorbaForClient(false);

			connectToManager();

			m_threadFactory = new CleaningDaemonThreadFactory(m_namePrefix, m_logger);
			m_containerServices = new ContainerServicesImpl(m_acsManagerProxy, rootPOA, acsCorba,
										m_logger, m_acsManagerProxy.getManagerHandle(), 
										this.getClass().getName(), null, m_threadFactory) {
				public AcsLogger getLogger() {
					return m_logger;
				}
			};
			managerClientImpl.setContainerServices(m_containerServices);
			initRemoteLogging();
		}
		catch (Exception ex1)
		{
			m_logger.log(Level.SEVERE, "failed to initialize the ORB, or connect to the ACS Manager, " + 
										"or to set up the container services.", ex1);
            if (acsCorba != null) {
            	try {
	            	acsCorba.shutdownORB(true, false);
	            	acsCorba.doneCorba();
            	} catch (Exception ex2) {
            		// to JUnit we want to forward the original exception ex1, 
            		// not any other exception from cleaning up the orb 
            		// which we would not have done without the first exception.
            		// Thus we simply print ex2 but let ex1 fly
            		ex2.printStackTrace();
            	}
            }
			throw ex1;
		}
	}


	/**
	 * Connects to the ACS Manager using {@link AcsManagerProxy}.
	 * @throws Exception
	 */
	protected void connectToManager() throws Exception
	{
{'fraction_non_alphanumeric': 0.08682017740345847, 'fraction_numerical': 0.007347011916494938, 'mean_word_length': 3.2700841622035197, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6893088', 'n_tokens_mistral': 3480, 'n_tokens_neox': 3145, 'n_words': 1102}