1 package org.apache.ojb.broker.metadata; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 import java.lang.reflect.Field ; 8 import java.util.Arrays ; 9 10 15 public class RepositoryElementsTest extends TestCase 16 { 17 18 22 public RepositoryElementsTest(String name) 23 { 24 super(name); 25 } 26 27 public static void main(String [] args) 28 { 29 junit.textui.TestRunner.run(RepositoryElementsTest.suite()); 30 } 31 32 public static Test suite() 33 { 34 TestSuite suite = new TestSuite(RepositoryElementsTest.class); 37 return suite; 38 39 } 40 41 44 protected void setUp() throws Exception 45 { 46 super.setUp(); 47 } 48 49 52 protected void tearDown() throws Exception 53 { 54 super.tearDown(); 55 } 56 57 64 65 public void testForDuplicateElements() throws Exception 66 { 67 68 Class c = RepositoryElements.class; 69 Field [] fields = c.getDeclaredFields(); 70 71 String [] fieldvalues = new String [fields.length]; 73 74 for (int i = 0; i < fields.length; i++) 75 { 76 try 77 { 78 fieldvalues[i] = c.getDeclaredField(fields[i].getName()).get(fields[i]).toString(); 79 } 80 catch (IllegalAccessException e) 81 { 82 System.out.println(e); 83 throw e; 84 } 85 catch (NoSuchFieldException e) 86 { 87 System.out.println("No such field " + fields[i] + " " + e); 88 throw e; 89 } 90 } 91 92 Arrays.sort(fieldvalues); 93 94 try 95 { 96 checkForDuplicateConstant(fieldvalues); 97 assertTrue(true); 98 } 99 catch (DuplicateRepositoryElementsFound e) 100 { 101 fail( 103 e.getMessage() 104 + "\n All the constants values in string sort order that i read are -> \n" 105 + fieldValuesToString(fieldvalues)); 106 107 } 108 109 } 110 111 private String fieldValuesToString(String [] fieldvalues) 112 { 113 StringBuffer result = new StringBuffer (100); 114 115 for (int i = 0; i < fieldvalues.length; i++) 116 { 117 result.append(fieldvalues[i].toString()).append(','); 118 } 119 120 return result.substring(0, ((result.length()) - 1)); 121 } 122 123 129 private void checkForDuplicateConstant(String [] fieldvalues) 130 throws DuplicateRepositoryElementsFound 131 { 132 for (int i = 1; i < fieldvalues.length; i++) 133 { 134 if (fieldvalues[i - 1].equals(fieldvalues[i])) 135 { 136 throw new DuplicateRepositoryElementsFound(fieldvalues[i]); 137 } 138 } 139 } 140 141 } 142 143 144 148 149 class DuplicateRepositoryElementsFound extends Exception 150 { 151 152 153 156 public DuplicateRepositoryElementsFound(String errorMsg) 157 { 158 super("Duplicate value of " + errorMsg + " found"); 159 } 160 } 161 | Popular Tags |