[hibernate-commits] Hibernate SVN: r14973 - in search/trunk/src: test/org/hibernate/search/test/configuration and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jul 28 19:07:33 EDT 2008


Author: epbernard
Date: 2008-07-28 19:07:33 -0400 (Mon, 28 Jul 2008)
New Revision: 14973

Modified:
   search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java
   search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java
   search/trunk/src/test/org/hibernate/search/test/configuration/ShardsConfigurationTest.java
   search/trunk/src/test/org/hibernate/search/test/configuration/UselessShardingStrategy.java
Log:
HSEARCH-241 make possible to list available properties in the Properties object passed by Hibernate Search (MaskedProperties)

Modified: search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java	2008-07-28 17:09:56 UTC (rev 14972)
+++ search/trunk/src/java/org/hibernate/search/backend/configuration/MaskedProperty.java	2008-07-28 23:07:33 UTC (rev 14973)
@@ -15,6 +15,10 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -23,10 +27,14 @@
  * A wrapper to Properties, to restrict the availability of
  * values to only those which have a key beginning with some
  * masking String.
- * Methods to list available keys or values or otherwise
- * enumerate available properties are not available.
+ * Supported methods to enumerate the lsit of properties are:
+ *   - propertyNames()
+ *   - keySet()
+ *   - keys()
+ * Other methods including methods returning Entries and values are not supported
  * 
  * @author Sanne Grinovero
+ * @author Emmanuel Bernard
  */
 public class MaskedProperty extends Properties implements Serializable {
 	
@@ -36,6 +44,7 @@
 	private final Properties masked;
 	private final Properties fallBack;
 	private final String radix;
+	private transient List<String> propertyNames;
 	
 	/**
 	 * Provides a view to the provided Properties hiding
@@ -131,13 +140,41 @@
 		throw new UnsupportedOperationException();
 	}
 
-	/**
-	 * @throws UnsupportedOperationException
-	 */
 	@Override
 	public Enumeration<?> propertyNames() {
-		throw new UnsupportedOperationException();
+		initPropertyNames();
+		return new PropertyNameEnumeration();
 	}
+
+	private void initPropertyNames() {
+		if ( propertyNames != null) return;
+		propertyNames = new ArrayList<String>();
+		List<String> maskedProperties = new ArrayList<String>();
+		//we use keys to be safe and avoid CCE for non String key entries
+		Enumeration maskedNames = masked.keys();
+		while ( maskedNames.hasMoreElements() ) {
+			Object key = maskedNames.nextElement();
+			if ( String.class.isInstance( key ) ) {
+				String maskedProperty = (String) key;
+				if ( maskedProperty.startsWith( radix ) ) {
+					maskedProperties.add(maskedProperty);
+					propertyNames.add(maskedProperty.substring( radix.length(), maskedProperty.length() ) );
+				}
+			}
+		}
+		if (fallBack != null) {
+			Enumeration fallbackKeys = fallBack.keys();
+			while ( fallbackKeys.hasMoreElements() ) {
+				Object key = fallbackKeys.nextElement();
+				if ( String.class.isInstance( key ) ) {
+					String fallbackProperty = (String) key;
+					if ( ! maskedProperties.contains( fallbackProperty )) {
+						propertyNames.add(fallbackProperty);
+					}
+				}
+			}
+		}
+	}
 	
 	/**
 	 * @throws UnsupportedOperationException
@@ -198,12 +235,10 @@
 		throw new UnsupportedOperationException();
 	}
 
-	/**
-	 * @throws UnsupportedOperationException
-	 */
 	@Override
 	public boolean contains(Object value) {
-		throw new UnsupportedOperationException();
+		initPropertyNames();
+		return propertyNames.contains( value );
 	}
 
 	/**
@@ -219,6 +254,7 @@
 	 */
 	@Override
 	public Enumeration<Object> elements() {
+		//TODO
 		throw new UnsupportedOperationException();
 	}
 
@@ -253,15 +289,14 @@
 	 */
 	@Override
 	public Enumeration<Object> keys() {
-		throw new UnsupportedOperationException();
+		initPropertyNames();
+		return new PropertyNameEnumeration();
 	}
 
-	/**
-	 * @throws UnsupportedOperationException
-	 */
 	@Override
 	public Set<Object> keySet() {
-		throw new UnsupportedOperationException();
+		initPropertyNames();
+		return new HashSet<Object>(propertyNames);
 	}
 
 	/**
@@ -301,7 +336,8 @@
 	 */
 	@Override
 	public int size() {
-		throw new UnsupportedOperationException();
+		initPropertyNames();
+		return propertyNames.size();
 	}
 
 	@Override
@@ -356,5 +392,17 @@
 	private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
 		aOutputStream.defaultWriteObject();
 	}
-	
+
+	private class PropertyNameEnumeration implements Enumeration<Object> {
+		private int index = -1;
+
+		public boolean hasMoreElements() {
+			return index + 1 < propertyNames.size();
+		}
+
+		public Object nextElement() {
+			index++;
+			return propertyNames.get( index );
+		}
+	}
 }

Modified: search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java	2008-07-28 17:09:56 UTC (rev 14972)
+++ search/trunk/src/test/org/hibernate/search/test/configuration/MaskedPropertiesTest.java	2008-07-28 23:07:33 UTC (rev 14973)
@@ -3,6 +3,7 @@
 
 import java.io.IOException;
 import java.util.Properties;
+import java.util.Enumeration;
 
 import org.hibernate.search.backend.configuration.MaskedProperty;
 import org.hibernate.search.test.SerializationTestHelper;
@@ -20,6 +21,7 @@
 		cfg.put( "hibernate.search.Animals.transaction.max_merge_docs", "5" );
 		cfg.put( "hibernate.search.default.transaction.max_merge_docs", "6" );
 		cfg.put( "hibernate.search.default.transaction.indexwriter.max_field_length", "7" );
+		cfg.put( "hibernate.notsearch.tests.default", "7" );
 
 		//this is more a "concept demo" than a test:
 		Properties root = new MaskedProperty( cfg, "hibernate.search" );
@@ -38,6 +40,13 @@
 		assertEquals( "7" , newStyleTransaction.getProperty( "max_field_length" ) );
 		assertEquals( "7" , newStyleTransactionInShard2.getProperty( "max_field_length" ) );
 		assertEquals( "5" , transaction.getProperty( "max_merge_docs" ) );
+
+		Enumeration<?> propertyNames = newStyleTransaction.propertyNames();
+		int count = 0;
+		while ( propertyNames.hasMoreElements() ) {
+			count++;
+			System.out.println( propertyNames.nextElement() );
+		}
 	}
 	
 	public void testSerializability() throws IOException, ClassNotFoundException {

Modified: search/trunk/src/test/org/hibernate/search/test/configuration/ShardsConfigurationTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/configuration/ShardsConfigurationTest.java	2008-07-28 17:09:56 UTC (rev 14972)
+++ search/trunk/src/test/org/hibernate/search/test/configuration/ShardsConfigurationTest.java	2008-07-28 23:07:33 UTC (rev 14973)
@@ -34,6 +34,8 @@
 		cfg.setProperty( "hibernate.search.Documents.transaction.max_buffered_docs", "6" );
 		cfg.setProperty( "hibernate.search.Documents.sharding_strategy.nbr_of_shards", "4" );
 		cfg.setProperty( "hibernate.search.Documents.sharding_strategy", UselessShardingStrategy.class.getCanonicalName() );
+		cfg.setProperty( "hibernate.search.Documents.sharding_strategy.test.system.default", "45" );
+		cfg.setProperty( "hibernate.search.Documents.sharding_strategy.test.output", "70" );
 		cfg.setProperty( "hibernate.search.Documents.0.batch.max_merge_docs", "57" );
 		cfg.setProperty( "hibernate.search.Documents.0.directory_provider", RAMDirectoryProvider.class.getCanonicalName() );
 		cfg.setProperty( "hibernate.search.Documents.0.transaction.max_buffered_docs", "58" );

Modified: search/trunk/src/test/org/hibernate/search/test/configuration/UselessShardingStrategy.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/configuration/UselessShardingStrategy.java	2008-07-28 17:09:56 UTC (rev 14972)
+++ search/trunk/src/test/org/hibernate/search/test/configuration/UselessShardingStrategy.java	2008-07-28 23:07:33 UTC (rev 14973)
@@ -3,6 +3,7 @@
 
 import java.io.Serializable;
 import java.util.Properties;
+import java.util.Enumeration;
 
 import org.apache.lucene.document.Document;
 import org.hibernate.search.store.DirectoryProvider;
@@ -27,6 +28,34 @@
 	}
 
 	public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
+		Enumeration<?> propertyNames = properties.propertyNames();
+		int counter;
+		counter = checkEnumeration( propertyNames );
+		if (counter != 2) throw new IllegalStateException( "propertyNames() fails" );
+		counter = checkEnumeration( properties.keys() );
+		if (counter != 2) throw new IllegalStateException( "keys() fails" );
+		counter = 0;
+		for (Object key :  properties.keySet() ) {
+			if ( ! String.class.isInstance( key ) ) continue;
+			if ( String.class.cast( key ).startsWith("test.") ) {
+				System.out.println( key );
+				counter++;
+			}
+		}
+		if (counter != 2) throw new IllegalStateException( "keySet() fails" );		
 	}
 
+	private int checkEnumeration(Enumeration<?> propertyNames) {
+		int counter = 0;
+		while ( propertyNames.hasMoreElements() ) {
+			Object key = propertyNames.nextElement();
+			if ( ! String.class.isInstance( key ) ) continue;
+			String propertyName = (String) key;
+			if ( propertyName.startsWith("test.") ) {
+				counter++;
+			}
+		}
+		return counter;
+	}
+
 }




More information about the hibernate-commits mailing list