001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.gl.util;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.text.DateFormat;
023: import java.text.SimpleDateFormat;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.Date;
027: import java.util.List;
028:
029: /**
030: * Utilities for helping set up and run GL tests, mostly giving GL unit test writers the ability
031: * to load flat files of String-formatted origin entries from the classpath
032: */
033: public class GeneralLedgerTestHelper {
034:
035: /**
036: * Loads a file of String-formatted Origin Entries from the class path
037: *
038: * @param nameOfOutputOriginEntryFileFromFis the name of the file to load
039: * @return a List of origin entries
040: * @throws IOException thrown if the file cannot be read
041: */
042: static public List loadOutputOriginEntriesFromClasspath(
043: String nameOfOutputOriginEntryFileFromFis)
044: throws IOException {
045: return loadOutputOriginEntriesFromClasspath(
046: nameOfOutputOriginEntryFileFromFis, null);
047: }
048:
049: /**
050: * This method differs from OriginEntryServiceImpl.loadFlatFile in that it loads a file using a classloader instead of loading a
051: * file from an absolute file path. This allows and in fact requires that the file from which the entries will be loaded be
052: * checked into the source repository along with this test.
053: *
054: * @param nameOfOutputOriginEntryFileFromFis the name of the file to load
055: * @return a List of origin entries
056: * @throws IOException thrown if the file cannot be read
057: */
058: static public List loadOutputOriginEntriesFromClasspath(
059: String nameOfOutputOriginEntryFileFromFis, Date date)
060: throws IOException {
061: ClassLoader classLoader = Thread.currentThread()
062: .getContextClassLoader();
063: InputStream inputStream = classLoader
064: .getResourceAsStream(nameOfOutputOriginEntryFileFromFis);
065:
066: BufferedReader reader = new BufferedReader(
067: new InputStreamReader(inputStream));
068:
069: List expectedOutputOriginEntries = new ArrayList();
070: String line = null;
071: DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
072: char[] dateChars = null == date ? null : dateFormat
073: .format(date).toCharArray();
074:
075: while (null != (line = reader.readLine())) {
076: // correct for differences in line format
077: char[] lineChars = new char[173];
078: Arrays.fill(lineChars, ' ');
079: char[] lineA = line.toCharArray();
080: System.arraycopy(lineA, 0, lineChars, 0, lineA.length);
081: // converts the + at pos 91 to a space
082: lineChars[91] = ' ';
083: int idx = 92;
084: do {
085: // converts leading 0 characters for amount to spaces
086: if ('0' != lineChars[idx]) {
087: break;
088: } else {
089: lineChars[idx] = ' ';
090: }
091: } while (103 > idx++);
092:
093: if (null != date) { // splice in the date
094:
095: for (int i = 0; i < dateChars.length; i++) {
096: lineChars[109 + i] = dateChars[i];
097: }
098:
099: }
100:
101: String lin2 = new String(lineChars);
102:
103: // KULRNE-34: in FIS, doc numbers were 9 characters long, and in Kuali, they are 14
104: // it means we need to add 5 spaces after the doc number
105: // first char after doc number is at array pos 46
106:
107: String prefix = lin2.substring(0, 46);
108: String suffix = lin2.substring(46);
109:
110: lin2 = prefix + " " + suffix;
111:
112: // now we add 5 characters to reference doc number
113: // reference doc number ends 11 spaces from end (see OriginEntryFull.java)
114: prefix = lin2.substring(0, lin2.length() - 11);
115: suffix = lin2.substring(lin2.length() - 11);
116:
117: lin2 = prefix + " " + suffix;
118:
119: // the string is too long, so we truncate 10 characters off from it
120: lin2 = lin2.substring(0, lin2.length() - 10);
121:
122: expectedOutputOriginEntries.add(lin2);
123: }
124:
125: return expectedOutputOriginEntries;
126: }
127:
128: }
|