Skip to content

Commit

Permalink
Fixes #158 : Path assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
fge authored and joel-costigliola committed Feb 6, 2015
1 parent a19bea6 commit 9e86697
Show file tree
Hide file tree
Showing 102 changed files with 6,014 additions and 219 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Expand Up @@ -47,6 +47,22 @@
<version>2.2.2</version>
<optional>true</optional>
</dependency>
<!--
NEEDED! Unlike File, a Path is not linked to the JRE's filesystem.
In order to accurately test assertions, we need a decent JSR 203 implementation
which lets us test our assertions. Right now, this is memoryfilesystem
(https://github.com/marschall/memoryfilesystem).
Another choice would be jimfs from Google (https://github.com/google/jimfs),
but its support for reading/writing file attributes is not as complete as that
of memoryfilesystem's.
-->
<dependency>
<groupId>com.github.marschall</groupId>
<artifactId>memoryfilesystem</artifactId>
<version>0.6.3</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/assertj/core/api/AbstractFileAssert.java
Expand Up @@ -190,7 +190,7 @@ public S usingCharset(Charset charset) {
* @throws AssertionError if the actual {@code File} is {@code null}.
* @throws AssertionError if the actual {@code File} is not an existing file.
* @throws FilesException if an I/O error occurs.
* @throws AssertionError if the content of the actual {@code File} is not equal to the given binary content.
* @throws AssertionError if the content of the actual {@code File} is not equal to the given content.
*/
public S hasContent(String expected) {
files.assertHasContent(info, actual, expected, charset);
Expand Down
1,267 changes: 1,267 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractPathAssert.java

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractSoftAssertions.java
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -217,6 +218,16 @@ public FileAssert assertThat(File actual) {
return proxy(FileAssert.class, File.class, actual);
}

/**
* Creates a new, proxied instance of a {@link PathAssert}
*
* @param actual the path
* @return the created assertion object
*/
public PathAssert assertThat(Path actual) {
return proxy(PathAssert.class, Path.class, actual);
}

/**
* Creates a new instance of <code>{@link InputStreamAssert}</code>.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/assertj/core/api/Assertions.java
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.DateFormat;
import java.util.Date;
import java.util.Iterator;
Expand Down Expand Up @@ -252,6 +253,17 @@ public static AbstractFileAssert<?> assertThat(File actual) {
return new FileAssert(actual);
}

/**
* Creates a new instance of {@link PathAssert}
*
* @param actual the path to test
* @return the created assertion object
*/
public static AbstractPathAssert<?> assertThat(Path actual)
{
return new PathAssert(actual);
}

/**
* Creates a new instance of <code>{@link InputStreamAssert}</code>.
*
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/assertj/core/api/PathAssert.java
@@ -0,0 +1,31 @@
/**
* Licensed 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.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.api;

import java.nio.file.Path;

/**
* Assertion class for {@link Path}s
*/
public class PathAssert extends AbstractPathAssert<PathAssert>
{
/**
* Constructor
*
* @param actual the path to test
*/
protected PathAssert(Path actual)
{
super(actual, PathAssert.class);
}
}
20 changes: 18 additions & 2 deletions src/main/java/org/assertj/core/error/ShouldBeAbsolutePath.java
Expand Up @@ -12,16 +12,24 @@
*/
package org.assertj.core.error;

import org.assertj.core.util.VisibleForTesting;

import java.io.File;
import java.nio.file.Path;

/**
* Creates an error message indicating that an assertion that verifies that a <code>{@link File}</code> is an absolute path
* Creates an error message indicating that an assertion that verifies that a
* {@link File} or {@link Path} is an absolute path
* failed.
*
* @author Yvonne Wang
* @author Francis Galiegue
*/
public class ShouldBeAbsolutePath extends BasicErrorMessageFactory {

@VisibleForTesting
public static final String SHOULD_BE_ABSOLUTE_PATH = "%nExpecting:%n <%s>%nto be an absolute path.";

/**
* Creates a new <code>{@link ShouldBeAbsolutePath}</code>.
* @param actual the actual value in the failed assertion.
Expand All @@ -31,7 +39,15 @@ public static ErrorMessageFactory shouldBeAbsolutePath(File actual) {
return new ShouldBeAbsolutePath(actual);
}

public static ErrorMessageFactory shouldBeAbsolutePath(final Path actual) {
return new ShouldBeAbsolutePath(actual);
}

private ShouldBeAbsolutePath(File actual) {
super("\nExpecting:\n <%s>\nto be an absolute path", actual);
super(SHOULD_BE_ABSOLUTE_PATH, actual);
}

private ShouldBeAbsolutePath(final Path actual) {
super(SHOULD_BE_ABSOLUTE_PATH, actual);
}
}
40 changes: 40 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldBeCanonicalPath.java
@@ -0,0 +1,40 @@
/**
* Licensed 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.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.error;

import org.assertj.core.util.VisibleForTesting;

import java.nio.file.Path;

/**
* Creates an error message indicating that an assertion that verifies that a
* {@link Path} is canonical has failed.
*
*/
public class ShouldBeCanonicalPath
extends BasicErrorMessageFactory
{
@VisibleForTesting
public static final String SHOULD_BE_CANONICAL
= "%nExpecting:%n <%s>%nto be a canonical path";

public static ErrorMessageFactory shouldBeCanonicalPath(final Path actual)
{
return new ShouldBeCanonicalPath(actual);
}

private ShouldBeCanonicalPath(final Path actual)
{
super(SHOULD_BE_CANONICAL, actual);
}
}
39 changes: 26 additions & 13 deletions src/main/java/org/assertj/core/error/ShouldBeDirectory.java
Expand Up @@ -13,25 +13,38 @@
package org.assertj.core.error;

import java.io.File;
import java.nio.file.Path;

import org.assertj.core.util.VisibleForTesting;

/**
* Creates an error message indicating that an assertion that verifies that a <code>{@link File}</code> is an existing directory
* failed.
* Creates an error message indicating that an assertion that verifies that a {@link File} or {@link Path} is an
* existing directory failed
*
* @author Yvonne Wang
* @author Francis Galiegue
*/
public class ShouldBeDirectory extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldBeDirectory}</code>.
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeDirectory(File actual) {
return new ShouldBeDirectory(actual);
public class ShouldBeDirectory extends BasicErrorMessageFactory
{
@VisibleForTesting
public static final String PATH_SHOULD_BE_DIRECTORY = "%nExpecting path:%n <%s>%nto be a directory.";

@VisibleForTesting
public static final String FILE_SHOULD_BE_DIRECTORY = "%nExpecting file:%n <%s>%n to be an existing directory.";

public static ErrorMessageFactory shouldBeDirectory(final Path actual) {
return new ShouldBeDirectory(actual);
}

public static ErrorMessageFactory shouldBeDirectory(final File actual) {
return new ShouldBeDirectory(actual);
}

private ShouldBeDirectory(final Path actual) {
super(PATH_SHOULD_BE_DIRECTORY, actual);
}

private ShouldBeDirectory(File actual) {
super("\nExpecting:\n <%s>\nto be an existing directory", actual);
private ShouldBeDirectory(final File actual) {
super(FILE_SHOULD_BE_DIRECTORY, actual);
}
}
16 changes: 14 additions & 2 deletions src/main/java/org/assertj/core/error/ShouldBeExecutable.java
Expand Up @@ -13,6 +13,7 @@
package org.assertj.core.error;

import java.io.File;
import java.nio.file.Path;

/**
* Creates an error message indicating that an assertion that verifies that a <code>{@link File}</code> is executable
Expand All @@ -22,17 +23,28 @@
*
*/
public class ShouldBeExecutable extends BasicErrorMessageFactory {
static final String SHOULD_BE_EXECUTABLE = "%nExpecting:%n <%s>%nto be executable.";

private ShouldBeExecutable(File actual) {
super("\nExpecting:\n <%s>\nto be executable", actual);
super(SHOULD_BE_EXECUTABLE, actual);
}

private ShouldBeExecutable(Path actual) {
super(SHOULD_BE_EXECUTABLE, actual);
}

/**
* Creates a new <code>{@link ShouldBeExecutable}</code>.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeExecutable(File actual) {
return new ShouldBeExecutable(actual);
return new ShouldBeExecutable(actual);
}

public static ErrorMessageFactory shouldBeExecutable(Path actual) {
return new ShouldBeExecutable(actual);
}

}
36 changes: 36 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldBeNormalized.java
@@ -0,0 +1,36 @@
/**
* Licensed 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.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.error;

import org.assertj.core.util.VisibleForTesting;

import java.nio.file.Path;

/**
* Assertion error message delivered when a {@link Path} is not normalized
*
* @see Path#normalize()
*/
public class ShouldBeNormalized extends BasicErrorMessageFactory
{
@VisibleForTesting
public static final String SHOULD_BE_NORMALIZED = "Expected path:%n <%s>%nto be normalized.";

private ShouldBeNormalized(Path actual) {
super(SHOULD_BE_NORMALIZED, actual);
}

public static ErrorMessageFactory shouldBeNormalized(Path actual) {
return new ShouldBeNormalized(actual);
}
}
20 changes: 16 additions & 4 deletions src/main/java/org/assertj/core/error/ShouldBeReadable.java
Expand Up @@ -13,26 +13,38 @@
package org.assertj.core.error;

import java.io.File;
import java.nio.file.Path;

/**
* Creates an error message indicating that an assertion that verifies that a <code>{@link File}</code> is readable
* failed.
* Creates an error message indicating that an assertion that verifies that a <code>{@link File}</code> or a a
* <code>{@link Path}</code> is readable failed.
*
* @author Olivier Demeijer
*
*/
public class ShouldBeReadable extends BasicErrorMessageFactory {
static final String SHOULD_BE_READABLE = "%nExpecting:%n <%s>%nto be readable.";

private ShouldBeReadable(File actual) {
super("\nExpecting file:\n <%s>\nto be readable", actual);
super(SHOULD_BE_READABLE, actual);
}

private ShouldBeReadable(Path actual) {
super(SHOULD_BE_READABLE, actual);
}

/**
* Creates a new <code>{@link ShouldBeReadable}</code>.
*
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeReadable(File actual) {
return new ShouldBeReadable(actual);
return new ShouldBeReadable(actual);
}

public static ErrorMessageFactory shouldBeReadable(Path actual) {
return new ShouldBeReadable(actual);
}

}

0 comments on commit 9e86697

Please sign in to comment.