View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys;
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  
24  import org.kuali.kfs.gl.GeneralLedgerConstants;
25  import org.kuali.kfs.sys.context.KualiTestBase;
26  import org.kuali.kfs.sys.context.SpringContext;
27  import org.kuali.kfs.sys.context.TestUtils;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  
30  @ConfigureContext
31  public class FileUtilTest extends KualiTestBase {
32      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileUtilTest.class);
33      
34      protected String stagingDirectory;
35      
36      protected String[] DATA = new String[] {
37              "2007BL1031400-----    ---A2EX05BT  01LP2837509     88888------------------TEST DESCRIPTION                                      619.90D2009-02-05                                                                     0.00     200905000000000010                                                      ",
38              "2007BL1031400-----    ---A2EX05BT  01LP2837509     88888------------------TEST DESCRIPTION                                      276.47D2009-02-05                                                                     0.00     200905000000000010                                                      ",
39      };
40      
41      @Override
42      protected void setUp() throws Exception {
43          super.setUp();
44          
45          ConfigurationService kualiConfigurationService = SpringContext.getBean(ConfigurationService.class); 
46          stagingDirectory = kualiConfigurationService.getPropertyValueAsString("staging.directory") + "/";
47      }
48      
49      public void testGetNewestDataFile() {
50          final String testFilename = "testGetNewestDataFile";
51          final String testFilenameUnmatched = "testUmatchedGetNewestDataFile";
52          
53          File batchDirectory = new File(this.stagingDirectory);
54          
55          // Use a filename filter matching testFilename. This shouldn't impact the logic as long as we're consistently testing with this one
56          FilenameFilter filenameFilter = new FilenameFilter() {
57              public boolean accept(File dir, String name) {
58                  return (name.startsWith(testFilename) &&
59                          name.endsWith(GeneralLedgerConstants.BatchFileSystem.EXTENSION));
60              }
61          };
62          
63          assertNull("Shouldn't have found any files of name " + testFilename, FileUtil.getNewestFile(batchDirectory, filenameFilter));
64          
65          try {
66              LOG.debug("Create three test files. Sleeping briefly between each to ensure unique timestamps.");
67              String filePathA = this.stagingDirectory + File.separator + testFilename + "FileA" + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
68              TestUtils.writeFile(filePathA, this.DATA);
69              this.addGeneratedFile(filePathA);
70              Thread.sleep(1000);
71              String filePathB = this.stagingDirectory + File.separator + testFilename + "FileB" + GeneralLedgerConstants.BatchFileSystem.EXTENSION; 
72              TestUtils.writeFile(filePathB, this.DATA);
73              this.addGeneratedFile(filePathB);
74              Thread.sleep(1000);
75              String filePathC = this.stagingDirectory + File.separator + testFilename + "FileC" + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
76              TestUtils.writeFile(filePathC, this.DATA);
77              this.addGeneratedFile(filePathC);
78              Thread.sleep(1000);
79              String filePathUnmatched = this.stagingDirectory + File.separator + testFilenameUnmatched + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
80              TestUtils.writeFile(filePathUnmatched, this.DATA);
81              this.addGeneratedFile(filePathUnmatched);
82          } catch (InterruptedException e) {
83              assertTrue("No reason that this job should have gotten interrupted.", false);
84          }
85          
86          File newestFile = FileUtil.getNewestFile(batchDirectory, filenameFilter);
87          assertNotNull("We just created a few files but none was found, filename=" + testFilename, newestFile);
88          assertTrue("Was expecting last file created. Not the case, found: " + newestFile.getName(), newestFile.getName().contains("FileC"));
89          
90          LOG.debug("Cleanup files after ourselves." + testFilename);
91          File[] directoryListing = batchDirectory.listFiles(filenameFilter);
92          for (int i = 0; i < directoryListing.length; i++) {
93              File file = directoryListing[i];
94              assertTrue("Delete failed. Shouldn't.", file.delete());
95          }
96          assertTrue("Delete failed. Shouldn't.", new File(this.stagingDirectory + File.separator + testFilenameUnmatched + GeneralLedgerConstants.BatchFileSystem.EXTENSION).delete());
97  
98      }
99  }