-
Notifications
You must be signed in to change notification settings - Fork 2
Work on junit tests #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sknull
wants to merge
4
commits into
RouHim:main
Choose a base branch
from
sknull:work-on-junit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e93356b
Work on JUnit tests regarding non existent files. Introduced configur…
sknull 6f8bbff
Enable some tests only on windows (gives unexpected results on github)
sknull 563391c
Enable some tests only on windows (gives unexpected results on github)
sknull 8a65547
Add status badge
sknull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
| <component name="ProjectRunConfigurationManager"> | ||
| <configuration default="false" name="java in jaudiotagger" type="JUnit" factoryName="JUnit" nameIsGenerated="true"> | ||
| <module name="jaudiotagger" /> | ||
| <option name="MAIN_CLASS_NAME" value="" /> | ||
| <option name="METHOD_NAME" value="" /> | ||
| <option name="TEST_OBJECT" value="directory" /> | ||
| <dir value="$PROJECT_DIR$/src/test/java" /> | ||
| <method v="2"> | ||
| <option name="Make" enabled="true" /> | ||
| </method> | ||
| </configuration> | ||
| </component> |
This file contains hidden or 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
This file contains hidden or 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
197 changes: 197 additions & 0 deletions
197
src/test/java/org/jaudiotagger/AbstractBaseTestCase.java
This file contains hidden or 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,197 @@ | ||
| package org.jaudiotagger; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.BufferedOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.file.Files; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class AbstractBaseTestCase { | ||
|
|
||
| private static File tempDirectory; | ||
| protected final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
|
||
| @BeforeAll | ||
| public static void setUpOnce() throws Exception { | ||
| tempDirectory = Files.createTempDirectory("jaudiotagger_").toFile(); | ||
| } | ||
|
|
||
| public boolean executeAlsoWithMissingResources() { | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Copy audiofile to processing dir ready for use in test | ||
| * | ||
| * @param fileName | ||
| * @return | ||
| */ | ||
| public File copyAudioToTmp(String fileName) { | ||
| File inputFile = fileResource("testdata", fileName); | ||
| if (inputFile.exists()) { | ||
| File outputFile = tempFileResource(fileName); | ||
| if (!outputFile.getParentFile().exists()) { | ||
| outputFile.getParentFile().mkdirs(); | ||
| } | ||
| boolean result = copy(inputFile, outputFile); | ||
| assertTrue(result); | ||
| return outputFile; | ||
| } else { | ||
| log.warn("Sourcefile does not exist '" + inputFile + "'"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copy a File | ||
| * | ||
| * @param fromFile The existing File | ||
| * @param toFile The new File | ||
| * @return <code>true</code> if and only if the renaming succeeded; | ||
| * <code>false</code> otherwise | ||
| */ | ||
| public boolean copy(File fromFile, File toFile) { | ||
| try ( | ||
| FileInputStream fins = new FileInputStream((fromFile)); | ||
| FileOutputStream fouts = new FileOutputStream((toFile)) | ||
| ) { | ||
| fins.transferTo(fouts); | ||
| fouts.flush(); | ||
|
|
||
| // cleanupif files are not the same length | ||
| long length1 = fromFile.length(); | ||
| long length2 = toFile.length(); | ||
| if (length1 != length2) { | ||
| toFile.delete(); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } catch (IOException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public File tempFileResource(String fileName) { | ||
| File file; | ||
| if (fileName == null) { | ||
| file = tempDirectory; | ||
| } else { | ||
| file = new File(tempDirectory, fileName); | ||
| } | ||
| return file; | ||
| } | ||
|
|
||
| public File fileResource(String directory, String fileName) { | ||
| String dir; | ||
| if (!directory.endsWith("/")) { | ||
| dir = directory + "/"; | ||
| } else { | ||
| dir = directory; | ||
| } | ||
| File file = null; | ||
| try { | ||
| URI resource; | ||
| if (fileName == null) { | ||
| resource = ClassLoader.getSystemResource(dir).toURI(); | ||
| } else { | ||
| resource = ClassLoader.getSystemResource(dir + fileName).toURI(); | ||
| } | ||
| file = new File(resource); | ||
| } catch (Exception e) { | ||
| log.error("Could not open resource '" + dir + fileName + "'"); | ||
| } | ||
| return file; | ||
| } | ||
|
|
||
| /** | ||
| * Copy audiofile to processing dir ready for use in test, use this if using | ||
| * same file in multiple tests because with junit multithreading can have | ||
| * problems otherwise | ||
| * | ||
| * @param fileName | ||
| * @return | ||
| */ | ||
| public File copyAudioToTmp(String fileName, String newFileName) { | ||
| File inputFile = fileResource("testdata", fileName); | ||
| File outputFile = tempFileResource(newFileName); | ||
| if (!outputFile.getParentFile().exists()) { | ||
| outputFile.getParentFile().mkdirs(); | ||
| } | ||
| boolean result = copy(inputFile, outputFile); | ||
| assertTrue(result); | ||
| return outputFile; | ||
| } | ||
|
|
||
| /** | ||
| * Prepends file with tag file in order to create an mp3 with a valid id3 | ||
| * | ||
| * @param tagfile | ||
| * @param fileName | ||
| * @return | ||
| */ | ||
| public File prependAudioToTmp(String tagfile, String fileName) { | ||
| File inputTagFile = fileResource("testtagdata", tagfile); | ||
| File inputFile = fileResource("testdata", fileName); | ||
| File outputFile = tempFileResource(fileName); | ||
| if (!outputFile.getParentFile().exists()) { | ||
| outputFile.getParentFile().mkdirs(); | ||
| } | ||
| boolean result = append(inputTagFile, inputFile, outputFile); | ||
| assertTrue(result); | ||
| return outputFile; | ||
| } | ||
|
|
||
| private boolean append(File fromFile1, File fromFile2, File toFile) { | ||
| try { | ||
| FileInputStream in = new FileInputStream(fromFile1); | ||
| FileInputStream in2 = new FileInputStream(fromFile2); | ||
| FileOutputStream out = new FileOutputStream(toFile); | ||
| BufferedInputStream inBuffer = new BufferedInputStream(in); | ||
| BufferedInputStream inBuffer2 = new BufferedInputStream(in2); | ||
| BufferedOutputStream outBuffer = new BufferedOutputStream(out); | ||
|
|
||
| int theByte; | ||
|
|
||
| while ((theByte = inBuffer.read()) > -1) { | ||
| outBuffer.write(theByte); | ||
| } | ||
|
|
||
| while ((theByte = inBuffer2.read()) > -1) { | ||
| outBuffer.write(theByte); | ||
| } | ||
|
|
||
| outBuffer.close(); | ||
| inBuffer.close(); | ||
| inBuffer2.close(); | ||
| out.close(); | ||
| in.close(); | ||
| in2.close(); | ||
|
|
||
| // cleanupif files are not the same length | ||
| if ((fromFile1.length() + fromFile2.length()) != toFile.length()) { | ||
| toFile.delete(); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } catch (IOException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public File fileResource(String directory) { | ||
| return fileResource(directory, null); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most stuff here moved from AbstractTestCase and deals with resources needed in the tests.