KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > RepositoryElementsTest


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 JavaDoc;
8 import java.util.Arrays JavaDoc;
9
10 /**
11  * @author Virender Dogra
12  * @version Apr 16, 2003 6:14:56 PM
13  *
14  */

15 public class RepositoryElementsTest extends TestCase
16 {
17
18     /**
19      * Constructor for RespositoryElementsTest.
20      * @param name
21      */

22     public RepositoryElementsTest(String JavaDoc name)
23     {
24         super(name);
25     }
26
27     public static void main(String JavaDoc[] args)
28     {
29         junit.textui.TestRunner.run(RepositoryElementsTest.suite());
30     }
31
32     public static Test suite()
33     {
34         // Reflection is used here to add all
35
// the testXXX() methods to the suite.
36
TestSuite suite = new TestSuite(RepositoryElementsTest.class);
37         return suite;
38
39     }
40
41     /*
42      * @see TestCase#setUp()
43      */

44     protected void setUp() throws Exception JavaDoc
45     {
46         super.setUp();
47     }
48
49     /*
50      * @see TestCase#tearDown()
51      */

52     protected void tearDown() throws Exception JavaDoc
53     {
54         super.tearDown();
55     }
56
57     /**
58      * Method testForDuplicateElements.
59      *
60      * This test is to check that there are no constants with the same values. This
61      * method collates the required data and then calls another resuable method to do the
62      * final checking
63      */

64
65     public void testForDuplicateElements() throws Exception JavaDoc
66     {
67
68         Class JavaDoc c = RepositoryElements.class;
69         Field JavaDoc[] fields = c.getDeclaredFields();
70
71         // make a string array of all the field values
72
String JavaDoc[] fieldvalues = new String JavaDoc[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 JavaDoc e)
81             {
82                 System.out.println(e);
83                 throw e;
84             }
85             catch (NoSuchFieldException JavaDoc 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             // Constants with similar values found
102
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 JavaDoc fieldValuesToString(String JavaDoc[] fieldvalues)
112     {
113         StringBuffer JavaDoc result = new StringBuffer JavaDoc(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     /**
124      * Method checkForDuplicateConstant.: This method checks for duplicate constant
125      * values
126      * @param fieldvalues
127      * @throws DuplicateRepositoryElementsFound
128      */

129     private void checkForDuplicateConstant(String JavaDoc[] 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 /**
145  * This exception is for the case when constants with
146  * similar values are found in the RepositoryElements class
147  */

148
149 class DuplicateRepositoryElementsFound extends Exception JavaDoc
150 {
151
152
153     /* (non-Javadoc)
154      * @see java.lang.Throwable#Throwable(java.lang.String)
155      */

156     public DuplicateRepositoryElementsFound(String JavaDoc errorMsg)
157     {
158         super("Duplicate value of " + errorMsg + " found");
159     }
160 }
161
Popular Tags