You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ea...@apache.org on 2007/05/14 17:47:44 UTC

svn commit: r537889 - /incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java

Author: eae
Date: Mon May 14 08:47:43 2007
New Revision: 537889

URL: http://svn.apache.org/viewvc?view=rev&rev=537889
Log:
UIMA-359 add test case for blob serialization

Modified:
    incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java?view=diff&rev=537889&r1=537888&r2=537889
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationReinitTest.java Mon May 14 08:47:43 2007
@@ -19,6 +19,8 @@
 
 package org.apache.uima.cas.test;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -27,6 +29,9 @@
 
 import junit.framework.TestCase;
 
+import org.apache.uima.cas.ArrayFS;
+import org.apache.uima.cas.ByteArrayFS;
+import org.apache.uima.cas.ShortArrayFS;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.CASException;
 import org.apache.uima.cas.FSIndex;
@@ -42,6 +47,7 @@
 import org.apache.uima.cas.admin.TypeSystemMgr;
 import org.apache.uima.cas.impl.CASImpl;
 import org.apache.uima.cas.impl.CASSerializer;
+import org.apache.uima.cas.impl.LowLevelCAS;
 import org.apache.uima.cas.impl.Serialization;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.internal.util.TextStringTokenizer;
@@ -78,6 +84,20 @@
 
   public static final String[] STR_1_VALS = { "test1", "test2" };
 
+  public static final String OSTR_TYPE = "theType";
+  
+  public static final String OSTR_TYPE_FEAT = "theString";
+  
+  public static final String OBYTE_TYPE_FEAT = "theByte";
+  
+  public static final String OSHORT_TYPE_FEAT = "theShort";
+  
+  public static final String OBYTEA_TYPE_FEAT = "theByteArray";
+  
+  public static final String OSHORTA_TYPE_FEAT = "theShortArray";
+  
+  public static final String OLONG_TYPE_FEAT = "theLong";
+
   private CASMgr casMgr;
 
   private CAS cas;
@@ -100,6 +120,21 @@
 
   private Type strSub1;
 
+  private Type theTypeType;
+  
+  private Feature theStringFeature;
+  
+  private Feature theByteFeature;
+  
+  private Feature theShortFeature;
+  
+  private Feature theByteArrayFeature;
+  
+  private Feature theShortArrayFeature;
+  
+  private Feature theLongFeature;
+
+
   public SerializationReinitTest(String arg) {
     super(arg);
   }
@@ -124,6 +159,13 @@
     sentenceType = ts.getType(SENT_TYPE);
     strSub1 = ts.getType(STRING_SUBTYPE_1);
     assertTrue(strSub1 != null);
+    theTypeType = ts.getType(OSTR_TYPE);
+    theStringFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OSTR_TYPE_FEAT);
+    theByteFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OBYTE_TYPE_FEAT);
+    theByteArrayFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OBYTEA_TYPE_FEAT);
+    theShortFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OSHORT_TYPE_FEAT);
+    theShortArrayFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OSHORTA_TYPE_FEAT);
+    theLongFeature = ts.getFeatureByFullName(OSTR_TYPE + TypeSystem.FEATURE_SEPARATOR + OLONG_TYPE_FEAT);
   }
 
   public void tearDown() {
@@ -168,6 +210,19 @@
     tsa.addType(EOS_TYPE, tokenTypeType);
     tsa.addFeature(TOKEN_TYPE_FEAT, tokenType, tokenTypeType);
     tsa.addStringSubtype(STRING_SUBTYPE_1, STR_1_VALS);
+    Type stringType = tsa.getType(CAS.TYPE_NAME_STRING);
+    Type byteType = tsa.getType(CAS.TYPE_NAME_BYTE);
+    Type byteArrayType = tsa.getType(CAS.TYPE_NAME_BYTE_ARRAY);
+    Type shortType = tsa.getType(CAS.TYPE_NAME_SHORT);
+    Type shortArrayType = tsa.getType(CAS.TYPE_NAME_SHORT_ARRAY);
+    Type longType = tsa.getType(CAS.TYPE_NAME_LONG);
+    Type theTypeType = tsa.addType(OSTR_TYPE, annotType);
+    tsa.addFeature(OSTR_TYPE_FEAT, theTypeType, stringType);
+    tsa.addFeature(OBYTE_TYPE_FEAT, theTypeType, byteType);
+    tsa.addFeature(OSHORT_TYPE_FEAT, theTypeType, shortType);
+    tsa.addFeature(OBYTEA_TYPE_FEAT, theTypeType, byteArrayType);
+    tsa.addFeature(OSHORTA_TYPE_FEAT, theTypeType, shortArrayType);
+    tsa.addFeature(OLONG_TYPE_FEAT, theTypeType, longType);
     // Commit the type system.
     ((CASImpl) aCas).commitTypeSystem();
     // assert(tsa.isCommitted());
@@ -529,6 +584,92 @@
     overallTime = System.currentTimeMillis() - overallTime;
     // System.out.println("Time taken over all: " + new TimeSpan(overallTime));
 
+  }
+
+
+  /** Test basic blob serialization
+   */
+  public void testBlob() throws Exception {
+
+    /*
+     * Test that FS, indexes and strings work after repeated blob serialization
+     * For each iteration, add two new FS, serialize and test all created so
+     * The first FS sets the string feature using standard API => goes into stringlist
+     * The second FS sets the string feature using lowlevel API => goes into stringheap 
+     * 
+     * Throw in tests of the byte, short and long heaps as well
+     * 
+     */
+  String testString = "testString";
+  cas.reset();
+  LowLevelCAS ll_cas = cas.getLowLevelCAS();
+  FSIndexRepository ir = cas.getIndexRepository();
+  int ll_strfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theStringFeature);
+  int ll_bytefeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theByteFeature);
+  int ll_shortfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theShortFeature);
+  int ll_bytearrayfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theByteArrayFeature);
+  int ll_shortarrayfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theShortArrayFeature);
+  int ll_longfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theLongFeature);
+  
+  for (int cycle=0; cycle<10; cycle+=2) {
+    FeatureStructure newFS1 = cas.createFS(theTypeType); 
+    ir.addFS(newFS1);
+    newFS1.setIntValue(startFeature, cycle);
+    newFS1.setIntValue(endFeature, cycle+1);
+    // set string using normal string feature create
+    newFS1.setStringValue(theStringFeature, testString);
+    newFS1.setByteValue(theByteFeature, (byte)cycle);
+    newFS1.setShortValue(theShortFeature, (short)cycle);
+    newFS1.setLongValue(theLongFeature, (long)cycle);
+    ByteArrayFS newBA1 = cas.createByteArrayFS(1); 
+    ShortArrayFS newSA1 = cas.createShortArrayFS(1); 
+    newBA1.set(0, (byte)cycle);
+    newSA1.set(0, (short)cycle);
+    newFS1.setFeatureValue(theByteArrayFeature, newBA1);
+    newFS1.setFeatureValue(theShortArrayFeature, newSA1);
+
+    FeatureStructure newFS2 = cas.createFS(theTypeType);
+    ByteArrayFS newBA2 = cas.createByteArrayFS(1);
+    ShortArrayFS newSA2 = cas.createShortArrayFS(1); 
+    newFS2.setIntValue(startFeature, cycle+1);
+    newFS2.setIntValue(endFeature, cycle+2);
+    ir.addFS(newFS2);
+    // set string using lowlevel string create API
+    final int llfs2 = ll_cas.ll_getFSRef(newFS2);
+    final int llba2 = ll_cas.ll_getFSRef(newBA2);
+    final int llsa2 = ll_cas.ll_getFSRef(newSA2);
+    ll_cas.ll_setCharBufferValue(llfs2, ll_strfeatcode,
+            testString.toCharArray(), 0, testString.length());
+    ll_cas.ll_setByteValue(llfs2, ll_bytefeatcode, (byte)(cycle+1));
+    ll_cas.ll_setShortValue(llfs2, ll_shortfeatcode, (short)(cycle+1));
+    ll_cas.ll_setLongValue(llfs2, ll_longfeatcode, (long)(cycle+1));
+    ll_cas.ll_setByteArrayValue(llba2, 0, (byte)(cycle+1));
+    ll_cas.ll_setShortArrayValue(llsa2, 0, (short)(cycle+1));
+    newFS2.setFeatureValue(theByteArrayFeature, newBA2);
+    newFS2.setFeatureValue(theShortArrayFeature, newSA2);
+
+    ByteArrayOutputStream fos = new ByteArrayOutputStream();
+    Serialization.serializeCAS(cas, fos);
+      cas.reset();
+    ByteArrayInputStream fis = new ByteArrayInputStream(fos.toByteArray());
+    Serialization.deserializeCAS(cas, fis);
+
+    FSIndex idx = cas.getAnnotationIndex(theTypeType);
+    FSIterator iter = idx.iterator();
+    for (int tc=0; tc<cycle+1; tc++) {
+      FeatureStructure testFS = iter.get();
+      iter.moveToNext();
+      assertTrue(tc == testFS.getIntValue(startFeature));
+      assertTrue(testString.equals(testFS.getStringValue(theStringFeature)));
+      assertTrue(tc == testFS.getByteValue(theByteFeature));
+      assertTrue(tc == testFS.getShortValue(theShortFeature));
+      assertTrue(tc == testFS.getLongValue(theLongFeature));
+      ByteArrayFS ba = (ByteArrayFS)testFS.getFeatureValue(theByteArrayFeature);
+      assertTrue(tc == ba.get(0));
+      ShortArrayFS sa = (ShortArrayFS)testFS.getFeatureValue(theShortArrayFeature);
+      assertTrue(tc == sa.get(0));
+    }
+    }  
   }
 
   public static void main(String[] args) {