From: <res...@us...> - 2010-09-27 13:44:44
|
Revision: 3634 http://bigdata.svn.sourceforge.net/bigdata/?rev=3634&view=rev Author: resendes Date: 2010-09-27 13:44:37 +0000 (Mon, 27 Sep 2010) Log Message: ----------- Added more test cases and cleanup for com.bigdata.util package. Modified Paths: -------------- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/EntryUtil.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java Added Paths: ----------- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/EntryUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/EntryUtil.java 2010-09-27 13:39:12 UTC (rev 3633) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/EntryUtil.java 2010-09-27 13:44:37 UTC (rev 3634) @@ -75,7 +75,7 @@ * type. If no element in the array is an instance of the given * type, then <code>null</code> is returned. */ - public static <T> T getEntryByType(Entry[] attrs, Class<T> type) + public static <T extends Entry> T getEntryByType(Entry[] attrs, Class<T> type) { if( (attrs == null) || (type == null) ) return null; for(int i=0; i<attrs.length; i++) { @@ -85,144 +85,144 @@ return null; }//end getEntryByType - /** - * Examines the given array of entries and returns an array of all - * elements that are an instance of the given class type. If no element in - * the array is an instance of the given type, then <code>null</code> is - * returned. - */ - public static <T> T[] getEntriesByType(Entry[] attrs, Class<T> type) - { - if( (attrs == null) || (type == null) ) return null; - ArrayList<T> matches = null; - for(int i=0; i<attrs.length; i++) { - if( !( type.isInstance(attrs[i]) ) ) continue; - if ( matches == null ) - matches = new ArrayList<T>(); - matches.add( (T)attrs[i] ); - }//end loop +// /** +// * Examines the given array of entries and returns an array of all +// * elements that are an instance of the given class type. If no element in +// * the array is an instance of the given type, then <code>null</code> is +// * returned. +// */ +// public static <T> T[] getEntriesByType(Entry[] attrs, Class<T> type) +// { +// if( (attrs == null) || (type == null) ) return null; +// ArrayList<T> matches = null; +// for(int i=0; i<attrs.length; i++) { +// if( !( type.isInstance(attrs[i]) ) ) continue; +// if ( matches == null ) +// matches = new ArrayList<T>(); +// matches.add( (T)attrs[i] ); +// }//end loop +// +// if ( matches == null ) return null; +// +// return matches.toArray +// ( (T[])java.lang.reflect.Array.newInstance +// ( type, matches.size() ) ); +// } - if ( matches == null ) return null; +// /** +// * Examines the given array of entries and returns a list of all +// * elements that are an instance of the given class type. If no element +// * in the array is an instance of the given type, then an empty list is +// * returned. +// */ +// public static <T> List<T> getEntryListByType(final Entry[] attrs, +// final Class<T> type) +// { +// if(attrs == null) { +// throw new NullPointerException("null attrs array"); +// } +// if(type == null) { +// throw new NullPointerException("null type"); +// } +// final List<T> matches = new ArrayList<T>(); +// for (final Entry attr : attrs) { +// if(type.isInstance(attr)){ +// matches.add(type.cast(attr)); +// } +// } +// return matches; +// } - return matches.toArray - ( (T[])java.lang.reflect.Array.newInstance - ( type, matches.size() ) ); - } +// /** +// * Examines the given array of entries and returns a list of all +// * elements that match the given <code>template</code>; where +// * the matching semantics are defined by the template-matching +// * semantics specified in the Lookup Service Specification. If no +// * element in the given array matches the template, then an empty +// * list is returned. +// */ +// public static <T extends Entry> List<T> getEntryList(final Entry[] attrs, +// final T template) +// { +// final List<T> matches = new ArrayList<T>(); +// for (final Entry attr : attrs) { +// if(LookupAttributes.matches(template, attr)){ +// matches.add((T) attr); +// } +// } +// return matches; +// } - /** - * Examines the given array of entries and returns a list of all - * elements that are an instance of the given class type. If no element - * in the array is an instance of the given type, then an empty list is - * returned. - */ - public static <T> List<T> getEntryListByType(final Entry[] attrs, - final Class<T> type) - { - if(attrs == null) { - throw new NullPointerException("null attrs array"); - } - if(type == null) { - throw new NullPointerException("null type"); - } - final List<T> matches = new ArrayList<T>(); - for (final Entry attr : attrs) { - if(type.isInstance(attr)){ - matches.add(type.cast(attr)); - } - } - return matches; - } +// /** +// * Using the given <code>Logger</code>, displays the contents +// * of the given array of entries. +// */ +// public static void displayEntrySet(Entry[] entries, Logger logger) { +// displayEntrySet(null, entries, null, logger); +// } - /** - * Examines the given array of entries and returns a list of all - * elements that match the given <code>template</code>; where - * the matching semantics are defined by the template-matching - * semantics specified in the Lookup Service Specification. If no - * element in the given array matches the template, then an empty - * list is returned. - */ - public static <T extends Entry> List<T> getEntryList(final Entry[] attrs, - final T template) - { - final List<T> matches = new ArrayList<T>(); - for (final Entry attr : attrs) { - if(LookupAttributes.matches(template, attr)){ - matches.add((T) attr); - } - } - return matches; - } +// /** +// * Using the given <code>Logger</code>, displays the contents +// * of the given array of entries. +// * <p> +// * Additionally, the identification of the output can be +// * customized using the given <code>entrySetName</code>. +// */ +// public static void displayEntrySet(Entry[] entries, +// String entrySetName, +// Logger logger) +// { +// displayEntrySet(null, entries, entrySetName, logger); +// } - /** - * Using the given <code>Logger</code>, displays the contents - * of the given array of entries. - */ - public static void displayEntrySet(Entry[] entries, Logger logger) { - displayEntrySet(null, entries, null, logger); - } +// /** +// * Using the given <code>Logger</code>, displays the contents +// * of the given array of entries. +// * <p> +// * Each line displayed is prefixed using the value of the +// * <code>prefix</code> parameter. +// */ +// public static void displayEntrySet(String prefix, +// Entry[] entries, +// Logger logger) +// { +// displayEntrySet(prefix, entries, null, logger); +// } - /** - * Using the given <code>Logger</code>, displays the contents - * of the given array of entries. - * <p> - * Additionally, the identification of the output can be - * customized using the given <code>entrySetName</code>. - */ - public static void displayEntrySet(Entry[] entries, - String entrySetName, - Logger logger) - { - displayEntrySet(null, entries, entrySetName, logger); - } +// /** +// * Using the given <code>Logger</code>, displays the contents +// * of the given array of entries. +// * <p> +// * Each line displayed is prefixed using the value of the +// * <code>prefix</code> parameter. Additionally, the identification +// * of the output can be customized using the given +// * <code>entrySetName</code>. +// */ +// public static void displayEntrySet(String prefix, +// Entry[] entries, +// String entrySetName, +// Logger logger) +// { +// if ( logger.isDebugEnabled() ) { +// if(prefix == null) prefix = ""; +// String name = ((entrySetName == null) ? +// "Entry Set" : entrySetName); +// if(entries == null) { +// logger.debug(prefix+": "+name+" = null"); +// } else if(entries.length <= 0) { +// logger.debug(prefix+": "+name+" = NO_ENTRIES"); +// } else { +// logger.debug(prefix+": "+name); +// logger.debug(prefix+": " +// +"-- Number of Entries = " + entries.length); +// for(int i=0; i<entries.length; i++) { +// displayEntry(entries[i], "", logger); +// }//end loop +// }//endif +// } +// } /** - * Using the given <code>Logger</code>, displays the contents - * of the given array of entries. - * <p> - * Each line displayed is prefixed using the value of the - * <code>prefix</code> parameter. - */ - public static void displayEntrySet(String prefix, - Entry[] entries, - Logger logger) - { - displayEntrySet(prefix, entries, null, logger); - } - - /** - * Using the given <code>Logger</code>, displays the contents - * of the given array of entries. - * <p> - * Each line displayed is prefixed using the value of the - * <code>prefix</code> parameter. Additionally, the identification - * of the output can be customized using the given - * <code>entrySetName</code>. - */ - public static void displayEntrySet(String prefix, - Entry[] entries, - String entrySetName, - Logger logger) - { - if ( logger.isDebugEnabled() ) { - if(prefix == null) prefix = ""; - String name = ((entrySetName == null) ? - "Entry Set" : entrySetName); - if(entries == null) { - logger.debug(prefix+": "+name+" = null"); - } else if(entries.length <= 0) { - logger.debug(prefix+": "+name+" = NO_ENTRIES"); - } else { - logger.debug(prefix+": "+name); - logger.debug(prefix+": " - +"-- Number of Entries = " + entries.length); - for(int i=0; i<entries.length; i++) { - displayEntry(entries[i], "", logger); - }//end loop - }//endif - } - } - - /** * Using the given <code>Logger</code>, displays the contents of * the given <code>entry</code>: Class, class loader, fields. */ @@ -230,19 +230,19 @@ displayEntry(null, entry, null, logger); } - /** - * Using the given <code>Logger</code>, displays the contents of - * the given <code>entry</code>: Class, class loader, fields. - * <p> - * Each line displayed is prefixed using the value of the - * <code>prefix</code> parameter. - */ - public static void displayEntry(String prefix, - Entry entry, - Logger logger) - { - displayEntry(prefix, entry, null, logger); - } +// /** +// * Using the given <code>Logger</code>, displays the contents of +// * the given <code>entry</code>: Class, class loader, fields. +// * <p> +// * Each line displayed is prefixed using the value of the +// * <code>prefix</code> parameter. +// */ +// public static void displayEntry(String prefix, +// Entry entry, +// Logger logger) +// { +// displayEntry(prefix, entry, null, logger); +// } /** * Using the given <code>Logger</code>, displays the contents of @@ -271,7 +271,7 @@ String label, Logger logger) { - if ( logger.isDebugEnabled() ) + if ( logger!= null && logger.isDebugEnabled() ) { if(prefix == null) prefix = ""; if(label == null) label = ""; @@ -305,80 +305,80 @@ } } - /** - * Using the given <code>Logger</code>, displays fields of the given - * <code>entry</code> instances that are not equal. Each line displayed - * is prefixed using the value of the <code>prefix</code> parameter. - */ - public static void displayDiff(String prefix, - Entry entry1, - Entry entry2, - Logger logger) - { - if ( logger.isDebugEnabled() ) { - if(prefix == null) prefix = ""; +// /** +// * Using the given <code>Logger</code>, displays fields of the given +// * <code>entry</code> instances that are not equal. Each line displayed +// * is prefixed using the value of the <code>prefix</code> parameter. +// */ +// public static void displayDiff(String prefix, +// Entry entry1, +// Entry entry2, +// Logger logger) +// { +// if ( logger.isDebugEnabled() ) { +// if(prefix == null) prefix = ""; +// +// String e1Str = (entry1 == null ? +// null : (entry1.getClass()).getName() ); +// String e2Str = (entry2 == null ? +// null : (entry2.getClass()).getName() ); +// +// Map<Field, List> diffMap = diff(entry1, entry2); +// +// if(diffMap == null) { +// logger.log(Level.DEBUG, prefix+": " +// +"CANNOT BE COMPARED: [entry1="+e1Str+"], " +// +"[entry2="+e2Str+"]"); +// return; +// }//endif +// +// if(diffMap.size() == 0) { +// logger.log(Level.DEBUG, prefix+": " +// +"NO DIFFERENCE: [entry1="+e1Str+"], " +// +"[entry2="+e2Str+"]"); +// return; +// }//endif +// +// Set<Map.Entry<Field, List>> fieldSet = diffMap.entrySet(); +// Iterator<Map.Entry<Field, List>> fieldItr = fieldSet.iterator(); +// while( fieldItr.hasNext() ) { +// Map.Entry<Field, List> pair = fieldItr.next(); +// Field field = pair.getKey(); +// List vals = pair.getValue(); +// String fieldName = field.getName(); +// if(vals.size() != 2) {//shouldn't happen +// logger.log(Level.WARN, "UNEXPECTED ERROR: " +// +"number of field values != 2 [field="+fieldName +// +", # of values="+vals.size()+", " +// +"vals="+vals+"]"); +// }//endif +// Object val1 = vals.get(0); +// Object val2 = vals.get(1); +// logger.log(Level.DEBUG, prefix+": " +// +e1Str+"."+fieldName+" = "+val1+" --> "+val2); +// }//end loop +// }//end(logger.isDebugEnabled) +// +// } - String e1Str = (entry1 == null ? - null : (entry1.getClass()).getName() ); - String e2Str = (entry2 == null ? - null : (entry2.getClass()).getName() ); +// public static void displayDiff(Entry entry1, Entry entry2, Logger logger) { +// displayDiff(null, entry1, entry2, logger); +// } - Map<Field, List> diffMap = diff(entry1, entry2); +// /** +// * Compares for equivalence, the contents of two individual entries. +// */ +// public static boolean compareEntries(Entry entry1, +// Entry entry2) +// { +// return ( compareEntrySets(null, +// new Entry[] {entry1}, +// new Entry[] {entry2}, +// null) ); +// } - if(diffMap == null) { - logger.log(Level.DEBUG, prefix+": " - +"CANNOT BE COMPARED: [entry1="+e1Str+"], " - +"[entry2="+e2Str+"]"); - return; - }//endif - - if(diffMap.size() == 0) { - logger.log(Level.DEBUG, prefix+": " - +"NO DIFFERENCE: [entry1="+e1Str+"], " - +"[entry2="+e2Str+"]"); - return; - }//endif - - Set<Map.Entry<Field, List>> fieldSet = diffMap.entrySet(); - Iterator<Map.Entry<Field, List>> fieldItr = fieldSet.iterator(); - while( fieldItr.hasNext() ) { - Map.Entry<Field, List> pair = fieldItr.next(); - Field field = pair.getKey(); - List vals = pair.getValue(); - String fieldName = field.getName(); - if(vals.size() != 2) {//shouldn't happen - logger.log(Level.WARN, "UNEXPECTED ERROR: " - +"number of field values != 2 [field="+fieldName - +", # of values="+vals.size()+", " - +"vals="+vals+"]"); - }//endif - Object val1 = vals.get(0); - Object val2 = vals.get(1); - logger.log(Level.DEBUG, prefix+": " - +e1Str+"."+fieldName+" = "+val1+" --> "+val2); - }//end loop - }//end(logger.isDebugEnabled) - - } - - public static void displayDiff(Entry entry1, Entry entry2, Logger logger) { - displayDiff(null, entry1, entry2, logger); - } - /** * Compares for equivalence, the contents of two individual entries. - */ - public static boolean compareEntries(Entry entry1, - Entry entry2) - { - return ( compareEntrySets(null, - new Entry[] {entry1}, - new Entry[] {entry2}, - null) ); - } - - /** - * Compares for equivalence, the contents of two individual entries. * <p> * Any lines displayed during the comparision will be displayed * using the given <code>Logger</code>. @@ -393,50 +393,50 @@ logger) ); } - /** - * Compares for equivalence, the contents of two individual entries. - * <p> - * Any lines displayed during the comparision will be displayed - * using the given <code>Logger</code>, and will be prefixed using - * the value of the <code>prefix</code> parameter. - */ - public static boolean compareEntries(String prefix, - Entry entry1, - Entry entry2, - Logger logger) - { - return ( compareEntrySets(prefix, - new Entry[] {entry1}, - new Entry[] {entry2}, - logger) ); - } +// /** +// * Compares for equivalence, the contents of two individual entries. +// * <p> +// * Any lines displayed during the comparision will be displayed +// * using the given <code>Logger</code>, and will be prefixed using +// * the value of the <code>prefix</code> parameter. +// */ +// public static boolean compareEntries(String prefix, +// Entry entry1, +// Entry entry2, +// Logger logger) +// { +// return ( compareEntrySets(prefix, +// new Entry[] {entry1}, +// new Entry[] {entry2}, +// logger) ); +// } /* ***************************************************************** */ /* ************************ compareEntrySets *********************** */ /* ***************************************************************** */ - /** - * Compares for equivalence, the contents of two sets of entries - * ignoring duplicate entries. - */ - public static boolean compareEntrySets(Entry[] entrySet1, - Entry[] entrySet2) - { - return compareEntrySets(null, entrySet1, entrySet2, null); - } +// /** +// * Compares for equivalence, the contents of two sets of entries +// * ignoring duplicate entries. +// */ +// public static boolean compareEntrySets(Entry[] entrySet1, +// Entry[] entrySet2) +// { +// return compareEntrySets(null, entrySet1, entrySet2, null); +// } - /** - * Compares for equivalence, the contents of two sets of entries - * ignoring duplicate entries. - * <p> - * Any lines displayed during the comparision will be displayed - * using the given <code>Logger</code>. - */ - public static boolean compareEntrySets(Entry[] entrySet1, - Entry[] entrySet2, - Logger logger) - { - return compareEntrySets(null, entrySet1, entrySet2, logger); - } +// /** +// * Compares for equivalence, the contents of two sets of entries +// * ignoring duplicate entries. +// * <p> +// * Any lines displayed during the comparision will be displayed +// * using the given <code>Logger</code>. +// */ +// public static boolean compareEntrySets(Entry[] entrySet1, +// Entry[] entrySet2, +// Logger logger) +// { +// return compareEntrySets(null, entrySet1, entrySet2, logger); +// } /** * Compares for equivalence, the contents of two sets of entries @@ -467,7 +467,7 @@ logger.trace(prefix+": entrySet1 = " +"null, entrySet2 != null"); }//endif - return false; + return false; }//endif } else {//entrySet1 != null if(entrySet2 == null) { @@ -475,7 +475,7 @@ logger.trace(prefix+": entrySet1 != " +"null, entrySet2 == null"); }//endif - return false; + return false; }//endif }//endif @@ -546,10 +546,10 @@ +": entries left over from comparison loop, " + "entry sets not equal"); }//endif - return false; + return false; }//endif - return true; + return true; } /** Returns public, non-static, non-transient, non-final fields contained @@ -708,255 +708,255 @@ } } - /** - * Convenience method that, given two <code>Entry</code> instances that - * are of the <i>same type</i>, determines which of the corresponding - * public fields of the given <code>Entry</code> instances are not equal, - * and returns a <code>Map</code> in which each key-value pair contains - * a key represented by the <code>Field</code> being compared, and a - * corresponding value represented by a <code>List</code> whose - * elements are the two values of the fields being compared; where the - * first element of the <code>List</code> is the value of the field - * in the first given <code>Entry</code>, and the second element of - * the <code>List</code> is the value of the field in the second - * <code>Entry</code> parameter. - * <p> - * If the <code>Entry</code> values input to this method are not the - * same type, then <code>null</code> is returned. If <code>null</code> - * is input for either or both of the <code>Entry</code> instances, - * then <code>null</code> is also returned. Finally, if the value of - * each public field of the first parameter equals the value of the - * corresponding field of the second parameter, then an empty - * <code>Map</code> is returned. - */ - public static Map<Field, List> diff(Entry e1, Entry e2) { - if (e1 == null || e2 == null) return null; - if (e1.getClass() != e2.getClass()) return null;//must be same type +// /** +// * Convenience method that, given two <code>Entry</code> instances that +// * are of the <i>same type</i>, determines which of the corresponding +// * public fields of the given <code>Entry</code> instances are not equal, +// * and returns a <code>Map</code> in which each key-value pair contains +// * a key represented by the <code>Field</code> being compared, and a +// * corresponding value represented by a <code>List</code> whose +// * elements are the two values of the fields being compared; where the +// * first element of the <code>List</code> is the value of the field +// * in the first given <code>Entry</code>, and the second element of +// * the <code>List</code> is the value of the field in the second +// * <code>Entry</code> parameter. +// * <p> +// * If the <code>Entry</code> values input to this method are not the +// * same type, then <code>null</code> is returned. If <code>null</code> +// * is input for either or both of the <code>Entry</code> instances, +// * then <code>null</code> is also returned. Finally, if the value of +// * each public field of the first parameter equals the value of the +// * corresponding field of the second parameter, then an empty +// * <code>Map</code> is returned. +// */ +// public static Map<Field, List> diff(Entry e1, Entry e2) { +// if (e1 == null || e2 == null) return null; +// if (e1.getClass() != e2.getClass()) return null;//must be same type +// +// Map<Field, List> retMap = new LinkedHashMap<Field, List>(); +// +// if (e1 == e2) return retMap; +// +// // compare each field +// Field[] fieldsArray1 = getFieldInfo(e1);//returns public fields +// Field[] fieldsArray2 = getFieldInfo(e2);//returns public fields +// try { +// for(int i=0; i<fieldsArray1.length; i++) { +// +// Field field1 = fieldsArray1[i]; +// Field field2 = fieldsArray2[i]; +// +// Object f1 = field1.get(e1);//value of field1 +// Object f2 = field2.get(e2);//value of field2 +// +// List fieldVals = new ArrayList(); +// +// if(f2 == f1) continue;//same obj or both null, f1 == f2, skip +// +// if(f2 == null || f1 == null) {//only 1 is null, f1 != f2 +// fieldVals.add(f1); +// fieldVals.add(f2); +// retMap.put(field1, fieldVals); +// continue;//next field +// }//endif +// +// if( !f2.equals(f1) ) {//test for arrays +// Class f2Class = f2.getClass(); +// Class f1Class = f1.getClass(); +// if( f2Class.isArray() && f1Class.isArray() ) { +// //determine if they are Object or primitive arrays +// Class f2Type = f2Class.getComponentType(); +// Class f1Type = f1Class.getComponentType(); +// +// if( !f2Type.equals(f1Type) ) {//field types not equal +// fieldVals.add(f1); +// fieldVals.add(f2); +// retMap.put(field1, fieldVals); +// continue;//next field +// }//endif +// +// boolean equalArrays = false; +// if( !f2Type.isPrimitive() && !f1Type.isPrimitive() ) { +// +// //types are equal & both non-primitive, use Object +// equalArrays = +// Arrays.equals( (Object[])f2, (Object[])f1 ); +// +// } else if(f2Type.isPrimitive()&&f1Type.isPrimitive()){ +// +// //types are equal & both primitive, use primitive +// if( f2Type.equals(Boolean.TYPE) ) { +// equalArrays = Arrays.equals( (boolean[])f2, +// (boolean[])f1 ); +// } else if( f2Type.equals(Character.TYPE) ) { +// equalArrays = Arrays.equals( (char[])f2, +// (char[])f1 ); +// } else if( f2Type.equals(Byte.TYPE) ) { +// equalArrays = Arrays.equals( (byte[])f2, +// (byte[])f1 ); +// } else if( f2Type.equals(Short.TYPE) ) { +// equalArrays = Arrays.equals( (short[])f2, +// (short[])f1 ); +// } else if( f2Type.equals(Integer.TYPE) ) { +// equalArrays = Arrays.equals( (int[])f2, +// (int[])f1 ); +// } else if( f2Type.equals(Long.TYPE) ) { +// equalArrays = Arrays.equals( (long[])f2, +// (long[])f1 ); +// } else if( f2Type.equals(Float.TYPE) ) { +// equalArrays = Arrays.equals( (float[])f2, +// (float[])f1 ); +// } else if( f2Type.equals(Double.TYPE) ) { +// equalArrays = Arrays.equals( (double[])f2, +// (double[])f1 ); +// }//endif +// +// //else 1 primitive, 1 not primitive ==> !equalArrays +// +// }//endif +// +// if( !equalArrays ) {//f1 not equal f2 +// fieldVals.add(f1); +// fieldVals.add(f2); +// retMap.put(field1, fieldVals); +// continue;//next field +// }//endif +// +// continue;//f1 equals f2, skip it and go to next field +// +// }//endif( f2Class.isArray() && f1Class.isArray() ) +// +// //(not arrays && !f2.equals(f1)) +// fieldVals.add(f1); +// fieldVals.add(f2); +// retMap.put(field1, fieldVals); +// +// }//endif( !f2.equals(f1) ) +// +// }//end loop +// +// return retMap; +// +// } catch (IllegalAccessException e) { +// // should never happen, all entry fields are public +// throw new AssertionError(e); +// } +// } - Map<Field, List> retMap = new LinkedHashMap<Field, List>(); +// /** +// * Convenience method that, given two <code>Entry</code> instances, +// * copies the values of the <i>usable</i> (<code>public</code>, +// * non-<code>transient</code>, non-<code>static</code>, +// * non-<code>final</code>) fields of the first <code>Entry</code> +// * parameter to the usable fields of the same name to the second given +// * <code>Entry</code> parameter. Any usable field from one of the +// * parameters that does not have a counterpart in the other parameter +// * will be by passed. This method can be useful, for example, when +// * converting an <code>AcinionAttribute</code> to a parallel +// * <code>AcinionEvent</code> for reporting outside of the system. +// * If <code>null</code> is input for either parameter, then +// * <code>null</code> is returned. +// */ +// public static Entry copyEntry(Entry e1, Entry e2, Logger logger) { +// if (e1 == null || e2 == null) return null; +// Entry retEntry = e2; +// String e1Name = (e1.getClass()).getSimpleName(); +// String retEntryName = (retEntry.getClass()).getSimpleName(); +// +// Field[] e1Fields = EntryUtil.getFieldInfo(e1); +// Field[] retEntryFields = EntryUtil.getFieldInfo(retEntry); +// if( e1Fields == null || e1Fields.length == 0 ) return null; +// if( retEntryFields == null || retEntryFields.length == 0 ) return null; +// for(int i=0; i<e1Fields.length; i++) { +// Field e1Field = e1Fields[i]; +// String e1FieldName = e1Field.getName(); +// for(int j=0; j<retEntryFields.length; j++) { +// Field retEntryField = retEntryFields[j]; +// String retEntryFieldName = retEntryField.getName(); +// String errStr = (logger == null ? null : +// "cannot set " +// +retEntryName+"."+retEntryFieldName +// +" to value of " +// +e1Name+"."+e1FieldName ); +// if( e1FieldName.equals(retEntryFieldName) ) { +// try { +// retEntryField.set( retEntry, e1Field.get(e1) ); +// } catch(IllegalArgumentException e) { +// if(logger != null) logger.log(Level.WARN, errStr); +// e.printStackTrace(); +// } catch(IllegalAccessException e) { +// if(logger != null) logger.log(Level.WARN, errStr); +// e.printStackTrace(); +// } +// }//endif +// }//end loop(j) +// }//end loop(i) +// return retEntry; +// } - if (e1 == e2) return retMap; +// /** +// * Returns an instance of the given <code>Entry</code> class with +// * all <i>usable</i> (public, non-static, non-transient, non-final) +// * fields set to <code>null</code>. The object returned by this method +// * can be used as a template that will match any <code>Entry</code> +// * that is of the same type as the <code>Entry</code> input to this +// * method. +// * +// * This method can be useful when working with an <code>Entry</code> +// * instance that defines a no-arg constructor that sets one or more +// * of its fields to a non-<code>null</code> value. +// */ +// public static Entry wildcardAll(Entry entry) { +// try { +// Class realClass = entry.getClass(); +// Entry template = (Entry) realClass.newInstance(); +// +// Field[] f = realClass.getFields(); +// for(int i = 0; i < f.length; i++) { +// if(! usableField(f[i])) continue; +// f[i].set(template, null); +// }//end loop +// return template; +// } catch (Throwable t) { +// Logger.getLogger(EntryUtil.class).error("wildcardEntry failed", t); +// } +// return null; +// } - // compare each field - Field[] fieldsArray1 = getFieldInfo(e1);//returns public fields - Field[] fieldsArray2 = getFieldInfo(e2);//returns public fields - try { - for(int i=0; i<fieldsArray1.length; i++) { +// /** +// * Returns a clone of the given {@link Entry}. +// * (From com.sun.jini.example.browser.ServiceEditor.) +// */ +// public static Entry cloneEntry(Entry attr) { +// try { +// Class realClass = attr.getClass(); +// Entry template = (Entry) realClass.newInstance(); +// +// Field[] f = realClass.getFields(); +// for(int i = 0; i < f.length; i++) { +// if(! usableField(f[i])) +// continue; +// f[i].set(template, f[i].get(attr)); +// } +// +// return template; +// } catch (Throwable t) { +// Logger.getLogger(EntryUtil.class).error("duplicating entry failed", t); +// } +// return null; +// } - Field field1 = fieldsArray1[i]; - Field field2 = fieldsArray2[i]; - - Object f1 = field1.get(e1);//value of field1 - Object f2 = field2.get(e2);//value of field2 - - List fieldVals = new ArrayList(); - - if(f2 == f1) continue;//same obj or both null, f1 == f2, skip - - if(f2 == null || f1 == null) {//only 1 is null, f1 != f2 - fieldVals.add(f1); - fieldVals.add(f2); - retMap.put(field1, fieldVals); - continue;//next field - }//endif - - if( !f2.equals(f1) ) {//test for arrays - Class f2Class = f2.getClass(); - Class f1Class = f1.getClass(); - if( f2Class.isArray() && f1Class.isArray() ) { - //determine if they are Object or primitive arrays - Class f2Type = f2Class.getComponentType(); - Class f1Type = f1Class.getComponentType(); - - if( !f2Type.equals(f1Type) ) {//field types not equal - fieldVals.add(f1); - fieldVals.add(f2); - retMap.put(field1, fieldVals); - continue;//next field - }//endif - - boolean equalArrays = false; - if( !f2Type.isPrimitive() && !f1Type.isPrimitive() ) { - - //types are equal & both non-primitive, use Object - equalArrays = - Arrays.equals( (Object[])f2, (Object[])f1 ); - - } else if(f2Type.isPrimitive()&&f1Type.isPrimitive()){ - - //types are equal & both primitive, use primitive - if( f2Type.equals(Boolean.TYPE) ) { - equalArrays = Arrays.equals( (boolean[])f2, - (boolean[])f1 ); - } else if( f2Type.equals(Character.TYPE) ) { - equalArrays = Arrays.equals( (char[])f2, - (char[])f1 ); - } else if( f2Type.equals(Byte.TYPE) ) { - equalArrays = Arrays.equals( (byte[])f2, - (byte[])f1 ); - } else if( f2Type.equals(Short.TYPE) ) { - equalArrays = Arrays.equals( (short[])f2, - (short[])f1 ); - } else if( f2Type.equals(Integer.TYPE) ) { - equalArrays = Arrays.equals( (int[])f2, - (int[])f1 ); - } else if( f2Type.equals(Long.TYPE) ) { - equalArrays = Arrays.equals( (long[])f2, - (long[])f1 ); - } else if( f2Type.equals(Float.TYPE) ) { - equalArrays = Arrays.equals( (float[])f2, - (float[])f1 ); - } else if( f2Type.equals(Double.TYPE) ) { - equalArrays = Arrays.equals( (double[])f2, - (double[])f1 ); - }//endif - - //else 1 primitive, 1 not primitive ==> !equalArrays - - }//endif - - if( !equalArrays ) {//f1 not equal f2 - fieldVals.add(f1); - fieldVals.add(f2); - retMap.put(field1, fieldVals); - continue;//next field - }//endif - - continue;//f1 equals f2, skip it and go to next field - - }//endif( f2Class.isArray() && f1Class.isArray() ) - - //(not arrays && !f2.equals(f1)) - fieldVals.add(f1); - fieldVals.add(f2); - retMap.put(field1, fieldVals); - - }//endif( !f2.equals(f1) ) - - }//end loop - - return retMap; - - } catch (IllegalAccessException e) { - // should never happen, all entry fields are public - throw new AssertionError(e); - } - } - - /** - * Convenience method that, given two <code>Entry</code> instances, - * copies the values of the <i>usable</i> (<code>public</code>, - * non-<code>transient</code>, non-<code>static</code>, - * non-<code>final</code>) fields of the first <code>Entry</code> - * parameter to the usable fields of the same name to the second given - * <code>Entry</code> parameter. Any usable field from one of the - * parameters that does not have a counterpart in the other parameter - * will be by passed. This method can be useful, for example, when - * converting an <code>AcinionAttribute</code> to a parallel - * <code>AcinionEvent</code> for reporting outside of the system. - * If <code>null</code> is input for either parameter, then - * <code>null</code> is returned. - */ - public static Entry copyEntry(Entry e1, Entry e2, Logger logger) { - if (e1 == null || e2 == null) return null; - Entry retEntry = e2; - String e1Name = (e1.getClass()).getSimpleName(); - String retEntryName = (retEntry.getClass()).getSimpleName(); - - Field[] e1Fields = EntryUtil.getFieldInfo(e1); - Field[] retEntryFields = EntryUtil.getFieldInfo(retEntry); - if( e1Fields == null || e1Fields.length == 0 ) return null; - if( retEntryFields == null || retEntryFields.length == 0 ) return null; - for(int i=0; i<e1Fields.length; i++) { - Field e1Field = e1Fields[i]; - String e1FieldName = e1Field.getName(); - for(int j=0; j<retEntryFields.length; j++) { - Field retEntryField = retEntryFields[j]; - String retEntryFieldName = retEntryField.getName(); - String errStr = (logger == null ? null : - "cannot set " - +retEntryName+"."+retEntryFieldName - +" to value of " - +e1Name+"."+e1FieldName ); - if( e1FieldName.equals(retEntryFieldName) ) { - try { - retEntryField.set( retEntry, e1Field.get(e1) ); - } catch(IllegalArgumentException e) { - if(logger != null) logger.log(Level.WARN, errStr); - e.printStackTrace(); - } catch(IllegalAccessException e) { - if(logger != null) logger.log(Level.WARN, errStr); - e.printStackTrace(); - } - }//endif - }//end loop(j) - }//end loop(i) - return retEntry; - } - - /** - * Returns an instance of the given <code>Entry</code> class with - * all <i>usable</i> (public, non-static, non-transient, non-final) - * fields set to <code>null</code>. The object returned by this method - * can be used as a template that will match any <code>Entry</code> - * that is of the same type as the <code>Entry</code> input to this - * method. - * - * This method can be useful when working with an <code>Entry</code> - * instance that defines a no-arg constructor that sets one or more - * of its fields to a non-<code>null</code> value. - */ - public static Entry wildcardAll(Entry entry) { - try { - Class realClass = entry.getClass(); - Entry template = (Entry) realClass.newInstance(); - - Field[] f = realClass.getFields(); - for(int i = 0; i < f.length; i++) { - if(! usableField(f[i])) continue; - f[i].set(template, null); - }//end loop - return template; - } catch (Throwable t) { - Logger.getLogger(EntryUtil.class).error("wildcardEntry failed", t); - } - return null; - } - - /** - * Returns a clone of the given {@link Entry}. - * (From com.sun.jini.example.browser.ServiceEditor.) - */ - public static Entry cloneEntry(Entry attr) { - try { - Class realClass = attr.getClass(); - Entry template = (Entry) realClass.newInstance(); - - Field[] f = realClass.getFields(); - for(int i = 0; i < f.length; i++) { - if(! usableField(f[i])) - continue; - f[i].set(template, f[i].get(attr)); - } - - return template; - } catch (Throwable t) { - Logger.getLogger(EntryUtil.class).error("duplicating entry failed", t); - } - return null; - } - - // from EntryRep - private static boolean usableField(Field field) { - Class desc = field.getDeclaringClass(); - - if(desc.isPrimitive()) { - throw new IllegalArgumentException( - "Primitive types not allowed in an Entry: " + field.getName() ); - } - - // skip anything that isn't a public per-object mutable field - int mods = field.getModifiers(); - return (0 == (mods & (Modifier.TRANSIENT | Modifier.STATIC | Modifier.FINAL))); - } +// // from EntryRep +// private static boolean usableField(Field field) { +// Class desc = field.getDeclaringClass(); +// +// if(desc.isPrimitive()) { +// throw new IllegalArgumentException( +// "Primitive types not allowed in an Entry: " + field.getName() ); +// } +// +// // skip anything that isn't a public per-object mutable field +// int mods = field.getModifiers(); +// return (0 == (mods & (Modifier.TRANSIENT | Modifier.STATIC | Modifier.FINAL))); +// } } Modified: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java 2010-09-27 13:39:12 UTC (rev 3633) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestAll.java 2010-09-27 13:44:37 UTC (rev 3634) @@ -75,6 +75,7 @@ suite.addTestSuite( TestCSVReader.class ); suite.addTestSuite( TestBootStateUtil.class ); + suite.addTestSuite( TestEntryUtil.class ); return suite; } Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/TestEntryUtil.java 2010-09-27 13:44:37 UTC (rev 3634) @@ -0,0 +1,313 @@ +package com.bigdata.util; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import net.jini.core.entry.Entry; +import net.jini.entry.AbstractEntry; +import net.jini.lookup.entry.Address; +import net.jini.lookup.entry.Comment; +import net.jini.lookup.entry.Location; +import net.jini.lookup.entry.Name; +import net.jini.lookup.entry.ServiceType; +import junit.framework.TestCase; + +public class TestEntryUtil extends TestCase { + + public void testGetEntryByType_with_null_args() + throws SecurityException, NoSuchMethodException, + IllegalArgumentException, IllegalAccessException, InvocationTargetException + { + Object[][] cmdLines = { + {null, null}, + {null, Name.class}, + {new Entry[] {}, null} + }; + for (Object[] args: cmdLines) { + @SuppressWarnings("unchecked") + Object r = EntryUtil.getEntryByType( + (Entry[])args[0], (Class<? extends Entry>)args[1]); + assertNull(r); + } + } + + public void testGetEntryByType_with_emtpy_args() + throws SecurityException, NoSuchMethodException, + IllegalArgumentException, IllegalAccessException, InvocationTargetException + { + Name t = EntryUtil.getEntryByType(new Entry[]{}, Name.class); + assertNull(t); + } + + public void testGetEntryByType_no_match() + throws SecurityException, NoSuchMethodException, + IllegalArgumentException, IllegalAccessException, InvocationTargetException + { + Entry[] entries = new Entry[] { + new Address(), + new Comment(), + new Location() + }; + Name t = EntryUtil.getEntryByType(entries, Name.class); + assertNull(t); + } + + public void testGetEntryByType_match() + throws SecurityException, NoSuchMethodException, + IllegalArgumentException, IllegalAccessException, InvocationTargetException + { + Entry[] entries = new Entry[] { + new Address(), + new Comment(), + new Location() + }; + Location t = EntryUtil.getEntryByType(entries, Location.class); + assertNotNull(t); + } + + public void testDisplayEntryEntryLogger() { + EntryUtil.displayEntry( + new Address(), + getLevelLogger(Level.DEBUG)); + } + + public void testDisplayEntryEntryStringLogger() { + EntryUtil.displayEntry( + new Location(), + "Label", + getLevelLogger(Level.DEBUG)); + } + + public void testDisplayEntryStringEntryStringLogger() { + EntryUtil.displayEntry( + "Prefix", + new Comment("This is a comment."), + "Label", + getLevelLogger(Level.DEBUG)); + } + + public void testDisplayEntryStringEntryStringLogger_null() { + EntryUtil.displayEntry( + null, + null, + null, + getLevelLogger(Level.DEBUG)); + } + + private static void assertNotEquivalentEntries(Entry entry1, Entry entry2) { + assertFalse( + EntryUtil.compareEntries( + entry1, + entry2, + getLevelLogger(Level.TRACE))); + } + + public void testCompareEntries_not_equal_null() { + Entry entry1 = null; + Entry entry2 = new Name(); + assertNotEquivalentEntries(entry1, entry2); + assertNotEquivalentEntries(entry2, entry1); + } + + public void testCompareEntries_not_equal_diff_type() { + Entry entry1 = new Name(); + Entry entry2 = new Address(); + assertNotEquivalentEntries(entry1, entry2); + assertNotEquivalentEntries(entry2, entry1); + } + + public void testCompareEntries_not_equal_diff_content() { + Entry entry1 = new Name("Name1"); + Entry entry2 = new Name("Name2"); + assertNotEquivalentEntries(entry1, entry2); + assertNotEquivalentEntries(entry2, entry1); + } + + private static void assertEquivalentEntries(Entry entry1, Entry entry2) { + assertTrue( + EntryUtil.compareEntries( + entry1, + entry2, + getLevelLogger(Level.TRACE))); + } + + public void testCompareEntries_equal_null() { + Entry entry1 = null; + Entry entry2 = null; + assertEquivalentEntries(entry1, entry2); + assertEquivalentEntries(entry2, entry1); + } + + public void testCompareEntries_equal_same_content() { + Entry entry1 = new Name("Name1"); + Entry entry2 = new Name("Name1"); + assertEquivalentEntries(entry1, entry2); + assertEquivalentEntries(entry2, entry1); + } + + public void testCompareEntrySets_equiv_null() { + Entry[] entries1 = null; + Entry[] entries2 = null; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_equiv_empty() { + Entry[] entries1 = new Entry[] {}; + Entry[] entries2 = new Entry[] {}; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_equiv_non_empty_singleton() { + Entry[] entries1 = new Entry[] {new Address()}; + Entry[] entries2 = new Entry[] {new Address()}; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_equiv_non_empty_mulitple() { + Entry[] entries1 = new Entry[] {new Address(), new Name(), new Location()}; + Entry[] entries2 = new Entry[] {new Address(), new Name(), new Location()}; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_equiv_non_empty_mulitple_and_dups() { + Entry[] entries1 = + new Entry[] {new Address(), new Name(), new Location(), + new Location(), new Name()}; + Entry[] entries2 = + new Entry[] {new Address(), new Name(), new Location(), + new Address()}; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + private static void assertEquivalentSets(Entry[] entries1, Entry[] entries2) { + assertTrue( + EntryUtil.compareEntrySets("Equivalent", + entries1, + entries2, + getLevelLogger(Level.TRACE))); + } + + private static void assertNotEquivalentSets(Entry[] entries1, Entry[] entries2) { + assertFalse( + EntryUtil.compareEntrySets("Not equivalent", + entries1, + entries2, + getLevelLogger(Level.TRACE))); + } + + public void testCompareEntrySets_unequiv_null() { + Entry[] entries1 = null; + Entry[] entries2 = new Entry[] {}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_non_empty_singleton() { + Entry[] entries1 = new Entry[] {new Comment("C1")}; + Entry[] entries2 = new Entry[] {new Comment("C2")}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_non_empty_diff_size() { + Entry[] entries1 = new Entry[] {new Comment("C1")}; + Entry[] entries2 = new Entry[] {new Comment("C2"), new Comment("C3")}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public static class MyServiceType extends ServiceType { + private static final long serialVersionUID = 1L; + //Only public, non-transient/final/static are used for Entry objs + public String name = null; + public String desc = null; + + public MyServiceType() {} //default cons per spec + + public MyServiceType(String name, String desc) { + super(); + this.name = name; + this.desc = desc; + } + + @Override + public String getDisplayName() { + return name; + } + + @Override + public String getShortDescription() { + return desc; + } + } + + public void testCompareEntrySets_equiv_serviceType() { + Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; + Entry[] entries2 = new Entry[] {new MyServiceType("A", "B")}; + assertEquivalentSets(entries1, entries2); + assertEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_serviceType() { + Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; + Entry[] entries2 = new Entry[] {new MyServiceType("A", "C")}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_serviceType2() { + Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; + Entry[] entries2 = new Entry[] {new MyServiceType("D", "B")}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_serviceType_null1() { + Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; + Entry[] entries2 = new Entry[] {new MyServiceType(null, "B")}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + public void testCompareEntrySets_unequiv_serviceType_null2() { + Entry[] entries1 = new Entry[] {new MyServiceType("A", "B")}; + Entry[] entries2 = new Entry[] {new MyServiceType("A", null)}; + assertNotEquivalentSets(entries1, entries2); + assertNotEquivalentSets(entries2, entries1); + } + + private static class MyEntryWithUnusableFields extends AbstractEntry { + private static final long serialVersionUID = 1L; // final, static, excluded + public final String finalString = "finalString"; // final, excluded + public transient String transientString = "transientString"; // trans, excluded + public static String staticString = "staticString"; // static, excluded + private String privateString = "privateString"; // private, excluded + public String publicString = "publicString"; // included + } + + public void testGetFieldInfo() { + MyEntryWithUnusableFields mf = new MyEntryWithUnusableFields(); + Field[] fields = EntryUtil.getFieldInfo(mf); + assertTrue(fields.length==1); + assertTrue(fields[0].getName().equals("publicString")); + } + + private static Logger getLevelLogger(Level level) { + Logger logger = getLogger(); + logger.setLevel(level); + return logger; + } + private static Logger getLogger() { + return Logger.getLogger(TestEntryUtil.class); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |