/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io
 *
 * This program 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 3 of the License, or (at your option) any later version.
 * This program 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exoplatform.container.test;

import junit.framework.TestCase;

import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.RootContainer;
import org.exoplatform.container.component.ComponentPlugin;
import org.exoplatform.mocks.MockService;
import org.exoplatform.mocks.PriorityService;
import org.exoplatform.services.log.ExoLogger;

import java.util.List;

/**
 * Created by the Exo Development team.<br>
 * Author : Mestrallet Benjamin benjamin.mestrallet@exoplatform.com
 * @version $Id: TestContainer.java 34394 2009-07-23 09:23:31Z dkatayev $
 */
public class TestContainer extends TestCase
{
   private static final org.exoplatform.services.log.Log LOG = ExoLogger.getLogger("exo.kernel.container.TestContainer");
   
   
   public void setUp() throws Exception
   {
      LOG.info("Start SetUp");
   
      System.setProperty("maven.exoplatform.dir", TestContainer.class.getResource("/").getFile());
      ExoContainerContext.setCurrentContainer(null);
      PortalContainer.setInstance(null);
      // FIXED: Don't call getTopContainer() which lazily creates a RootContainer.
      // Instead stop the current singleton if one exists, which will clear topContainer.
      ExoContainer current = ExoContainerContext.getCurrentContainerIfPresent();
      if (current != null) {
         current.stop();
      }
      RootContainer.setInstance(null);
      LOG.info("End SetUp");
   
   }

   public void testComponent() throws Exception
   {
      LOG.info("Start testComponent");
      RootContainer rootContainer = RootContainer.getInstance();
      MockService mservice = (MockService)rootContainer.getComponentInstance("MockService");
      assertTrue(mservice != null);
      assertTrue(mservice.getPlugins().size() == 2);
      LOG.info("End testComponent");
   }

   public void testComponent2() throws Exception
   {
      LOG.info("Start testComponent2");
      RootContainer rootContainer = RootContainer.getInstance();
      PortalContainer pcontainer = rootContainer.getPortalContainer("portal");
      assertNotNull(pcontainer);
      MultibleComponent c = (MultibleComponent)pcontainer.getComponentInstanceOfType(MultibleComponent.class);
      assertNotNull(c);
      c = (MultibleComponent)pcontainer.getComponentInstanceOfType(MultibleComponent.class);
      assertNotNull(c);
      c = (MultibleComponent)pcontainer.getComponentInstanceOfType(MultibleComponent.class);
      assertNotNull(c);
      LOG.info("End testComponent2");
   }

   public void testComponent3() throws Exception
   {
      LOG.info("Start testComponent3");
      RootContainer rootContainer = RootContainer.getInstance();
      PortalContainer pcontainer = rootContainer.getPortalContainer("portal");
      assertNotNull(pcontainer);
      DefaultComponent c = (DefaultComponent)pcontainer.getComponentInstanceOfType(DefaultComponent.class);
      assertNotNull(c);
      c = (DefaultComponent)pcontainer.getComponentInstanceOfType(DefaultComponent.class);
      assertNotNull(c);
      c = (DefaultComponent)pcontainer.getComponentInstanceOfType(DefaultComponent.class);
      assertNotNull(c);
      LOG.info("End testComponent3");
   }

   public void testPriorityPlugins()
   {
      LOG.info("Start testPriorityPlugins");
   
      RootContainer rootContainer = RootContainer.getInstance();
      PortalContainer pcontainer = rootContainer.getPortalContainer("portal");
      assertNotNull(pcontainer);
      PriorityService ps = (PriorityService)pcontainer.getComponentInstanceOfType(PriorityService.class);
      assertNotNull(ps);
      List<ComponentPlugin> l = ps.getPlugins();
      assertNotNull(l);
      assertEquals(3, l.size());
      assertEquals("PluginPriority3", l.get(0).getName());
      assertEquals("PluginPriority1", l.get(1).getName());
      assertEquals("PluginPriority2", l.get(2).getName());
      LOG.info("End testPriorityPlugins");
   
   }
   
   public void testPortalContainer() throws Exception
   {
      LOG.info("Start testPortalContainer");
   
      RootContainer rootContainer = RootContainer.getInstance();
      PortalContainer pcontainer = rootContainer.getPortalContainer("portal");
      Object parent = pcontainer.getParent();
      assertTrue("Root container should not be null", parent != null);
      // performance test

      int INSERTLOOP = 0;
      for (int i = 0; i < INSERTLOOP; i++)
      {
         rootContainer.getPortalContainer("name-" + Integer.toString(i));
      }

      int LOOP = 10000000;
      for (int i = 0; i < LOOP; i++)
      {
         pcontainer = (PortalContainer)rootContainer.getComponentInstance("portal");
         assertTrue("not null", pcontainer != null);
      }
      LOG.info("End testPortalContainer");
   
   }
}
