From 9ff711605d767d28bd9019667675c482c6ee73b8 Mon Sep 17 00:00:00 2001 From: Marten Gajda Date: Sun, 28 Jul 2024 00:58:06 +0200 Subject: [PATCH] Add Path Qualities, closes #174 --- .../confidence/description/Value.java | 6 ++ .../confidence/quality/path/ADirectory.java | 38 +++++++++ .../confidence/quality/path/AFile.java | 38 +++++++++ .../confidence/quality/path/ContainsText.java | 59 +++++++++++++ .../confidence/quality/path/Exists.java | 38 +++++++++ .../confidence/quality/path/HasName.java | 43 ++++++++++ .../confidence/quality/path/HasParent.java | 43 ++++++++++ .../confidence/quality/path/Readable.java | 38 +++++++++ .../confidence/quality/path/Writeable.java | 38 +++++++++ .../quality/path/ADirectoryTest.java | 51 +++++++++++ .../confidence/quality/path/AFileTest.java | 51 +++++++++++ .../quality/path/ContainsTextTest.java | 84 +++++++++++++++++++ .../confidence/quality/path/ExistsTest.java | 50 +++++++++++ .../confidence/quality/path/HasNameTest.java | 42 ++++++++++ .../quality/path/HasParentTest.java | 52 ++++++++++++ .../confidence/quality/path/ReadableTest.java | 54 ++++++++++++ .../confidence/quality/path/WritableTest.java | 54 ++++++++++++ 17 files changed, 779 insertions(+) create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ADirectory.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/AFile.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ContainsText.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Exists.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasName.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasParent.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Readable.java create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Writeable.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ADirectoryTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/AFileTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ContainsTextTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ExistsTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasNameTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasParentTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ReadableTest.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/WritableTest.java diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/description/Value.java b/confidence-core/src/main/java/org/saynotobugs/confidence/description/Value.java index 9ff3ab98..dfac4ea2 100644 --- a/confidence-core/src/main/java/org/saynotobugs/confidence/description/Value.java +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/description/Value.java @@ -23,6 +23,7 @@ import org.saynotobugs.confidence.Scribe; import org.saynotobugs.confidence.utils.ArrayIterable; +import java.nio.file.Path; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -71,6 +72,11 @@ else if (value instanceof Set) { description = new SetDescription((Set) value); } + else if (value instanceof Path) + { + // Path is an Iterable of Path, potentially resulting in an infinite loop, hence we treat it seprately here + description = new ToStringDescription(value); + } else if (value instanceof Iterable) { description = new IterableDescription((Iterable) value); diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ADirectory.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ADirectory.java new file mode 100644 index 00000000..d08e8000 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ADirectory.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.Satisfies; + +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class ADirectory extends QualityComposition +{ + public ADirectory() + { + super(new Satisfies<>(Files::isDirectory, file -> new Text("not a directory"), new Text("a directory"))); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/AFile.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/AFile.java new file mode 100644 index 00000000..a3d4fd2a --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/AFile.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.Satisfies; + +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class AFile extends QualityComposition +{ + public AFile() + { + super(new Satisfies<>(Files::isRegularFile, file -> new Text("not a file"), new Text("a file"))); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ContainsText.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ContainsText.java new file mode 100644 index 00000000..e425c3be --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/ContainsText.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.Quality; +import org.saynotobugs.confidence.description.Spaced; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.description.Value; +import org.saynotobugs.confidence.quality.composite.DescribedAs; +import org.saynotobugs.confidence.quality.composite.Has; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.EqualTo; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class ContainsText extends QualityComposition +{ + public ContainsText(String text) + { + this(new EqualTo<>(text)); + } + + public ContainsText(Quality mDelegate) + { + this(StandardCharsets.UTF_8, mDelegate); + } + + public ContainsText(Charset charset, Quality delegate) + { + super(new Has<>( + new Text("contains"), + new Text("contained"), + path -> new String(Files.readAllBytes(path), charset), + new DescribedAs<>(orig -> new Spaced(new Value(charset.name()), new Text("text"), orig), delegate))); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Exists.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Exists.java new file mode 100644 index 00000000..da0950a1 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Exists.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.Satisfies; + +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class Exists extends QualityComposition +{ + public Exists() + { + super(new Satisfies<>(Files::exists, file -> new Text("does not exist"), new Text("exists"))); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasName.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasName.java new file mode 100644 index 00000000..3b5738a0 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasName.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.Quality; +import org.saynotobugs.confidence.quality.composite.Has; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.EqualTo; + +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class HasName extends QualityComposition +{ + public HasName(CharSequence name) + { + this(new EqualTo<>(name)); + } + + public HasName(Quality nameQuality) + { + super(new Has<>("name", path -> path.getFileName().toString(), nameQuality)); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasParent.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasParent.java new file mode 100644 index 00000000..fd3c1bc2 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/HasParent.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.Quality; +import org.saynotobugs.confidence.quality.composite.Has; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.EqualTo; + +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class HasParent extends QualityComposition +{ + public HasParent(Path parent) + { + this(new EqualTo<>(parent)); + } + + public HasParent(Quality parentQuality) + { + super(new Has<>("parent", Path::getParent, parentQuality)); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Readable.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Readable.java new file mode 100644 index 00000000..217478b3 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Readable.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.Satisfies; + +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class Readable extends QualityComposition +{ + public Readable() + { + super(new Satisfies<>(Files::isReadable, file -> new Text("not readable"), new Text("readable"))); + } +} diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Writeable.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Writeable.java new file mode 100644 index 00000000..186023b7 --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/path/Writeable.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.composite.QualityComposition; +import org.saynotobugs.confidence.quality.object.Satisfies; + +import java.nio.file.Files; +import java.nio.file.Path; + +@StaticFactories( + value = "Path", + packageName = "org.saynotobugs.confidence.core.quality") +public final class Writeable extends QualityComposition +{ + public Writeable() + { + super(new Satisfies<>(Files::isWritable, file -> new Text("not writeable"), new Text("writeable"))); + } +} diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ADirectoryTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ADirectoryTest.java new file mode 100644 index 00000000..60ce5a06 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ADirectoryTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class ADirectoryTest +{ + @TempDir + Path tempDir; + + @Test + void test() throws IOException + { + Path file = tempDir.resolve("somefile"); + Files.createFile(file); + assertThat(new ADirectory(), + new AllOf<>( + new Passes<>(new Path[] { tempDir }), + new Fails<>(file, "not a directory"), + new Fails<>(tempDir.resolve("nonExistentFile"), "not a directory"), + new HasDescription("a directory"))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/AFileTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/AFileTest.java new file mode 100644 index 00000000..18758b37 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/AFileTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class AFileTest +{ + @TempDir + Path tempDir; + + @Test + void test() throws IOException + { + Path file = tempDir.resolve("somefile"); + Files.createFile(file); + assertThat(new AFile(), + new AllOf<>( + new Passes<>(new Path[] { file }), + new Fails<>(tempDir, "not a file"), + new Fails<>(tempDir.resolve("nonExistentFile"), "not a file"), + new HasDescription("a file"))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ContainsTextTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ContainsTextTest.java new file mode 100644 index 00000000..3598963e --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ContainsTextTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.charsequence.MatchesPattern; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.quality.object.EqualTo; +import org.saynotobugs.confidence.test.quality.DescribesAs; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.saynotobugs.confidence.Assertion.assertThat; + +class ContainsTextTest +{ + @TempDir + Path tempDir; + + @Test + void testWithQuality() throws IOException + { + Path emptyFile = tempDir.resolve("somefile"); + Files.createFile(emptyFile); + Path file10Bytes = tempDir.resolve("fileWithSize10"); + Files.createFile(file10Bytes); + try (Writer fileWriter = new FileWriter(file10Bytes.toFile())) + { + fileWriter.write("0123456789"); + } + assertThat(new ContainsText(UTF_8, new EqualTo<>("0123456789")), + new AllOf<>( + new Passes<>(new Path[] { file10Bytes }), + new Fails<>(tempDir, new DescribesAs(new MatchesPattern("threw ]+>"))), + new Fails<>(emptyFile, "contained \"UTF-8\" text \"\""), + new HasDescription("contains \"UTF-8\" text \"0123456789\"") + )); + } + + @Test + void testWithPrimitive() throws IOException + { + Path emptyFile = tempDir.resolve("somefile"); + Files.createFile(emptyFile); + Path file10Bytes = tempDir.resolve("fileWithSize10"); + Files.createFile(file10Bytes); + try (Writer fileWriter = new FileWriter(file10Bytes.toFile())) + { + fileWriter.write("0123456789"); + } + assertThat(new ContainsText("0123456789"), + new AllOf<>( + new Passes<>(new Path[] { file10Bytes }), + new Fails<>(tempDir, new DescribesAs(new MatchesPattern("threw ]+>"))), + new Fails<>(emptyFile, "contained \"UTF-8\" text \"\""), + new HasDescription("contains \"UTF-8\" text \"0123456789\"") + )); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ExistsTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ExistsTest.java new file mode 100644 index 00000000..f5929838 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ExistsTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class ExistsTest +{ + @TempDir + Path tempDir; + + @Test + void test() throws IOException + { + Path file = tempDir.resolve("somefile"); + Files.createFile(file); + assertThat(new Exists(), + new AllOf<>( + new Passes<>(tempDir, file), + new Fails<>(tempDir.resolve("nonExistentFile"), "does not exist"), + new HasDescription("exists"))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasNameTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasNameTest.java new file mode 100644 index 00000000..caaf6eba --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasNameTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class HasNameTest +{ + @Test + void testWithQuality() + { + assertThat(new HasName("someFile"), + new AllOf<>( + new Passes<>(new Path[] { Path.of("path", "someFile") }), + new Fails<>(Path.of("path", "otherFile"), "had name \"otherFile\""), + new HasDescription("has name \"someFile\""))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasParentTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasParentTest.java new file mode 100644 index 00000000..90d5c54d --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/HasParentTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class HasParentTest +{ + @Test + void testWithQuality() + { + assertThat(new HasParent(new HasName("path")), + new AllOf<>( + new Passes<>(new Path[] { Path.of("path", "someFile") }), + new Fails<>(Path.of("otherPath", "otherFile"), "had parent had name \"otherPath\""), + new HasDescription("has parent has name \"path\""))); + } + + @Test + void testWithFile() + { + assertThat(new HasParent(Path.of("path")), + new AllOf<>( + new Passes<>(new Path[] { Path.of("path", "someFile") }), + new Fails<>(Path.of("otherPath", "otherFile"), "had parent "), + new HasDescription("has parent "))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ReadableTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ReadableTest.java new file mode 100644 index 00000000..92ae1d09 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/ReadableTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class ReadableTest +{ + @TempDir + Path tempDir; + + @Test + void test() throws IOException + { + Path readableFile = tempDir.resolve("readableFile"); + Files.createFile(readableFile); + Path unreadableFile = tempDir.resolve("unreadableFile"); + Files.createFile(unreadableFile); + unreadableFile.toFile().setReadable(false); + assertThat(new Readable(), + new AllOf<>( + new Passes<>(tempDir, readableFile), + new Fails<>(unreadableFile, "not readable"), + new Fails<>(tempDir.resolve("nonExistentFile"), "not readable"), + new HasDescription("readable"))); + } +} \ No newline at end of file diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/WritableTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/WritableTest.java new file mode 100644 index 00000000..8f342853 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/path/WritableTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2024 dmfs GmbH + * + * + * 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. + * + */ + +package org.saynotobugs.confidence.quality.path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.saynotobugs.confidence.Assertion.assertThat; + +class WritableTest +{ + @TempDir + Path tempDir; + + @Test + void test() throws IOException + { + Path writeableFile = tempDir.resolve("writeableFile"); + Files.createFile(writeableFile); + Path unwriteableFile = tempDir.resolve("unwriteableFile"); + Files.createFile(unwriteableFile); + unwriteableFile.toFile().setWritable(false); + assertThat(new Writeable(), + new AllOf<>( + new Passes<>(tempDir, writeableFile), + new Fails<>(unwriteableFile, "not writeable"), + new Fails<>(tempDir.resolve("nonExistentFile"), "not writeable"), + new HasDescription("writeable"))); + } +} \ No newline at end of file