001 /*
002 * Created on Nov 19, 2003
003 *
004 * To change the template for this generated file go to
005 * Window>Preferences>Java>Code Generation>Code and Comments
006 */
007 package calhoun.util;
008
009 import java.beans.PropertyDescriptor;
010 import java.util.Collection;
011 import java.util.HashSet;
012 import java.util.List;
013 import java.util.Map;
014 import java.util.Set;
015
016 import junit.framework.TestCase;
017
018 import org.apache.commons.logging.Log;
019 import org.apache.commons.logging.LogFactory;
020
021 /** Basic test case with file matching (XML and regular) added as well as improved logging for errors.
022 */
023 public abstract class AbstractTestCase extends TestCase {
024 private static final Log log = LogFactory.getLog(AbstractTestCase.class);
025
026 public AbstractTestCase() {
027 }
028
029 public AbstractTestCase(String arg0) {
030 super(arg0);
031 }
032
033 /** Overrides the default runTest to provide error logging. Using this runBare logs setUp and tearDown errors.
034 *
035 */
036 @Override
037 public void runBare() throws Throwable {
038 try {
039 log.info("Running Test: " + this.getClass().getName() + " " + getName());
040 setUp();
041 } catch (Throwable ex) {
042 log.error(getName() + ": ", ex);
043 throw ex;
044 }
045 try {
046 runTest();
047 } catch (Throwable ex) {
048 log.error(getName() + ": ", ex);
049 throw ex;
050 } finally {
051 tearDown();
052 }
053 }
054
055
056 private void failCompare(String name, Object expect, Object actual) throws Exception {
057 fail("Property compare failed on " + name + ". Expected: " + expect + " Actual: " + actual);
058 }
059
060 private static Set basicTypes = new HashSet();
061 static {
062 basicTypes.add(String.class);
063 basicTypes.add(Byte.class);
064 basicTypes.add(Byte.TYPE);
065 basicTypes.add(Boolean.class);
066 basicTypes.add(Boolean.TYPE);
067 basicTypes.add(Short.class);
068 basicTypes.add(Short.TYPE);
069 basicTypes.add(Integer.class);
070 basicTypes.add(Integer.TYPE);
071 basicTypes.add(Long.class);
072 basicTypes.add(Long.TYPE);
073 basicTypes.add(Character.class);
074 basicTypes.add(Character.TYPE);
075 basicTypes.add(Double.class);
076 basicTypes.add(Double.TYPE);
077 basicTypes.add(Float.class);
078 basicTypes.add(Float.TYPE);
079 }
080
081 public void assertXmlFilesMatch(String template, String actual) throws Exception {
082 String result = XmlDiff.compareFiles(template, actual);
083 log.debug("Result: "+result);
084 if (result != null) {
085 log.warn(result);
086 fail();
087 }
088 }
089
090 public void assertArrayEquals(byte[] a, byte[] b) {
091 for(int i=0;i<a.length;i++) {
092 assertEquals(a[i], b[i]);
093 }
094 }
095
096 public void assertArrayEquals(double[] a, double[] b) {
097 for(int i=0;i<a.length;i++) {
098 assertEquals(a[i], b[i]);
099 }
100 }
101
102 public void assertArrayEquals(double[] a, double[] b, double tolerance) {
103 for(int i=0;i<a.length;i++) {
104 assertEquals(a[i], b[i], tolerance);
105 }
106 }
107
108 public void assertArrayEquals(int[] a, int[] b) {
109 for(int i=0;i<a.length;i++) {
110 assertEquals(a[i], b[i]);
111 }
112 }
113
114 public void assertBeginsWith(byte[] a, byte[] b) {
115 for(int i=0;i<a.length;i++) {
116 assertEquals(a[i], b[i]);
117 }
118 }
119
120 public void assertEndsWith(byte[] a, byte[] b) {
121 for(int i=0;i<a.length;i++) {
122 assertEquals(a[i], b[b.length-a.length+i]);
123 }
124 }
125
126 public void assertEquals(byte[] a, byte[] b) {
127 assertEquals(a.length, b.length);
128 for(int i=0;i<a.length;i++) {
129 assertEquals(a[i], b[i]);
130 }
131 }
132
133 public void assertStringEquals(String [] expected, List l) {
134 assertStringEquals(expected, (String[])l.toArray(new String[l.size()]));
135 }
136
137 public void assertStringEquals(String [] expected, String[] strings) {
138 for (int i = 0; i < strings.length; ++i) {
139 if (expected.length > i) {
140 assertEquals(expected[i], strings[i]);
141 }
142 }
143 assertEquals(expected.length, strings.length);
144 }
145
146 public void assertFilesMatch(String expected, String actual) throws Exception {
147 FileDiff diff = new FileDiff(expected, actual);
148 boolean mismatch = diff.execute();
149 assertFalse(diff.toString(), mismatch);
150 }
151
152 public void assertFlatFileRecordsMatch(String expected, String actual) throws Exception {
153 String s = FileUtil.fileRecordCompare(expected, actual);
154 assertNull(s, s);
155 }
156 }