001: package org.apache.ojb.broker.metadata;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006:
007: import java.lang.reflect.Field;
008: import java.util.Arrays;
009:
010: /**
011: * @author Virender Dogra
012: * @version Apr 16, 2003 6:14:56 PM
013: *
014: */
015: public class RepositoryElementsTest extends TestCase {
016:
017: /**
018: * Constructor for RespositoryElementsTest.
019: * @param name
020: */
021: public RepositoryElementsTest(String name) {
022: super (name);
023: }
024:
025: public static void main(String[] args) {
026: junit.textui.TestRunner.run(RepositoryElementsTest.suite());
027: }
028:
029: public static Test suite() {
030: // Reflection is used here to add all
031: // the testXXX() methods to the suite.
032: TestSuite suite = new TestSuite(RepositoryElementsTest.class);
033: return suite;
034:
035: }
036:
037: /*
038: * @see TestCase#setUp()
039: */
040: protected void setUp() throws Exception {
041: super .setUp();
042: }
043:
044: /*
045: * @see TestCase#tearDown()
046: */
047: protected void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: /**
052: * Method testForDuplicateElements.
053: *
054: * This test is to check that there are no constants with the same values. This
055: * method collates the required data and then calls another resuable method to do the
056: * final checking
057: */
058:
059: public void testForDuplicateElements() throws Exception {
060:
061: Class c = RepositoryElements.class;
062: Field[] fields = c.getDeclaredFields();
063:
064: // make a string array of all the field values
065: String[] fieldvalues = new String[fields.length];
066:
067: for (int i = 0; i < fields.length; i++) {
068: try {
069: fieldvalues[i] = c
070: .getDeclaredField(fields[i].getName()).get(
071: fields[i]).toString();
072: } catch (IllegalAccessException e) {
073: System.out.println(e);
074: throw e;
075: } catch (NoSuchFieldException e) {
076: System.out.println("No such field " + fields[i] + " "
077: + e);
078: throw e;
079: }
080: }
081:
082: Arrays.sort(fieldvalues);
083:
084: try {
085: checkForDuplicateConstant(fieldvalues);
086: assertTrue(true);
087: } catch (DuplicateRepositoryElementsFound e) {
088: // Constants with similar values found
089: fail(e.getMessage()
090: + "\n All the constants values in string sort order that i read are -> \n"
091: + fieldValuesToString(fieldvalues));
092:
093: }
094:
095: }
096:
097: private String fieldValuesToString(String[] fieldvalues) {
098: StringBuffer result = new StringBuffer(100);
099:
100: for (int i = 0; i < fieldvalues.length; i++) {
101: result.append(fieldvalues[i].toString()).append(',');
102: }
103:
104: return result.substring(0, ((result.length()) - 1));
105: }
106:
107: /**
108: * Method checkForDuplicateConstant.: This method checks for duplicate constant
109: * values
110: * @param fieldvalues
111: * @throws DuplicateRepositoryElementsFound
112: */
113: private void checkForDuplicateConstant(String[] fieldvalues)
114: throws DuplicateRepositoryElementsFound {
115: for (int i = 1; i < fieldvalues.length; i++) {
116: if (fieldvalues[i - 1].equals(fieldvalues[i])) {
117: throw new DuplicateRepositoryElementsFound(
118: fieldvalues[i]);
119: }
120: }
121: }
122:
123: }
124:
125: /**
126: * This exception is for the case when constants with
127: * similar values are found in the RepositoryElements class
128: */
129:
130: class DuplicateRepositoryElementsFound extends Exception {
131:
132: /* (non-Javadoc)
133: * @see java.lang.Throwable#Throwable(java.lang.String)
134: */
135: public DuplicateRepositoryElementsFound(String errorMsg) {
136: super ("Duplicate value of " + errorMsg + " found");
137: }
138: }
|