Author: sannegrinovero
Date: 2008-06-09 12:58:31 -0400 (Mon, 09 Jun 2008)
New Revision: 14755
Added:
search/trunk/src/test/org/hibernate/search/test/SerializationTestHelper.java
Modified:
search/trunk/src/java/org/hibernate/search/backend/LuceneIndexingParameters.java
search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderHelper.java
search/trunk/src/test/org/hibernate/search/test/configuration/LuceneIndexingParametersTest.java
search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java
Log:
fix Serialization for LuceneIndexingParameters and MaskedProperty; added serializability
test helper.
Modified:
search/trunk/src/java/org/hibernate/search/backend/LuceneIndexingParameters.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/LuceneIndexingParameters.java 2008-06-09
15:57:07 UTC (rev 14754)
+++
search/trunk/src/java/org/hibernate/search/backend/LuceneIndexingParameters.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -28,8 +28,6 @@
private static final long serialVersionUID = 5424606407623591663L;
- private final Logger log = LoggerFactory.getLogger( LuceneIndexingParameters.class );
-
// value keyword
public static final String EXPLICIT_DEFAULT_VALUE = "default";
// property path keywords
@@ -47,12 +45,14 @@
Properties transactionProps = new MaskedProperty( indexingParameters, TRANSACTION );
//get keys for "batch" (defaulting to transaction)
Properties batchProps = new MaskedProperty( indexingParameters, BATCH, transactionProps
); //TODO to close HSEARCH-201 just remove 3° parameter
- transactionIndexParameters = new ParameterSet( transactionProps, TRANSACTION );
- batchIndexParameters = new ParameterSet( batchProps, BATCH );
- doSanityChecks( transactionIndexParameters, batchIndexParameters );
+ //logger only used during object construction: (logger not serializable).
+ Logger log = LoggerFactory.getLogger( LuceneIndexingParameters.class );
+ transactionIndexParameters = new ParameterSet( transactionProps, TRANSACTION, log );
+ batchIndexParameters = new ParameterSet( batchProps, BATCH, log );
+ doSanityChecks( transactionIndexParameters, batchIndexParameters, log );
}
- private void doSanityChecks(ParameterSet transParams, ParameterSet batchParams) {
+ private void doSanityChecks(ParameterSet transParams, ParameterSet batchParams, Logger
log) {
if ( log.isWarnEnabled() ) {
Integer maxFieldLengthTransaction = transParams.parameters.get( MAX_FIELD_LENGTH );
Integer maxFieldLengthBatch = transParams.parameters.get( MAX_FIELD_LENGTH );
@@ -77,13 +77,13 @@
return batchIndexParameters;
}
- public class ParameterSet implements Serializable {
+ public static class ParameterSet implements Serializable {
private static final long serialVersionUID = -6121723702279869524L;
final Map<IndexWriterSetting, Integer> parameters = new
EnumMap<IndexWriterSetting, Integer>(IndexWriterSetting.class);
- public ParameterSet(Properties prop, String paramName) {
+ public ParameterSet(Properties prop, String paramName, Logger log) {
//don't iterate on property entries as we know all the keys:
for ( IndexWriterSetting t : IndexWriterSetting.values() ) {
String key = t.getKey();
@@ -127,6 +127,32 @@
}
}
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((parameters == null) ? 0 : parameters.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final ParameterSet other = (ParameterSet) obj;
+ if (parameters == null) {
+ if (other.parameters != null)
+ return false;
+ } else if (!parameters.equals(other.parameters))
+ return false;
+ return true;
+ }
+
}
public void applyToWriter(IndexWriter writer, boolean batch) {
Modified:
search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java 2008-06-09
15:57:07 UTC (rev 14754)
+++
search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -2,6 +2,8 @@
import java.io.IOException;
import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
@@ -29,7 +31,7 @@
private static final long serialVersionUID = -593307257383085113L;
- private final Logger log = LoggerFactory.getLogger( MaskedProperty.class );
+ private transient Logger log = LoggerFactory.getLogger( MaskedProperty.class );
private final Properties masked;
private final Properties fallBack;
private final String radix;
@@ -344,4 +346,14 @@
return true;
}
+ private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException,
IOException {
+ //always perform the default de-serialization first
+ aInputStream.defaultReadObject();
+ log = LoggerFactory.getLogger( MaskedProperty.class );
+ }
+
+ private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
+ aOutputStream.defaultWriteObject();
+ }
+
}
Modified: search/trunk/src/java/org/hibernate/search/store/DirectoryProviderHelper.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderHelper.java 2008-06-09
15:57:07 UTC (rev 14754)
+++
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderHelper.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -146,7 +146,7 @@
}
/**
- * Users may configure the number of KB to use as
+ * Users may configure the number of MB to use as
* "chunk size" for large file copy operations performed
* by DirectoryProviders.
* @param directoryProviderName
Added: search/trunk/src/test/org/hibernate/search/test/SerializationTestHelper.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/SerializationTestHelper.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/SerializationTestHelper.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -0,0 +1,107 @@
+package org.hibernate.search.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hibernate.search.test.SerializationTestHelper.Foo.TestInnerClass;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Sanne Grinovero
+ */
+public class SerializationTestHelper extends TestCase {
+
+ /**
+ * Duplicates an object using Serialization, it moves
+ * state to and from a buffer. Should be used to test
+ * correct serializability.
+ * @param o The object to "clone"
+ * @return the clone.
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ public static Object duplicateBySerialization(Object o) throws IOException,
ClassNotFoundException {
+ //Serialize to buffer:
+ java.io.ByteArrayOutputStream outStream = new java.io.ByteArrayOutputStream();
+ ObjectOutputStream objectOutStream = new ObjectOutputStream( outStream );
+ objectOutStream.writeObject( o );
+ objectOutStream.flush();
+ objectOutStream.close();
+ //buffer version of Object:
+ byte[] objectBuffer = outStream.toByteArray();
+ //deserialize to new instance:
+ java.io.ByteArrayInputStream inStream = new ByteArrayInputStream( objectBuffer );
+ ObjectInputStream objectInStream = new ObjectInputStream( inStream );
+ Object copy = objectInStream.readObject();
+ return copy;
+ }
+
+ public void testSelf() throws IOException, ClassNotFoundException {
+ Foo a = new Foo();
+ a.list.add( new TestInnerClass(30) );
+ Foo b = (Foo) duplicateBySerialization( a );
+ assertEquals( Integer.valueOf(6), a.integer);
+ assertEquals( Integer.valueOf(7), b.integer);
+ assertEquals( a.list, b.list );
+ }
+
+ static class Foo implements Serializable {
+
+ List<TestInnerClass> list = new ArrayList<TestInnerClass>();
+ transient Integer integer = Integer.valueOf( 6 );
+
+ static class TestInnerClass implements Serializable {
+ private final int v;
+
+ public TestInnerClass(int i) {
+ v = i;
+ }
+
+ public void print() {
+ System.out.println(v);
+ }
+
+ public String toString(){
+ return ""+v;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + v;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final TestInnerClass other = (TestInnerClass) obj;
+ if (v != other.v)
+ return false;
+ return true;
+ }
+ }
+
+ private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException,
IOException {
+ aInputStream.defaultReadObject();
+ integer = Integer.valueOf( 7 );
+ }
+
+ private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
+ aOutputStream.defaultWriteObject();
+ }
+ }
+
+}
Modified:
search/trunk/src/test/org/hibernate/search/test/configuration/LuceneIndexingParametersTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/configuration/LuceneIndexingParametersTest.java 2008-06-09
15:57:07 UTC (rev 14754)
+++
search/trunk/src/test/org/hibernate/search/test/configuration/LuceneIndexingParametersTest.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -1,6 +1,11 @@
package org.hibernate.search.test.configuration;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.hibernate.search.backend.LuceneIndexingParameters;
import org.hibernate.search.test.Document;
+import org.hibernate.search.test.SerializationTestHelper;
import org.hibernate.search.test.query.Author;
import org.hibernate.search.test.query.Book;
import static
org.hibernate.search.backend.configuration.IndexWriterSetting.MAX_BUFFERED_DOCS;
@@ -85,6 +90,14 @@
assertValueIsDefault( Document.class, TRANSACTION, RAM_BUFFER_SIZE );
}
+ public void testSerializability() throws IOException, ClassNotFoundException {
+ LuceneIndexingParameters param = new LuceneIndexingParameters( new Properties() );
+ LuceneIndexingParameters paramCopy = (LuceneIndexingParameters)
+ SerializationTestHelper.duplicateBySerialization( param );
+ assertEquals(param.getBatchIndexParameters(), paramCopy.getBatchIndexParameters());
+ assertEquals(param.getTransactionIndexParameters(),
paramCopy.getTransactionIndexParameters());
+ }
+
protected Class[] getMappings() {
return new Class[] {
Book.class,
Modified:
search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java 2008-06-09
15:57:07 UTC (rev 14754)
+++
search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java 2008-06-09
16:58:31 UTC (rev 14755)
@@ -1,8 +1,10 @@
package org.hibernate.search.test.configuration;
+import java.io.IOException;
import java.util.Properties;
import org.hibernate.search.backend.configuration.MaskedProperty;
+import org.hibernate.search.test.SerializationTestHelper;
/**
* @author Sanne Grinovero
@@ -37,4 +39,14 @@
assertEquals( "5" , transaction.getProperty( "max_merge_docs" ) );
}
+ public void testSerializability() throws IOException, ClassNotFoundException {
+ Properties cfg = new Properties();
+ cfg.setProperty( "base.key", "value" );
+ MaskedProperty originalProps = new MaskedProperty( cfg, "base" );
+ MaskedProperty theCopy = (MaskedProperty)
+ SerializationTestHelper.duplicateBySerialization( originalProps );
+ //this is also testing the logger (transient) has been restored:
+ assertEquals( "value", theCopy.getProperty( "key" ) );
+ }
+
}