alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons IO example source code file (IOFileFilterAbstractTestCase.java)

This example Commons IO source code file (IOFileFilterAbstractTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons IO tags/keywords

exception, falsefilefilter, file, file, filter, filter, io, iofilefilter, iofilefilterabstracttestcase, override, override, string, string, testerfalsefilefilter, testertruefilefilter

The Commons IO IOFileFilterAbstractTestCase.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.io.filefilter;

import java.io.File;

import junit.framework.TestCase;

public abstract class IOFileFilterAbstractTestCase extends TestCase {
  
  /**
   * Constructs a new instance of
   * <code>IOFileFilterAbstractTestCase.
   */
  public IOFileFilterAbstractTestCase(final String name) {
    super(name);
  }
  
  public boolean assertFileFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
  throws Exception {
    assertEquals(
    "test " + testNumber + " Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
    expected, filter.accept(file));
    return true; // return is irrelevant
  }

  public boolean assertFilenameFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
  throws Exception {
    // Assumes file has parent and is not passed as null
      assertEquals(
    "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
    expected, filter.accept(file.getParentFile(), file.getName()));
    return true; // return is irrelevant
  }

  public void assertFiltering(final int testNumber, final IOFileFilter filter, final File file, final boolean expected)
  throws Exception {
    // Note. This only tests the (File, String) version if the parent of
    //       the File passed in is not null
      assertEquals(
    "test " + testNumber + " Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
    expected, filter.accept(file));

    if (file != null && file.getParentFile() != null) {
        assertEquals(
      "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
      expected, filter.accept(file.getParentFile(), file.getName()));
    } 
    else if (file == null) {
        assertEquals(
      "test " + testNumber + " Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
      expected, filter.accept(file));
    }
  }

  public void assertTrueFiltersInvoked(final int testNumber, final TesterTrueFileFilter[] filters, final boolean[] invoked) {
    for(int i = 1; i < filters.length; i++) {
      assertEquals("test " + testNumber + " filter " + i + " invoked", invoked[i-1], filters[i].isInvoked());
    }
  }
  
  public void assertFalseFiltersInvoked(final int testNumber, final TesterFalseFileFilter[] filters, final boolean[] invoked) {
    for(int i = 1; i < filters.length; i++) {
      assertEquals("test " + testNumber + " filter " + i + " invoked", invoked[i-1], filters[i].isInvoked());
    }
  }
  
  public File determineWorkingDirectoryPath(final String key, final String defaultPath) {
    // Look for a system property to specify the working directory
    String workingPathName = System.getProperty(key, defaultPath);
    return new File(workingPathName);
  }

  public void resetFalseFilters(TesterFalseFileFilter[] filters) {
    for(int i = 0; i < filters.length; i++) {
      if(filters[i] != null) {
        filters[i].reset();
      }
    }
  }
  
  public void resetTrueFilters(TesterTrueFileFilter[] filters) {
    for(int i = 0; i < filters.length; i++) {
      if(filters[i] != null) {
        filters[i].reset();
      }
    }
  }
  
  class TesterTrueFileFilter extends TrueFileFilter {

    private boolean invoked;
    
    @Override
    public boolean accept(File file) {
      setInvoked(true);
      return super.accept(file);
    }
    
    @Override
    public boolean accept(File file, String str) {
      setInvoked(true);
      return super.accept(file, str);
    }
    
    public boolean isInvoked() {
      return this.invoked;
    }

    public void setInvoked(boolean invoked) {
      this.invoked = invoked;
    }
    
    public void reset() {
      setInvoked(false);
    }
  }
  
  class TesterFalseFileFilter extends FalseFileFilter {
    
    private boolean invoked;
    
    @Override
    public boolean accept(File file) {
      setInvoked(true);
      return super.accept(file);
    }
    
    @Override
    public boolean accept(File file, String str) {
      setInvoked(true);
      return super.accept(file, str);
    }
    
    public boolean isInvoked() {
      return this.invoked;
    }

    public void setInvoked(boolean invoked) {
      this.invoked = invoked;
    }
    
    public void reset() {
      setInvoked(false);
    }
  }
}

Other Commons IO examples (source code examples)

Here is a short list of links related to this Commons IO IOFileFilterAbstractTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.