[exo-jcr-commits] exo-jcr SVN: r2043 - jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/dataflow/serialization.
do-not-reply at jboss.org
do-not-reply at jboss.org
Tue Mar 9 05:17:49 EST 2010
Author: areshetnyak
Date: 2010-03-09 05:17:48 -0500 (Tue, 09 Mar 2010)
New Revision: 2043
Modified:
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/dataflow/serialization/JcrImplSerializationBaseTest.java
Log:
EXOJCR-571 : The tests be serialization was fixed (imitation save in JCR on deserialized changes log)
Modified: jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/dataflow/serialization/JcrImplSerializationBaseTest.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/dataflow/serialization/JcrImplSerializationBaseTest.java 2010-03-08 12:41:47 UTC (rev 2042)
+++ jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/dataflow/serialization/JcrImplSerializationBaseTest.java 2010-03-09 10:17:48 UTC (rev 2043)
@@ -18,23 +18,36 @@
*/
package org.exoplatform.services.jcr.impl.dataflow.serialization;
-import org.exoplatform.services.jcr.JcrImplBaseTest;
-import org.exoplatform.services.jcr.dataflow.ItemState;
-import org.exoplatform.services.jcr.dataflow.TransactionChangesLog;
-import org.exoplatform.services.jcr.dataflow.serialization.UnknownClassIdException;
-import org.exoplatform.services.jcr.datamodel.ItemData;
-import org.exoplatform.services.jcr.datamodel.PropertyData;
-import org.exoplatform.services.jcr.datamodel.ValueData;
-
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import org.exoplatform.services.jcr.JcrImplBaseTest;
+import org.exoplatform.services.jcr.dataflow.ChangesLogIterator;
+import org.exoplatform.services.jcr.dataflow.ItemState;
+import org.exoplatform.services.jcr.dataflow.PlainChangesLog;
+import org.exoplatform.services.jcr.dataflow.TransactionChangesLog;
+import org.exoplatform.services.jcr.dataflow.serialization.UnknownClassIdException;
+import org.exoplatform.services.jcr.datamodel.ItemData;
+import org.exoplatform.services.jcr.datamodel.PropertyData;
+import org.exoplatform.services.jcr.datamodel.ValueData;
+import org.exoplatform.services.jcr.impl.dataflow.persistent.StreamPersistedValueData;
+import org.exoplatform.services.jcr.impl.storage.value.fs.operations.ValueFileIOHelper;
+import org.exoplatform.services.jcr.storage.value.ValueIOChannel;
+
/**
* Created by The eXo Platform SAS. <br/>Date: 16.02.2009
*
@@ -126,10 +139,113 @@
{
// ok
}
+
+
+ //Imitation of save.
+ imitationSave(readed);
return readed;
}
- // public void test() throws Exception {
- // }
+ /**
+ * Imitation of JCR save
+ *
+ * @param readed
+ * @return
+ * @throws IOException
+ */
+ private void imitationSave(List<TransactionChangesLog> readed) throws IOException
+ {
+ for (TransactionChangesLog tLog : readed)
+ {
+ ChangesLogIterator it = tLog.getLogIterator();
+
+ while (it.hasNextLog())
+ {
+ PlainChangesLog pLog = it.nextLog();
+
+ for (ItemState state : pLog.getAllStates())
+ {
+ ItemData itemData = state.getData();
+
+ if (!itemData.isNode())
+ {
+ PropertyData propData = (PropertyData)itemData;
+
+ for(ValueData valueData : propData.getValues())
+ {
+ if (valueData instanceof StreamPersistedValueData) {
+ // imitation of JCR save
+ if (((StreamPersistedValueData) valueData).getTempFile() != null)
+ {
+ ((StreamPersistedValueData) valueData).setPersistedFile(((StreamPersistedValueData) valueData).getTempFile());
+ }
+ else
+ {
+ File file = File.createTempFile("tempFile", "tmp");
+ file.deleteOnExit();
+
+ copy(((StreamPersistedValueData) valueData).getStream(), new FileOutputStream(file));
+ ((StreamPersistedValueData) valueData).setPersistedFile(file);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ protected long copy(InputStream in, OutputStream out) throws IOException
+ {
+ // compare classes as in Java6 Channels.newChannel(), Java5 has a bug in newChannel().
+ boolean inFile = in instanceof FileInputStream && FileInputStream.class.equals(in.getClass());
+ boolean outFile = out instanceof FileOutputStream && FileOutputStream.class.equals(out.getClass());
+ if (inFile && outFile)
+ {
+ // it's user file
+ FileChannel infch = ((FileInputStream)in).getChannel();
+ FileChannel outfch = ((FileOutputStream)out).getChannel();
+
+ long size = 0;
+ long r = 0;
+ do
+ {
+ r = outfch.transferFrom(infch, r, infch.size());
+ size += r;
+ }
+ while (r < infch.size());
+ return size;
+ }
+ else
+ {
+ // it's user stream (not a file)
+ ReadableByteChannel inch = inFile ? ((FileInputStream)in).getChannel() : Channels.newChannel(in);
+ WritableByteChannel outch = outFile ? ((FileOutputStream)out).getChannel() : Channels.newChannel(out);
+
+ long size = 0;
+ int r = 0;
+ ByteBuffer buff = ByteBuffer.allocate(32 * 1024);
+ buff.clear();
+ while ((r = inch.read(buff)) >= 0)
+ {
+ buff.flip();
+
+ // copy all
+ do
+ {
+ outch.write(buff);
+ }
+ while (buff.hasRemaining());
+
+ buff.clear();
+ size += r;
+ }
+
+ if (outFile)
+ ((FileChannel)outch).force(true); // force all data to FS
+
+ return size;
+ }
+ }
}
More information about the exo-jcr-commits
mailing list