001: package com.ibm.emb.junit;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.text.SimpleDateFormat;
007:
008: import javax.emb.MediaException;
009:
010: import junit.framework.TestCase;
011:
012: /**
013: * This class extends junit.TestCase and is the base class for all EMB test
014: * classes, allowing for easier implementations of future extensions. Error,
015: * test, and tracing routines are provided along with the EMBTestConfiguration
016: * singleton, which provides access to configurable properties needed to run the
017: * test cases.
018: */
019: public class EMBTestCaseBase extends TestCase {
020:
021: protected EMBTestConfiguration embTestConfig = null;
022:
023: private static String MEBTESTER_JNDI = "java:comp/env/ejb/MediaEntityLocalTestDriverHome";
024:
025: private static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss:SS";
026:
027: /**
028: * Constructor for EMBTestCaseBase
029: */
030: public EMBTestCaseBase(String name) throws MediaException {
031: super (name);
032: // always retrieve the EMBTestConfiguration singleton
033: embTestConfig = EMBTestConfiguration.instance();
034: File mediaDir = new File(embTestConfig.getMediaDir());
035: if (!mediaDir.exists() || !mediaDir.isDirectory()) {
036: throw new MediaException(
037: "Media directory in EMBTestConfiguration property does not exit");
038: }
039: }
040:
041: /**
042: * initialization of test cases
043: */
044: protected void setUp() throws Exception {
045: }
046:
047: /**
048: * freeup resources
049: */
050: protected void tearDown() throws Exception {
051: removeFilesInDir(new File(embTestConfig.getMediaDir()
052: + File.separatorChar + "Generated"));
053: removeFilesInDir(new File(embTestConfig.getMediaDir()
054: + File.separatorChar + "export"));
055:
056: //
057: // try {
058: // InitialContext ic = new InitialContext();
059: // Object ojb = ic.lookup(MEBTESTER_JNDI);
060: // mebHome = (MediaEntityLocalTestDriverHome) PortableRemoteObject.narrow(ojb,
061: // MediaEntityLocalTestDriverHome.class);
062: // } catch (Exception e) {
063: // System.out.println("Exception while cleaning : "+e.getLocalizedMessage());
064: // }
065: // }
066: // // create new instance of MediaEntityLocalTestDriver
067: // mebTester = mebHome.create();
068: }
069:
070: /**
071: * Utility function used to write logging information to system out and to a
072: * log file.
073: */
074: public void testLog(String msg) {
075: SimpleDateFormat df = new SimpleDateFormat(TIMESTAMP_PATTERN);
076: String timeString = df.format(new java.util.Date());
077: String message = "[" + timeString + "][" + this .getName()
078: + "][LOG][" + this .getClass().getName() + "] :" + msg;
079: System.out.println(message);
080: if (embTestConfig.getWriteLog()) {
081: try {
082: embTestConfig.getLogWriter().write(message);
083: embTestConfig.getLogWriter().newLine();
084: embTestConfig.getLogWriter().flush();
085: } catch (java.io.IOException e) {
086: e.printStackTrace();
087: }
088: }
089: }
090:
091: /**
092: * Utility function called at the end of a succeeded testcase Event is then
093: * written to the test case log file an aditional message can be appended
094: */
095: public void succeed(String msg) {
096: SimpleDateFormat df = new SimpleDateFormat(TIMESTAMP_PATTERN);
097: String timeString = df.format(new java.util.Date());
098: String message = "[" + timeString + "][" + this .getName()
099: + "][SUCCEEDED][" + this .getClass().getName() + "] :"
100: + msg;
101: System.out.println(message);
102: if (embTestConfig.getWriteLog()) {
103: try {
104: embTestConfig.getLogWriter().write(message);
105: embTestConfig.getLogWriter().newLine();
106: embTestConfig.getLogWriter().flush();
107: } catch (java.io.IOException e) {
108: e.printStackTrace();
109: }
110: }
111: }
112:
113: /**
114: * Utility Function called at the end of a succeded testcase Event is then
115: * written to the TestCase log file
116: */
117: public void succeed() {
118: SimpleDateFormat df = new SimpleDateFormat(TIMESTAMP_PATTERN);
119: String timeString = df.format(new java.util.Date());
120: String message = "[" + timeString + "][" + this .getName()
121: + "][SUCCEEDED][" + this .getClass().getName() + "]";
122: System.out.println(message);
123: if (embTestConfig.getWriteLog()) {
124: try {
125: embTestConfig.getLogWriter().write(message);
126: embTestConfig.getLogWriter().newLine();
127: embTestConfig.getLogWriter().flush();
128: } catch (java.io.IOException e) {
129: e.printStackTrace();
130: }
131: }
132: }
133:
134: /**
135: * Utility function used to log a test suite error to system out and to the
136: * log file
137: */
138: public void testError(String msg) {
139: java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
140: TIMESTAMP_PATTERN);
141: String timeString = df.format(new java.util.Date());
142: String message = "[" + timeString + "][ERROR]["
143: + this .getName() + "][" + this .getClass().getName()
144: + "] :" + msg;
145: System.out.println(message);
146: if (embTestConfig.getWriteLog()) {
147: try {
148: embTestConfig.getLogWriter().write(message);
149: embTestConfig.getLogWriter().newLine();
150: embTestConfig.getLogWriter().flush();
151: } catch (java.io.IOException e) {
152: e.printStackTrace();
153: }
154: }
155: }
156:
157: /**
158: * Utility function used to write tracing to system out and to a traceFile.
159: * For the more common logging there is an additional function testLog
160: */
161: public void testTrace(String msg) {
162: SimpleDateFormat df = new SimpleDateFormat(TIMESTAMP_PATTERN);
163: String timeString = df.format(new java.util.Date());
164: String message = "[" + timeString + "]["
165: + this .getName().substring(4) + "][TRACE]["
166: + this .getClass().getName() + "] :"
167: + (msg == null ? "null" : msg);
168: System.out.println(message);
169: if (embTestConfig.getWriteTrace()) {
170: try {
171: embTestConfig.getTraceWriter().write(message);
172: embTestConfig.getTraceWriter().newLine();
173: embTestConfig.getTraceWriter().flush();
174: } catch (java.io.IOException e) {
175: e.printStackTrace();
176: }
177: }
178: }
179:
180: /**
181: * removes files in directory used to clean up after tests
182: */
183: public static void removeFilesInDir(File srcDir) throws IOException {
184: File[] srcFiles = srcDir.listFiles();
185: if (srcFiles == null) {
186: EMBStaticHelper
187: .testTrace("file name does not point to a directory");
188: } else {
189: for (int i = 0; i < srcFiles.length; i++) {
190: if (!(srcFiles[i].isDirectory())) {
191: srcFiles[i].delete();
192: }
193: }
194: }
195: }
196:
197: /**
198: * returns a byte array of the given file's contents
199: * @return Returns a byte[]
200: */
201: public static byte[] getByteArrayFromFile(File arg)
202: throws IOException {
203: FileInputStream fileInputStream = new FileInputStream(arg);
204: byte[] retByteArray = new byte[(int) arg.length()];
205: long inputByte = fileInputStream.read(retByteArray);
206: fileInputStream.close();
207: return retByteArray;
208: }
209:
210: /**
211: * writes a known and reported error to the log if flag is enabled
212: * simplifies tracking of known errors
213: */
214: public void logProductError(String msg) {
215: if (embTestConfig.getLogKnownErrors()) {
216: testLog(msg);
217: }
218: }
219:
220: }
|