001 /* 002 * Created on Jan 28, 2011 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2011 the original author or authors. 014 */ 015 package org.fest.assertions.api; 016 017 import java.io.File; 018 019 import org.fest.assertions.internal.Files; 020 import org.fest.util.*; 021 022 /** 023 * Assertion methods for <code>{@link File}</code>s. 024 * <p> 025 * To create a new instance of this class, invoke <code>{@link Assertions#assertThat(File)}</code>. 026 * </p> 027 * 028 * @author David DIDIER 029 * @author Yvonne Wang 030 * @author Alex Ruiz 031 */ 032 public class FileAssert extends AbstractAssert<FileAssert, File> { 033 034 @VisibleForTesting Files files = Files.instance(); 035 036 protected FileAssert(File actual) { 037 super(actual, FileAssert.class); 038 } 039 040 /** 041 * Verifies that the actual {@code File} exists, regardless it's a file or directory. 042 * @return {@code this} assertion object. 043 * @throws AssertionError if the actual {@code File} is {@code null}. 044 * @throws AssertionError if the actual {@code File} does not exist. 045 */ 046 public FileAssert exists() { 047 files.assertExists(info, actual); 048 return this; 049 } 050 051 /** 052 * Verifies that the actual {@code File} does not exist. 053 * @return {@code this} assertion object. 054 * @throws AssertionError if the actual {@code File} is {@code null}. 055 * @throws AssertionError if the actual {@code File} exists. 056 */ 057 public FileAssert doesNotExist() { 058 files.assertDoesNotExist(info, actual); 059 return this; 060 } 061 062 /** 063 * Verifies that the actual {@code File} is an existing file. 064 * @return {@code this} assertion object. 065 * @throws AssertionError if the actual {@code File} is {@code null}. 066 * @throws AssertionError if the actual {@code File} is not an existing file. 067 */ 068 public FileAssert isFile() { 069 files.assertIsFile(info, actual); 070 return this; 071 } 072 073 /** 074 * Verifies that the actual {@code File} is an existing directory. 075 * @return {@code this} assertion object. 076 * @throws AssertionError if the actual {@code File} is {@code null}. 077 * @throws AssertionError if the actual {@code File} is not an existing file. 078 */ 079 public FileAssert isDirectory() { 080 files.assertIsDirectory(info, actual); 081 return this; 082 } 083 084 /** 085 * Verifies that the actual {@code File} is an absolute path. 086 * @return {@code this} assertion object. 087 * @throws AssertionError if the actual {@code File} is {@code null}. 088 * @throws AssertionError if the actual {@code File} is not an absolute path. 089 */ 090 public FileAssert isAbsolute() { 091 files.assertIsAbsolute(info, actual); 092 return this; 093 } 094 095 /** 096 * Verifies that the actual {@code File} is a relative path. 097 * @return {@code this} assertion object. 098 * @throws AssertionError if the actual {@code File} is {@code null}. 099 * @throws AssertionError if the actual {@code File} is not a relative path. 100 */ 101 public FileAssert isRelative() { 102 files.assertIsRelative(info, actual); 103 return this; 104 } 105 106 /** 107 * Verifies that the content of the actual {@code File} is equal to the content of the given one. 108 * @param expected the given {@code File} to compare the actual {@code File} to. 109 * @return {@code this} assertion object. 110 * @throws NullPointerException if the given {@code File} is {@code null}. 111 * @throws IllegalArgumentException if the given {@code File} is not an existing file. 112 * @throws AssertionError if the actual {@code File} is {@code null}. 113 * @throws AssertionError if the actual {@code File} is not an existing file. 114 * @throws FilesException if an I/O error occurs. 115 * @throws AssertionError if the content of the actual {@code File} is not equal to the content of the given one. 116 */ 117 public FileAssert hasContentEqualTo(File expected) { 118 files.assertEqualContent(info, actual, expected); 119 return this; 120 } 121 }