-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Resource Initializer that creates a File, closes #189
- Loading branch information
Showing
4 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
confidence-core/src/main/java/org/saynotobugs/confidence/quality/file/ContainsData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2023 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.file; | ||
|
||
import org.dmfs.srcless.annotations.staticfactory.DeprecatedFactories; | ||
import org.dmfs.srcless.annotations.staticfactory.StaticFactories; | ||
import org.saynotobugs.confidence.Assessment; | ||
import org.saynotobugs.confidence.Description; | ||
import org.saynotobugs.confidence.Quality; | ||
import org.saynotobugs.confidence.assessment.Fail; | ||
import org.saynotobugs.confidence.assessment.FailPrepended; | ||
import org.saynotobugs.confidence.description.Spaced; | ||
import org.saynotobugs.confidence.description.Text; | ||
import org.saynotobugs.confidence.description.Value; | ||
import org.saynotobugs.confidence.quality.object.EqualTo; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
@StaticFactories( | ||
value = "File", | ||
packageName = "org.saynotobugs.confidence.core.quality", | ||
deprecates = @DeprecatedFactories(value = "File", packageName = "org.saynotobugs.confidence.quality")) | ||
public final class ContainsData implements Quality<File> | ||
{ | ||
private final Quality<? super byte[]> mDelegate; | ||
|
||
public ContainsData(byte[] data) | ||
{ | ||
this(new EqualTo<>(data)); | ||
} | ||
|
||
public ContainsData(Quality<? super byte[]> delegate) | ||
{ | ||
mDelegate = delegate; | ||
} | ||
|
||
@Override | ||
public Assessment assessmentOf(File candidate) | ||
{ | ||
try (DataInputStream reader = new DataInputStream(new FileInputStream(candidate))) | ||
{ | ||
byte[] buffer = new byte[(int) candidate.length()]; | ||
reader.readFully(buffer); | ||
return new FailPrepended( | ||
new Spaced( | ||
new Text("contained data")), mDelegate.assessmentOf(buffer)); | ||
} | ||
catch (IOException exception) | ||
Check warning on line 67 in confidence-core/src/main/java/org/saynotobugs/confidence/quality/file/ContainsData.java Codecov / codecov/patchconfidence-core/src/main/java/org/saynotobugs/confidence/quality/file/ContainsData.java#L67
|
||
{ | ||
return new Fail(new Spaced(new Text("threw"), new Value(exception), new Text("while reading"))); | ||
Check warning on line 69 in confidence-core/src/main/java/org/saynotobugs/confidence/quality/file/ContainsData.java Codecov / codecov/patchconfidence-core/src/main/java/org/saynotobugs/confidence/quality/file/ContainsData.java#L69
|
||
} | ||
} | ||
|
||
@Override | ||
public Description description() | ||
{ | ||
return new Spaced( | ||
new Text("contains data"), | ||
mDelegate.description()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
confidence-core/src/test/java/org/saynotobugs/confidence/quality/file/ContainsDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.saynotobugs.confidence.quality.file; | ||
|
||
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.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static org.saynotobugs.confidence.Assertion.assertThat; | ||
|
||
class ContainsDataTest | ||
{ | ||
@TempDir | ||
File dir; | ||
|
||
@Test | ||
void test() throws IOException | ||
{ | ||
File passingFile = new File(dir, "passes"); | ||
Files.write(passingFile.toPath(), new byte[] { 1, 2, 3 }); | ||
File emptyFile = new File(dir, "empty"); | ||
emptyFile.createNewFile(); | ||
|
||
assertThat(new ContainsData(new byte[] { 1, 2, 3 }), | ||
new AllOf<>( | ||
new Passes<>(passingFile), | ||
new Fails<>(emptyFile, "contained data array that iterated [ 0: missing <1>,\n 1: missing <2>,\n 2: missing <3> ]"), | ||
new HasDescription("contains data [ <1>,\n <2>,\n <3> ]"))); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...-incubator/src/main/java/org/saynotobugs/confidence/junit5/engine/procedure/WithFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.saynotobugs.confidence.junit5.engine.procedure; | ||
|
||
import org.dmfs.jems2.FragileProcedure; | ||
import org.saynotobugs.confidence.junit5.engine.Resource; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
/** | ||
* Initialization procedure for {@link File} directory {@link Resource}s | ||
* that creates a file inside that directory. | ||
*/ | ||
public final class WithFile implements FragileProcedure<File, IOException> | ||
{ | ||
private final String mName; | ||
private final byte[] mDdata; | ||
|
||
public WithFile(String name) | ||
{ | ||
this(name, new byte[0]); | ||
} | ||
|
||
public WithFile(String name, String data) | ||
{ | ||
this(name, data, StandardCharsets.UTF_8); | ||
} | ||
|
||
public WithFile(String name, String data, Charset charset) | ||
{ | ||
this(name, data.getBytes(charset)); | ||
} | ||
|
||
public WithFile(String name, byte[] data) | ||
{ | ||
mName = name; | ||
mDdata = data; | ||
} | ||
|
||
@Override | ||
public void process(File file) throws IOException | ||
{ | ||
Files.write(new File(file, mName).toPath(), mDdata); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ubator/src/test/java/org/saynotobugs/confidence/junit5/engine/procedure/WithFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.saynotobugs.confidence.junit5.engine.procedure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
|
||
import static java.nio.charset.StandardCharsets.ISO_8859_1; | ||
import static org.dmfs.jems2.confidence.Jems2.procedureThatAffects; | ||
import static org.saynotobugs.confidence.Assertion.assertThat; | ||
import static org.saynotobugs.confidence.core.quality.File.*; | ||
import static org.saynotobugs.confidence.core.quality.Grammar.soIt; | ||
import static org.saynotobugs.confidence.core.quality.Grammar.that; | ||
import static org.saynotobugs.confidence.core.quality.Object.hasToString; | ||
|
||
class WithFileTest | ||
{ | ||
@TempDir | ||
File dir; | ||
|
||
@Test | ||
void testEmptyFile() | ||
{ | ||
assertThat(new WithFile("somefile"), | ||
procedureThatAffects(() -> dir, soIt( | ||
containsFile("somefile")))); | ||
} | ||
|
||
|
||
@Test | ||
void testFileWithUtf8String() | ||
{ | ||
assertThat(new WithFile("somefile", "utf-8 contentöäü@ſ€"), | ||
procedureThatAffects(() -> dir, soIt( | ||
containsFile("somefile", that(containsText("utf-8 contentöäü@ſ€")))))); | ||
} | ||
|
||
@Test | ||
void testFileWithLatin1String() | ||
{ | ||
assertThat(new WithFile("somefile", "latin-1-text-öäü", ISO_8859_1), | ||
procedureThatAffects(() -> dir, soIt( | ||
containsFile("somefile", that(containsText(ISO_8859_1, hasToString("latin-1-text-öäü"))))))); | ||
} | ||
|
||
|
||
@Test | ||
void testFileWithByteArray() | ||
{ | ||
assertThat(new WithFile("somefile", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }), | ||
procedureThatAffects(() -> dir, soIt( | ||
containsFile("somefile", that(containsData(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })))))); | ||
} | ||
|
||
} |