1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.net.ftp.parser; 18 import junit.framework.TestCase; 19 20 import java.text.SimpleDateFormat; 21 import java.util.Locale; 22 import org.apache.commons.net.ftp.FTPFile; 23 import org.apache.commons.net.ftp.FTPFileEntryParser; 24 25 /** 26 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> 27 * @version $Id: FTPParseTestFramework.java 1407341 2012-11-09 01:31:00Z ggregory $ 28 */ 29 public abstract class FTPParseTestFramework extends TestCase 30 { 31 private FTPFileEntryParser parser = null; 32 protected SimpleDateFormat df = null; 33 34 /** 35 * @see junit.framework.TestCase#TestCase(String) 36 */ 37 public FTPParseTestFramework(String name) 38 { 39 super(name); 40 } 41 42 /** 43 * Method testBadListing. 44 * Tests that parser provided failures actually fail. 45 * @throws Exception 46 */ 47 public void testBadListing() throws Exception 48 { 49 50 String[] badsamples = getBadListing(); 51 for (String test : badsamples) 52 { 53 54 FTPFile f = parser.parseFTPEntry(test); 55 assertNull("Should have Failed to parse " + test, 56 nullFileOrNullDate(f)); 57 58 doAdditionalBadTests(test, f); 59 } 60 } 61 62 /** 63 * Method testGoodListing. 64 * Test that parser provided listings pass. 65 * @throws Exception 66 */ 67 public void testGoodListing() throws Exception 68 { 69 70 String[] goodsamples = getGoodListing(); 71 for (String test : goodsamples) 72 { 73 74 FTPFile f = parser.parseFTPEntry(test); 75 assertNotNull("Failed to parse " + test, 76 f); 77 78 doAdditionalGoodTests(test, f); 79 } 80 } 81 82 /** 83 * during processing you could hook here to do additional tests 84 * 85 * @param test raw entry 86 * @param f parsed entry 87 */ 88 protected void doAdditionalGoodTests(String test, FTPFile f) 89 { 90 } 91 92 /** 93 * during processing you could hook here to do additional tests 94 * 95 * @param test raw entry 96 * @param f parsed entry 97 */ 98 protected void doAdditionalBadTests(String test, FTPFile f) 99 { 100 } 101 102 /** 103 * Method getBadListing. 104 * Implementors must provide a listing that contains failures. 105 * @return String[] 106 */ 107 protected abstract String[] getBadListing(); 108 109 /** 110 * Method getGoodListing. 111 * Implementors must provide a listing that passes. 112 * @return String[] 113 */ 114 protected abstract String[] getGoodListing(); 115 116 /** 117 * Method getParser. 118 * Provide the parser to use for testing. 119 * @return FTPFileEntryParser 120 */ 121 protected abstract FTPFileEntryParser getParser(); 122 123 /** 124 * Method testParseFieldsOnDirectory. 125 * Provide a test to show that fields on a directory entry are parsed correctly. 126 * @throws Exception 127 */ 128 public abstract void testParseFieldsOnDirectory() throws Exception; 129 130 /** 131 * Method testParseFieldsOnFile. 132 * Provide a test to show that fields on a file entry are parsed correctly. 133 * @throws Exception 134 */ 135 public abstract void testParseFieldsOnFile() throws Exception; 136 137 /** 138 * @see junit.framework.TestCase#setUp() 139 */ 140 @Override 141 protected void setUp() throws Exception 142 { 143 super.setUp(); 144 parser = getParser(); 145 df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US); 146 } 147 148 /** 149 * Check if FTPFile entry parsing failed; i.e. if entry is null or date is null. 150 * 151 * @param f FTPFile entry - may be null 152 * @return null if f is null or the date is null 153 */ 154 protected FTPFile nullFileOrNullDate(FTPFile f) { 155 if (f==null){ 156 return null; 157 } 158 if (f.getTimestamp() == null){ 159 return null; 160 } 161 return f; 162 } 163 }