/*
 * The baseCode project
 * 
 * Copyright (c) 2006 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.basecode.util;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ubic.basecode.dataStructure.matrix.DoubleMatrixNamed;
import ubic.basecode.io.reader.DoubleMatrixReader;

/**
 * @author pavlidis
 * @version $Id$
 */
public class RServeClientTest extends TestCase {
    private static Log log = LogFactory.getLog( RServeClientTest.class.getName() );

    RServeClient rc = null;
    boolean connected = false;
    DoubleMatrixNamed tester;

    public void setUp() throws Exception {
        try {
            rc = new RServeClient();
            connected = rc != null;
        } catch ( RuntimeException e ) {
            connected = false;
        }

        DoubleMatrixReader reader = new DoubleMatrixReader();

        tester = ( DoubleMatrixNamed ) reader.read( this.getClass().getResourceAsStream( "/data/testdata.txt" ) );
    }

    public void tearDown() throws Exception {
        if ( rc != null ) {
            rc.disconnect();
            rc.stopServer();
        }
        tester = null;
    }

    /*
     * Test method for ' RCommand.exec(String)'
     */
    public void testExec() throws Exception {
        if ( !connected ) {
            log.warn( "Could not connect to RServe, skipping test." );
            return;
        }
        String actualValue = rc.eval( "R.version.string" ).asString();
        String expectedValue = "R version 2";

        assertTrue( "rc.eval() return version " + actualValue + ", expected something starting with R version 2",
                actualValue.startsWith( expectedValue ) );
    }

    /*
     * Test method for ' RCommand.exec(String)'
     */
    public void testExecDoubleArray() throws Exception {
        if ( !connected ) {
            log.warn( "Could not connect to RServe, skipping test." );
            return;
        }
        assertTrue( RegressionTesting.closeEnough( new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, rc
                .doubleArrayEval( "rep(1, 10)" ), 0.001 ) );
    }

    public void testAssignAndRetrieveMatrix() throws Exception {
        if ( !connected ) {
            log.warn( "Could not connect to RServe, skipping test." );
            return;
        }
        DoubleMatrixNamed result = rc.retrieveMatrix( rc.assignMatrix( tester ) );
        assertTrue( RegressionTesting.closeEnough( tester, result, 0.0001 ) );
    }
}
@@ -674,16 +674,68 @@
 .util;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import j
@@ -1790,39 +1790,591 @@
  = null;%0A    %7D%0A%0A
+    public void testAssignStringList() throws Exception %7B%0A        if ( !connected ) %7B%0A            log.warn( %22Could not connect to RServe, skipping test.%22 );%0A            return;%0A        %7D%0A%0A        List%3CString%3E l = new ArrayList%3CString%3E();%0A        l.add( %22foo%22 );%0A        l.add( %22bar%22 );%0A%0A        String varname = rc.assignStringList( l );%0A        String actualValue = rc.stringEval( varname + %22%5B1%5D%22 );%0A        assertEquals( %22foo%22, actualValue );%0A        actualValue = rc.stringEval( varname + %22%5B2%5D%22 );%0A        assertEquals( %22bar%22, actualValue );%0A    %7D%0A%0A
     /*%0A
-
      * Test meth
@@ -2620,17 +2620,23 @@
 ue = rc.
-e
+stringE
 val( %22R.
@@ -2655,19 +2655,8 @@
 ng%22 
-).asString(
 );%0A 

add test for assignStringList