/*
 * Copyright 2010 Toni Menzel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ops4j.pax.exam.spi.driversupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.TestContainer;
import org.ops4j.pax.exam.TestContainerFactory;
import org.ops4j.pax.exam.TestProbeProvider;
import org.ops4j.pax.exam.spi.ExxamReactor;
import org.ops4j.pax.exam.spi.StagedExamReactor;
import org.ops4j.pax.exam.spi.StagedExamReactorFactory;

/**
 * Reactor decouples {@link org.ops4j.pax.exam.TestContainer} state from the observer. It is also
 * in control to map probes to their configurations or vice versa. In essence,
 * this implements the Container re-start/re-use policy topic by collecting relevant tests and configurations and passing them to a (user selected factory (see stage()).
 *
 * @author tonit
 */
public class DefaultExamReactor implements ExxamReactor {

    private static Logger LOG = LoggerFactory.getLogger( DefaultExamReactor.class );

    final private List<Option[]> m_configurations;
    final private List<TestProbeProvider> m_probes;
    final private TestContainerFactory m_factory;

    public DefaultExamReactor( TestContainerFactory factory )
    {
        m_configurations = new ArrayList<Option[]>();
        m_probes = new ArrayList<TestProbeProvider>();
        m_factory = factory;
    }

    synchronized public void addConfiguration( Option[] options )
    {
        m_configurations.add( options );
    }

    synchronized public void addProbe( TestProbeProvider addTest )
    {
        m_probes.add( addTest );
    }

    synchronized public StagedExamReactor stage( StagedExamReactorFactory factory )
    {
        LOG.info( "Staging reactor with probes: " + m_probes.size() + " using strategy: " + factory );
        List<TestContainer> containers = new ArrayList<TestContainer>();

        for( Option[] options : m_configurations ) {
            containers.addAll( Arrays.asList( m_factory.parse( options ) ) );
        }

        return factory.create( containers, m_probes );
    }

    private void printMassiveInformation()
    {
        if( LOG.isInfoEnabled() ) {
            for( TestProbeProvider p : m_probes ) {
                LOG.info( p.toString() );
            }

        }
    }

}

@@ -691,16 +691,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -1119,16 +1119,65 @@
 ctory;%0A%0A
+import static org.ops4j.pax.exam.CoreOptions.*;%0A%0A
 /**%0A * R
@@ -2551,24 +2551,197 @@
 tainer%3E();%0A%0A
+        if( m_configurations.isEmpty() ) %7B%0A            LOG.info( %22No configuration given. Setting an empty one.%22 );%0A            m_configurations.add( options() );%0A        %7D%0A
         for(
@@ -2936,217 +2936,6 @@
  %7D%0A%0A
-    private void printMassiveInformation()%0A    %7B%0A        if( LOG.isInfoEnabled() ) %7B%0A            for( TestProbeProvider p : m_probes ) %7B%0A                LOG.info( p.toString() );%0A            %7D%0A%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Allow "no otions". This means exam will inject an empty one.