001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.net.ftp.parser; 018 import junit.framework.TestCase; 019 020 import java.text.SimpleDateFormat; 021 import java.util.Locale; 022 import org.apache.commons.net.ftp.FTPFile; 023 import org.apache.commons.net.ftp.FTPFileEntryParser; 024 025 /** 026 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> 027 * @version $Id: FTPParseTestFramework.java 437134 2006-08-26 09:36:36Z rwinston $ 028 */ 029 public abstract class FTPParseTestFramework extends TestCase 030 { 031 private FTPFileEntryParser parser = null; 032 protected SimpleDateFormat df = null; 033 034 /** 035 * @see junit.framework.TestCase#TestCase(String) 036 */ 037 public FTPParseTestFramework(String name) 038 { 039 super(name); 040 } 041 042 /** 043 * Method testBadListing. 044 * Tests that parser provided failures actually fail. 045 * @throws Exception 046 */ 047 public void testBadListing() throws Exception 048 { 049 050 String[] badsamples = getBadListing(); 051 for (int i = 0; i < badsamples.length; i++) 052 { 053 054 String test = badsamples[i]; 055 FTPFile f = parser.parseFTPEntry(test); 056 assertNull("Should have Failed to parse " + test, 057 nullFileOrNullDate(f)); 058 059 doAdditionalBadTests(test, f); 060 } 061 } 062 063 /** 064 * Method testGoodListing. 065 * Test that parser provided listings pass. 066 * @throws Exception 067 */ 068 public void testGoodListing() throws Exception 069 { 070 071 String[] goodsamples = getGoodListing(); 072 for (int i = 0; i < goodsamples.length; i++) 073 { 074 075 String test = goodsamples[i]; 076 FTPFile f = parser.parseFTPEntry(test); 077 assertNotNull("Failed to parse " + test, 078 f); 079 080 doAdditionalGoodTests(test, f); 081 } 082 } 083 084 /** 085 * during processing you could hook here to do additional tests 086 * 087 * @param test raw entry 088 * @param f parsed entry 089 */ 090 protected void doAdditionalGoodTests(String test, FTPFile f) 091 { 092 } 093 094 /** 095 * during processing you could hook here to do additional tests 096 * 097 * @param test raw entry 098 * @param f parsed entry 099 */ 100 protected void doAdditionalBadTests(String test, FTPFile f) 101 { 102 } 103 104 /** 105 * Method getBadListing. 106 * Implementors must provide a listing that contains failures. 107 * @return String[] 108 */ 109 protected abstract String[] getBadListing(); 110 111 /** 112 * Method getGoodListing. 113 * Implementors must provide a listing that passes. 114 * @return String[] 115 */ 116 protected abstract String[] getGoodListing(); 117 118 /** 119 * Method getParser. 120 * Provide the parser to use for testing. 121 * @return FTPFileEntryParser 122 */ 123 protected abstract FTPFileEntryParser getParser(); 124 125 /** 126 * Method testParseFieldsOnDirectory. 127 * Provide a test to show that fields on a directory entry are parsed correctly. 128 * @throws Exception 129 */ 130 public abstract void testParseFieldsOnDirectory() throws Exception; 131 132 /** 133 * Method testParseFieldsOnFile. 134 * Provide a test to show that fields on a file entry are parsed correctly. 135 * @throws Exception 136 */ 137 public abstract void testParseFieldsOnFile() throws Exception; 138 139 /** 140 * @see junit.framework.TestCase#setUp() 141 */ 142 @Override 143 protected void setUp() throws Exception 144 { 145 super.setUp(); 146 parser = getParser(); 147 df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US); 148 } 149 150 /** 151 * Check if FTPFile entry parsing failed; i.e. if entry is null or date is null. 152 * 153 * @param f FTPFile entry - may be null 154 * @return null if f is null or the date is null 155 */ 156 protected FTPFile nullFileOrNullDate(FTPFile f) { 157 if (f==null){ 158 return null; 159 } 160 if (f.getTimestamp() == null){ 161 return null; 162 } 163 return f; 164 } 165 }