package com.k_int.lom;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.*;
import junit.extensions.*;
import java.util.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.k_int.svc.refdata.datamodel.*;
    
/**
 * Unit test for simple App.
 */
public class LomDatamodelTest extends TestCase {

  private static ApplicationContext app_context = null;
  public static Log log = LogFactory.getLog(LomDatamodelTest.class);
    
  public static void main(String[] args) {
  }
    
  public LomDatamodelTest(String name) {
    super (name);
    app_context = new ClassPathXmlApplicationContext( new String[] { "TestApplicationContext.xml" } );
    if ( app_context == null )
      throw new RuntimeException("Unable to locate TestApplicationContext.xml definition file");
    log.debug("Got context");
  } 

  protected void setUp() {
    log.debug("Setup");
  } 
    
  protected void tearDown() {
    log.debug("Shutdown");
  } 
  

  /**
   * @return the suite of tests being tested
   */
  public static Test suite() {
    return new TestSuite( LomDatamodelTest.class );
  }

  /**
   * Rigourous Test :-)
   */
  public void testApp() throws org.hibernate.HibernateException, java.sql.SQLException {
    org.hibernate.SessionFactory sf = (org.hibernate.SessionFactory) app_context.getBean("UnitTestSessionFactory");

    org.hibernate.Session sess=null;
    try {
      sess=sf.openSession();

      // com.k_int.lom.service.upload.LOMRecordUpload upload_service = (com.k_int.lom.service.upload.LOMRecordUpload) app_context.getBean("RecordUploadService");

      // Import some vocabs
 
      // Import a lom document and see it's all properly indexed
      // upload(upload_service,"file1","user","pass","format","version","ret");

      sess.flush(); 
      sess.connection().commit();
    }
    finally { 
      if ( sess != null ) {
        try {
          sess.close();
        }
        catch ( Exception e ) {
          e.printStackTrace();
        }
      }
    }
  }

  // public static UploadResultDTO upload(com.k_int.lom.service.upload.LOMRecordUpload upload_service,
  //                                      String filename,
  //                                      String id,
  //                                      String pass,
  //                                      String format,
  //                                      String version,
  //                                      String ret) throws java.io.IOException {

  //   java.net.URL lom_url = LomDatamodelTest.class.getResource(filename);
  //   java.io.InputStream is = lom_url.openStream();
  //   java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
  //   int byte_read = is.read();
  //   while ( byte_read != -1 ) {
  //     baos.write(byte_read);
  //     byte_read = is.read();
  //   }

  //   byte[] lom = baos.toByteArray();

    // com.k_int.lom.service.upload.UploadRequestDTO upload_req = new com.k_int.lom.service.upload.UploadRequestDTO(lom,id,pass,format,version,ret);
    // UploadResultDTO result = upload_service.upload(upload_req);
    // System.err.println(result);

  //   return result;
  // }
}
