/*
 * The Gemma project
 * 
 * Copyright (c) 2007 University of British Columbia
 * 
 * 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 ubic.gemma.util.javaspaces.gigaspaces;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;

import ubic.gemma.javaspaces.gigaspaces.GemmaSpacesEnum;
import ubic.gemma.testing.BaseSpringContextTest;
import ubic.gemma.util.SpringContextUtil;

/**
 * @author keshav
 * @version $Id$
 */
public class GigaspacesUtilTest extends BaseSpringContextTest {

    /**
     * Tests if space is running.
     */
    public void testIsSpaceRunning() {

        boolean isRunning = GigaspacesUtil.isSpaceRunning( GemmaSpacesEnum.DEFAULT_SPACE.getSpaceUrl() );

        if ( isRunning )
            assertTrue( isRunning );

        else
            assertFalse( isRunning );

    }

    /**
     * Tests gigaspaces beans to the {@link org.springframework.beans.factory.BeanFactory}
     */
    public void testAddGigaspacesToBeanFactory() {

        ApplicationContext withoutGigaspacesCtx = ( ApplicationContext ) SpringContextUtil.getApplicationContext( true,
                false, false, false );

        assertFalse( withoutGigaspacesCtx.containsBean( "gigaspacesTemplate" ) );

        GigaspacesUtil gigaspacesUtil = new GigaspacesUtil();

        gigaspacesUtil.setApplicationContext( withoutGigaspacesCtx );

        BeanFactory updatedCtx = gigaspacesUtil.addGigaspacesToApplicationContext( GemmaSpacesEnum.DEFAULT_SPACE
                .getSpaceUrl() );

        /* verify that we have the new gigaspaces beans */
        assertTrue( updatedCtx.containsBean( "gigaspacesTemplate" ) );

        /* make sure we haven't lost the other beans */
        assertTrue( updatedCtx.containsBean( "sessionFactory" ) );
    }

}

@@ -1691,24 +1691,82 @@
 , false );%0A%0A
+        String gigaspacesTemplate = %22gigaspacesTemplate%22;%0A
         asse
@@ -1801,33 +1801,32 @@
 x.containsBean( 
-%22
 gigaspacesTempla
@@ -1819,33 +1819,32 @@
 gaspacesTemplate
-%22
  ) );%0A%0A        G
@@ -2167,32 +2167,218 @@
 spaces beans */%0A
+        if ( !GigaspacesUtil.isSpaceRunning( GemmaSpacesEnum.DEFAULT_SPACE.getSpaceUrl() ) )%0A            assertFalse( updatedCtx.containsBean( gigaspacesTemplate ) );%0A        else %7B%0A    
         assertTr
@@ -2406,17 +2406,16 @@
 nsBean( 
-%22
 gigaspac
@@ -2424,23 +2424,31 @@
 Template
-%22
  ) );%0A
+        %7D
 %0A       

assertFalse if space is not running