1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared.config; 20 21 import org.apache.beehive.netui.tools.testrecorder.shared.util.StringHelper; 22 23 import java.util.HashMap ; 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import java.util.List ; 27 import java.util.Iterator ; 28 import java.util.Collections ; 29 30 import org.apache.beehive.netui.tools.testrecorder.shared.Logger; 31 32 33 38 public class Categories { 39 40 private static final Logger log = Logger.getInstance( Categories.class ); 41 42 private Category[] categories; 43 private HashMap categoryStringMap; 45 private HashMap categoryTestMap; 47 48 public Categories( Category[] categories ) { 49 this.categories = categories; 50 categoryStringMap = new HashMap (); 51 categoryTestMap = new HashMap (); 52 init(); 53 } 54 55 private void init() { 56 for ( int i = 0; i < categories.length; i++ ) { 57 categoryStringMap.put( categories[i].getName(), categories[i] ); 58 categoryTestMap.put( categories[i], new ArrayList () ); 59 } 60 } 61 62 public Category[] getCategories() { 63 return categories; 64 } 65 66 public Category getCategory( String categoryString ) { 67 return (Category) getCategoryStringMap().get( categoryString ); 68 } 69 70 private Map getCategoryStringMap() { 71 return categoryStringMap; 72 } 73 74 public List getTests( String category ) { 75 assert category != null : "ERROR: category string may not be null"; 76 return getTests( getCategory( category ) ); 77 } 78 79 84 public List getTests( Category category ) { 85 List list = (List ) getCategoryTestMap().get( category ); 86 if ( list == null ) { 87 return null; 88 } 89 return Collections.unmodifiableList( list); 90 } 91 92 private Map getCategoryTestMap() { 93 return categoryTestMap; 94 } 95 96 public void addTest( TestDefinition test ) throws ConfigException { 97 List testCategories = test.getCategories(); 98 Category category = null; 99 for ( int i = 0; i < testCategories.size(); i++ ) { 100 category = (Category) testCategories.get( i ); 101 addTest( category, test ); 102 } 103 } 104 105 private void addTest( Category category, TestDefinition test ) throws ConfigException { 106 List list = null; 107 if ( !getCategoryTestMap().containsKey( category ) ) { 108 ConfigException ce = new ConfigException( "ERROR: unable to find category( " + category + " )" ); 109 log.fatal( ce ); 110 throw ce; 111 } 112 list = (List ) getCategoryTestMap().get( category ); 113 list.add( test ); 114 } 115 116 public String toString() { 117 StringBuffer sb = new StringBuffer ( 64 ); 118 sb.append( "[ " ); 119 sb.append( "categories( \n" + StringHelper.toString( getCategories(), "\n\t", "\n\t" ) + " ), \n" ); 120 sb.append( "testMap( \n" + testMapToString() + " )" ); 121 sb.append( " ]" ); 122 return sb.toString(); 123 } 124 125 public String testMapToString() { 126 Map map = getCategoryTestMap(); 127 Iterator it = map.keySet().iterator(); 128 StringBuffer sb = new StringBuffer ( 16 * map.size() ); 129 Category category = null; 130 List testList = null; 131 sb.append( "[\n" ); 132 for ( int i = 0; it.hasNext(); i++ ) { 133 category = (Category) it.next(); 134 testList = (List ) map.get( category ); 135 if ( i != 0 ) { 136 sb.append( "\n" ); 137 } 138 sb.append( "\t[" + i + "] key(" + category.getName() + "), value( " + 139 TestDefinition.testListToString( testList ) + " )" ); 140 } 141 sb.append( "\n]" ); 142 return sb.toString(); 143 } 144 145 } 146 | Popular Tags |