diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..08aced9 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,26 @@ +# +# The MIT License +# +# Copyright (C) 2015 Asterios Raptis +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + + diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..c761e02 --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,84 @@ +# Code of Conduct + +## 1. Purpose + +A primary goal of silly-io is to be inclusive to the largest number of contributors, with the most varied and diverse backgrounds possible. As such, we are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual orientation, ability, ethnicity, socioeconomic status, and religion (or lack thereof). + +This code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior. + +We invite all those who participate in silly-io to help us create safe and positive experiences for everyone. + +## 2. Open Source Citizenship + +A supplemental goal of this Code of Conduct is to increase open source citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community. + +Communities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society. + +If you see someone who is making an extra effort to ensure our community is welcoming, friendly, and encourages all participants to contribute to the fullest extent, we want to know. + +## 3. Expected Behavior + +The following behaviors are expected and requested of all community members: + +* Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community. +* Exercise consideration and respect in your speech and actions. +* Attempt collaboration before conflict. +* Refrain from demeaning, discriminatory, or harassing behavior and speech. +* Be mindful of your surroundings and of your fellow participants. Alert community leaders if you notice a dangerous situation, someone in distress, or violations of this Code of Conduct, even if they seem inconsequential. +* Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations. + +## 4. Unacceptable Behavior + +The following behaviors are considered harassment and are unacceptable within our community: + +* Violence, threats of violence or violent language directed against another person. +* Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language. +* Posting or displaying sexually explicit or violent material. +* Posting or threatening to post other people’s personally identifying information ("doxing"). +* Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability. +* Inappropriate photography or recording. +* Inappropriate physical contact. You should have someone’s consent before touching them. +* Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances. +* Deliberate intimidation, stalking or following (online or in person). +* Advocating for, or encouraging, any of the above behavior. +* Sustained disruption of community events, including talks and presentations. + +## 5. Consequences of Unacceptable Behavior + +Unacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated. + +Anyone asked to stop unacceptable behavior is expected to comply immediately. + +If a community member engages in unacceptable behavior, the community organizers may take any action they deem appropriate, up to and including a temporary ban or permanent expulsion from the community without warning (and without refund in the case of a paid event). + +## 6. Reporting Guidelines + +If you are subject to or witness unacceptable behavior, or have any other concerns, please notify a community organizer as soon as possible. asterios.raptis@web.de. + + + +Additionally, community organizers are available to help community members engage with local law enforcement or to otherwise help those experiencing unacceptable behavior feel safe. In the context of in-person events, organizers will also provide escorts as desired by the person experiencing distress. + +## 7. Addressing Grievances + +If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify astrapi69 with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies. + + + +## 8. Scope + +We expect all community participants (contributors, paid or otherwise; sponsors; and other guests) to abide by this Code of Conduct in all community venues–online and in-person–as well as in all one-on-one communications pertaining to community business. + +This code of conduct and its related procedures also applies to unacceptable behavior occurring outside the scope of community activities when such behavior has the potential to adversely affect the safety and well-being of community members. + +## 9. Contact info + +asterios.raptis@web.de + +## 10. License and attribution + +This Code of Conduct is distributed under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/). + +Portions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy). + +Retrieved on November 22, 2016 from [http://citizencodeofconduct.org/](http://citizencodeofconduct.org/) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..95219ce --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# How to Contribute + +Your are welcome to create pull requests or join in our mailing list for bugfix, doc, example, suggestion or anything else. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..99a868d --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +Fixes # . + +Changes proposed in this pull request: + +- + +@astrapi69 diff --git a/.gitignore b/.gitignore index a1c2a23..88f9ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,87 @@ -# Compiled class file +################## +# Compiled files # +################## *.class -# Log file -*.log +################## +# intellij files # +################## +*.iws +*.iml +*.ipr +!.idea/runConfigurations +.idea/inspectionProfiles +.idea/libraries +.idea/codeStyles +.idea/*.* + +################# +# eclipse files # +################# +/.project +/.classpath +/.settings +/.tern-project + +########### +### STS ### +########### +.apt_generated +.factorypath +.springBeans +.sts4-cache -# BlueJ files -*.ctxt -# Mobile Tools for Java (J2ME) -.mtj.tmp/ +################ +### NetBeans ### +################ +/nbproject/private/ +/nbbuild/ +/nbdist/ +/.nb-gradle/ +/build/ -# Package Files # +######################### +# maven generated files # +######################### +/target +/.mvn/ +*.versionsBackup + +############# +# Zip files # +############# +*.tar +*.tar.gz +*.zip +*.7z +*.dmg +*.gz +*.iso *.jar *.war -*.nar *.ear -*.zip -*.tar.gz *.rar -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* +############## +# Logs files # +############## +*.log + +################# +# test-ng files # +################# +/test-output + +############################ +# Binaries generated files # +############################ +/bin + +################ +# gradle files # +################ +/build +/.gradle +/gradle +/pom.xml.bak diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..49e7e73 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +# silly-io .travis.yml + +language: java + +cache: + directories: + - $HOME/.m2 + +jdk: + - oraclejdk8 + +notifications: + email: + recipients: + - asterios.raptis@web.de + on_failure: always +after_success: + - mvn clean cobertura:cobertura org.eluder.coveralls:coveralls-maven-plugin:report diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..eb2d1e0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +## Change log +---------------------- + +Version 1 +------------- + +ADDED: + +- this changelog file +- created PULL_REQUEST_TEMPLATE.md file +- created CODE_OF_CONDUCT.md file +- created CONTRIBUTING.md file +- provide package.html for the javadoc of packages +- moved classes from obsolet jcommons-lang project + + diff --git a/README.md b/README.md index 0eb491e..f53f06d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,122 @@ -# silly-io -java classes for create In- and OuputStreams and Reader or Writer for files +# Overview + +
+ +[![Build Status](https://travis-ci.org/astrapi69/silly-io.svg?branch=develop)](https://travis-ci.org/astrapi69/silly-io) +[![Coverage Status](https://coveralls.io/repos/github/astrapi69/silly-io/badge.svg?branch=develop)](https://coveralls.io/github/astrapi69/silly-io?branch=develop) +[![Open Issues](https://img.shields.io/github/issues/astrapi69/silly-io.svg?style=flat)](https://github.com/astrapi69/silly-io/issues) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.alpharogroup/silly-io/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.alpharogroup/silly-io) +[![Javadocs](http://www.javadoc.io/badge/de.alpharogroup/silly-io.svg)](http://www.javadoc.io/doc/de.alpharogroup/silly-io) +[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](http://opensource.org/licenses/MIT) + +
+ +Utility library that provides java classes for create In- and OuputStream and Reader or Writer for file operations + +If you like this project put a ⭐ and donate + +# Donations + +If you like this library, please consider a donation through paypal: +PayPal this + + +or over bitcoin or bitcoin-cash with: + +36JxRRDfRazLNqUV6NsywCw1q7TK38ukpC + +or over ether with: + +0x588Aa02De98B1Ef70afeDC3ec5290130a3E5e273 + +or over flattr: + + +Flattr this + + +## Note + +No animals were harmed in the making of this library. + +## License + +The source code comes under the liberal MIT License, making silly-io great for all types of applications. + +## Maven dependency + +Maven dependency is now on sonatype. +Check out [sonatype repository](https://oss.sonatype.org/index.html#nexus-search;gav~de.alpharogroup~silly-io~~~) for latest snapshots and releases. + +Add the following maven dependency to your project `pom.xml` if you want to import the core functionality of silly-io: + +Than you can add the dependency to your dependencies: + + + ... + + 1 + ... + + ... + + ... + + + de.alpharogroup + silly-io + ${silly-io.version} + + ... + + +## Semantic Versioning + +The versions of silly-io are maintained with the Simplified Semantic Versioning guidelines. + +Release version numbers will be incremented in the following format: + +`..` + +For detailed information on versioning for this project you can visit this [wiki page](https://github.com/lightblueseas/mvn-parent-projects/wiki/Simplified-Semantic-Versioning). + +## Want to Help and improve it? ### + +The source code for silly-io are on GitHub. Please feel free to fork and send pull requests! + +Create your own fork of [astrapi69/silly-io/fork](https://github.com/astrapi69/silly-io/fork) + +To share your changes, [submit a pull request](https://github.com/astrapi69/silly-io/pull/new/develop). + +Don't forget to add new units tests on your changes. + +## Contacting the Developers + +Do not hesitate to contact the silly-io developers with your questions, concerns, comments, bug reports, or feature requests. +- Feature requests, questions and bug reports can be reported at the [issues page](https://github.com/astrapi69/silly-io/issues). + +## Credits + +|**Travis CI**| +| :---: | +|[![Travis CI](https://travis-ci.com/images/logos/TravisCI-Full-Color.png)](https://coveralls.io/github/astrapi69/silly-io?branch=master)| +|Special thanks to [Travis CI](https://travis-ci.org) for providing a free continuous integration service for open source projects| +| | + +|**Nexus Sonatype repositories**| +| :---: | +|[![sonatype repository](https://img.shields.io/nexus/r/https/oss.sonatype.org/de.alpharogroup/silly-io.svg?style=for-the-badge)](https://oss.sonatype.org/index.html#nexus-search;gav~de.alpharogroup~silly-io~~~)| +|Special thanks to [sonatype repository](https://www.sonatype.com) for providing a free maven repository service for open source projects| +| | + +|**coveralls.io**| +| :---: | +|[![Coverage Status](https://coveralls.io/repos/github/astrapi69/silly-io/badge.svg?branch=develop)](https://coveralls.io/github/astrapi69/silly-io?branch=master)| +|Special thanks to [coveralls.io](https://coveralls.io) for providing a free code coverage for open source projects| +| | + +|**javadoc.io**| +| :---: | +|[![Javadocs](http://www.javadoc.io/badge/de.alpharogroup/silly-io.svg)](http://www.javadoc.io/doc/de.alpharogroup/silly-io)| +|Special thanks to [javadoc.io](http://www.javadoc.io) for providing a free javadoc documentation for open source projects| +| | diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..da3fcca --- /dev/null +++ b/pom.xml @@ -0,0 +1,235 @@ + + + 4.0.0 + + + de.alpharogroup + mvn-java-parent + 4.8 + + + silly-io + 1 + + ${project.artifactId} + + Provides java classes for create In- and OuputStream and Reader or Writer for file operations + + https://github.com/astrapi69/${project.artifactId} + + + + 5.0.1 + + 2.6 + + 3 + + 5.1 + + + + + MIT License + http://www.opensource.org/licenses/mit-license.php + repo + + + + + scm:git:git:@github.com:astrapi69/${project.artifactId}.git + scm:git:git@github.com:astrapi69/${project.artifactId}.git + git:@github.com:astrapi69/${project.artifactId}.git + + + + github + https://github.com/astrapi69/${project.artifactId}/issues + + + + travis-ci + https://travis-ci.org/astrapi69/${project.artifactId} + + + + + + + + + commons-io + commons-io + ${commons-io.version} + + + + de.alpharogroup + test-objects + ${test-objects.version} + + + + de.alpharogroup + jobj-core + ${jobj-core.version} + + + + de.alpharogroup + vintage-time + ${vintage-time.version} + + + + + + + + + commons-io + commons-io + test + + + + org.meanbean + meanbean + test + + + + de.alpharogroup + test-objects + test + + + + de.alpharogroup + jobj-core + test + + + + de.alpharogroup + vintage-time + test + + + + + + + + + + + false + src/main/java + + ** + + + **/*.java + **/package.html + + + + + false + src/main/resources + + + + false + src/site + + + + + + + + false + src/test/java + + ** + + + **/*.java + **/package.html + + + + false + src/test/resources + + + + + + + + + + org.codehaus.mojo + cobertura-maven-plugin + + + + + + + + + + + + + + de/alpharogroup/io/annotations/ImportResource.class + de/alpharogroup/io/annotations/ImportResources.class + + + de/alpharogroup/io/StreamExtensions.class + + + + + + + + + + + + + clean + + + + + + + + + + + + + true + maven-compiler-plugin + + + + true + maven-source-plugin + + + + + + + \ No newline at end of file diff --git a/src/launch/silly-io clean deploy.launch b/src/launch/silly-io clean deploy.launch new file mode 100644 index 0000000..d624b5b --- /dev/null +++ b/src/launch/silly-io clean deploy.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io clean install.launch b/src/launch/silly-io clean install.launch new file mode 100644 index 0000000..22aba02 --- /dev/null +++ b/src/launch/silly-io clean install.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io clean package.launch b/src/launch/silly-io clean package.launch new file mode 100644 index 0000000..a5d51aa --- /dev/null +++ b/src/launch/silly-io clean package.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io eclipse clean eclipse eclipse clean install.launch b/src/launch/silly-io eclipse clean eclipse eclipse clean install.launch new file mode 100644 index 0000000..d7b5ef7 --- /dev/null +++ b/src/launch/silly-io eclipse clean eclipse eclipse clean install.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io javadoc.launch b/src/launch/silly-io javadoc.launch new file mode 100644 index 0000000..d30ef14 --- /dev/null +++ b/src/launch/silly-io javadoc.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io update-license-header.launch b/src/launch/silly-io update-license-header.launch new file mode 100644 index 0000000..638ec69 --- /dev/null +++ b/src/launch/silly-io update-license-header.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/silly-io versions display-dependency-updates.launch b/src/launch/silly-io versions display-dependency-updates.launch new file mode 100644 index 0000000..4e69349 --- /dev/null +++ b/src/launch/silly-io versions display-dependency-updates.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/java/de/alpharogroup/io/SerializedObjectExtensions.java b/src/main/java/de/alpharogroup/io/SerializedObjectExtensions.java new file mode 100644 index 0000000..7fb55e0 --- /dev/null +++ b/src/main/java/de/alpharogroup/io/SerializedObjectExtensions.java @@ -0,0 +1,144 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import lombok.experimental.UtilityClass; + +/** + * Helper-class for read from and write to serialized objects. + * + * @version 1.0 + * @author Asterios Raptis + */ +@UtilityClass +public final class SerializedObjectExtensions +{ + + /** + * Reads the object from the given file. + * + * @param file + * In that file is the object saved. + * @return The object in the file or null. + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + public static Object readSerializedObjectFromFile(final File file) + throws IOException, ClassNotFoundException + { + Object object = null; + try (FileInputStream fis = new FileInputStream(file); + ObjectInputStream in = new ObjectInputStream(fis);) + { + object = in.readObject(); + in.close(); + } + return object; + } + + /** + * The Method toByteArray() serialize an Object to byte array. + * + * @param + * the generic type of the given object + * @param object + * The Object to convert into a byte array. + * @return The byte array from the Object. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static byte[] toByteArray(final T object) throws IOException + { + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);) + { + objectOutputStream.writeObject(object); + return byteArrayOutputStream.toByteArray(); + } + } + + /** + * The Method toObject() converts the given byte array into an Object. + * + * @param byteArray + * The byte array to convert into an Object. + * @return The Object the was converted from the byte array. + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException + { + Object object = null; + try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);) + { + object = objectInputStream.readObject(); + objectInputStream.close(); + } + return object; + } + + /** + * Writes the given object to the given File. + * + * @param obj + * The object to write to the File. + * @param file + * In this file will be the object saved. + * @return true if the object was written to the file otherwise false. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static boolean writeSerializedObjectToFile(final Object obj, final File file) + throws IOException + { + boolean written = false; + try (FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos);) + { + oos.writeObject(obj); + oos.flush(); + oos.close(); + written = true; + } + return written; + } + +} diff --git a/src/main/java/de/alpharogroup/io/StreamExtensions.java b/src/main/java/de/alpharogroup/io/StreamExtensions.java new file mode 100644 index 0000000..4060d4c --- /dev/null +++ b/src/main/java/de/alpharogroup/io/StreamExtensions.java @@ -0,0 +1,357 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectStreamClass; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Serializable; +import java.io.Writer; + +import lombok.experimental.UtilityClass; + +/** + * Utility class for input/output operations. + * + * @version 1.0 + * @author Asterios Raptis + */ +@UtilityClass +public final class StreamExtensions implements Serializable +{ + + /** + * The serialVersionUID. + */ + private static final long serialVersionUID = 5042445056004440533L; + + /** + * Returns the given InputStream as a byte array. + * + * @param in + * The InputStream. + * @return Returns the given InputStream as a byte array. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static byte[] getByteArray(final InputStream in) throws IOException + { + return getByteArray(in, new ByteArrayOutputStream(in.available())); + } + + /** + * Gets the byte array. + * + * @param in + * The InputStream. + * @param os + * The ByteArrayOutputStream. + * @return the byte array + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static byte[] getByteArray(final InputStream in, final ByteArrayOutputStream os) + throws IOException + { + int byt; + while ((byt = in.read()) != -1) + { + os.write(byt); + } + return os.toByteArray(); + } + + /** + * Gets the input stream from a File object. + * + * @param file + * the file. + * @return the input stream. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static InputStream getInputStream(final File file) throws IOException + { + return getInputStream(file, false); + } + + /** + * Gets the input stream from a File object. + * + * @param file + * the file + * @param createFile + * If true and the file does not exist it will be create a new file. + * @return the input stream + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static InputStream getInputStream(final File file, final boolean createFile) + throws IOException + { + InputStream is = null; + if (file.exists()) + { + is = file.toURI().toURL().openStream(); + } + else + { + if (createFile) + { + file.createNewFile(); + is = file.toURI().toURL().openStream(); + } + else + { + throw new FileNotFoundException("File " + file.getName() + " does not exist."); + } + } + return is; + } + + /** + * Gets the output stream from a File object. + * + * @param file + * the file. + * @return the output stream. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static OutputStream getOutputStream(final File file) throws IOException + { + return getOutputStream(file, false); + } + + /** + * Gets a {@link BufferedOutputStream} from the given {@link File} object. + * + * @param file + * the file + * @param createFile + * If true and the file does not exist it will be create a new file. + * @return the output stream + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static OutputStream getOutputStream(final File file, final boolean createFile) + throws IOException + { + OutputStream os = null; + BufferedOutputStream bos = null; + if (file.exists()) + { + os = new FileOutputStream(file); + } + else + { + if (createFile) + { + file.createNewFile(); + os = new FileOutputStream(file); + } + else + { + throw new FileNotFoundException("File " + file.getName() + " does not exist."); + } + } + bos = new BufferedOutputStream(os); + return bos; + } + + /** + * Gets a Reader from the given file object. + * + * @param inputFile + * the input file. + * @return the reader. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Reader getReader(final File inputFile) throws IOException + { + return getReader(inputFile, null, false); + } + + /** + * Gets a Reader from the given file object. + * + * @param inputFile + * the input file + * @param encoding + * The encoding from the file. + * @param createFile + * If true and the file does not exist it will be create a new file. + * @return the reader + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Reader getReader(final File inputFile, final String encoding, + final boolean createFile) throws IOException + { + FileInputStream fis = null; + InputStreamReader isr = null; + BufferedReader reader = null; + if (inputFile.exists()) + { + fis = new FileInputStream(inputFile); + } + else + { + if (createFile) + { + inputFile.createNewFile(); + fis = new FileInputStream(inputFile); + } + else + { + throw new FileNotFoundException("File " + inputFile.getName() + " does not exist."); + } + } + if (null == encoding) + { + isr = new InputStreamReader(fis); + } + else + { + isr = new InputStreamReader(fis, encoding); + } + // create the bufferedreader + reader = new BufferedReader(isr); + + return reader; + } + + /** + * Gets the SerialVersionUID from the given Class. + * + * @param clazz + * The class + * @return the serial version uid + */ + public static long getSerialVersionUID(final Class clazz) + { + return ObjectStreamClass.lookup(clazz).getSerialVersionUID(); + } + + /** + * Gets a Writer from the given file object. + * + * @param file + * the file. + * @return the Writer. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Writer getWriter(final File file) throws IOException + { + return getWriter(file, null, false); + } + + /** + * Gets a Writer from the given file object. + * + * @param file + * the file + * @param encoding + * The encoding from the file. + * @param createFile + * the create file + * @return the Writer. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Writer getWriter(final File file, final String encoding, final boolean createFile) + throws IOException + { + PrintWriter printWriter = null; + BufferedOutputStream bos = null; + FileOutputStream fos = null; + OutputStreamWriter osw = null; + if (file.exists()) + { + fos = new FileOutputStream(file); + } + else + { + if (createFile) + { + file.createNewFile(); + fos = new FileOutputStream(file); + } + else + { + throw new FileNotFoundException("File " + file.getName() + "does not exist."); + } + } + bos = new BufferedOutputStream(fos); + if (null == encoding) + { + osw = new OutputStreamWriter(bos); + } + else + { + osw = new OutputStreamWriter(bos, encoding); + } + printWriter = new PrintWriter(osw); + + return printWriter; + + } + + /** + * The Method writeInputStreamToOutputStream(InputStream, OutputStream, boolean) writes to the + * given OutputStream from an opened InputStream. + * + * @param inputStream + * The opened InputStream. + * @param outputStream + * The opened OutputStream. + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void writeInputStreamToOutputStream(final InputStream inputStream, + final OutputStream outputStream) throws IOException + { + int byt; + while ((byt = inputStream.read()) != -1) + { + outputStream.write(byt); + } + } + +} diff --git a/src/main/java/de/alpharogroup/io/annotations/ImportResource.java b/src/main/java/de/alpharogroup/io/annotations/ImportResource.java new file mode 100644 index 0000000..14a75dc --- /dev/null +++ b/src/main/java/de/alpharogroup/io/annotations/ImportResource.java @@ -0,0 +1,66 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The annotation {@link ImportResource} can be used to import a resource. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ImportResource { + + /** + * The index from the resource. + * + * @return the index from the resource. + */ + int index() default 0; + + /** + * The resource name. + * + * @return the resource name. + */ + String resourceName(); + + /** + * The resource type i.e. javascript, css, png etc. + * + * @return The resource type + */ + String resourceType(); + + /** + * The "skin" style of the resource. + * + * @return The "skin" style of the resource. + */ + String style() default ""; +} diff --git a/src/main/java/de/alpharogroup/io/annotations/ImportResources.java b/src/main/java/de/alpharogroup/io/annotations/ImportResources.java new file mode 100644 index 0000000..40835d0 --- /dev/null +++ b/src/main/java/de/alpharogroup/io/annotations/ImportResources.java @@ -0,0 +1,45 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The annotation {@link ImportResources} contains an Array of {@link ImportResource} objects. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ImportResources { + + /** + * The array of {@link ImportResource} objects. + * + * @return the array of {@link ImportResource} objects. + */ + ImportResource[] resources(); +} \ No newline at end of file diff --git a/src/main/java/de/alpharogroup/io/annotations/package.html b/src/main/java/de/alpharogroup/io/annotations/package.html new file mode 100644 index 0000000..ddbc60d --- /dev/null +++ b/src/main/java/de/alpharogroup/io/annotations/package.html @@ -0,0 +1,11 @@ + + + + + de.alpharogroup.io.annotations + + + + This package provides annotations and classes for importing resources + + \ No newline at end of file diff --git a/src/main/java/de/alpharogroup/io/package.html b/src/main/java/de/alpharogroup/io/package.html new file mode 100644 index 0000000..e7f620e --- /dev/null +++ b/src/main/java/de/alpharogroup/io/package.html @@ -0,0 +1,11 @@ + + + + + de.alpharogroup.io + + + + This package provides utility classes for reading serializable objects from files and stream operations + + diff --git a/src/main/resources/LICENSE.txt b/src/main/resources/LICENSE.txt new file mode 100644 index 0000000..9160e17 --- /dev/null +++ b/src/main/resources/LICENSE.txt @@ -0,0 +1,22 @@ +The MIT License + +Copyright (C) ${year} ${owner} + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/test/java/de/alpharogroup/io/SerializedObjectExtensionsTest.java b/src/test/java/de/alpharogroup/io/SerializedObjectExtensionsTest.java new file mode 100644 index 0000000..d6a2679 --- /dev/null +++ b/src/test/java/de/alpharogroup/io/SerializedObjectExtensionsTest.java @@ -0,0 +1,162 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.Date; + +import org.meanbean.factories.ObjectCreationException; +import org.meanbean.test.BeanTestException; +import org.meanbean.test.BeanTester; +import org.testng.annotations.Test; + +import de.alpharogroup.BaseTestCase; +import de.alpharogroup.date.CreateDateExtensions; +import de.alpharogroup.test.objects.Person; + +/** + * The unit test class for the class {@link SerializedObjectExtensions}. + * + * @version 1.0 + * @author Asterios Raptis + */ +public class SerializedObjectExtensionsTest extends BaseTestCase +{ + + /** + * Test method for {@link SerializedObjectExtensions#readSerializedObjectFromFile(File)} . + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test(enabled = true) + public void testReadSerializedObjectFromFile() throws IOException, ClassNotFoundException + { + final Date birthdayFromNiko = CreateDateExtensions.newDate(2007, 11, 8); + final File writeInMe = new File(".", "testWriteSerializedObjectToFile.dat"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromNiko, + writeInMe); + assertTrue("", actual); + final Object readedObjectFromFile = SerializedObjectExtensions + .readSerializedObjectFromFile(writeInMe); + final Date readedObj = (Date)readedObjectFromFile; + actual = birthdayFromNiko.equals(readedObj); + assertTrue("", actual); + try + { + writeInMe.deleteOnExit(); + } + catch (final Exception e) + { + // ignore... + } + } + + /** + * Test method for {@link SerializedObjectExtensions#toByteArray(Object)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test + public void testToByteArray() throws IOException, ClassNotFoundException + { + Person expected; + Person actual; + expected = Person.builder().build(); + byte[] byteArray = SerializedObjectExtensions.toByteArray(expected); + assertNotNull(byteArray); + actual = (Person)SerializedObjectExtensions.toObject(byteArray); + assertEquals(expected, actual); + } + + /** + * Test method for {@link SerializedObjectExtensions#toObject(byte[])} + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test + public void testToObject() throws ClassNotFoundException, IOException + { + String expected; + String actual; + final byte[] byteArray = { -84, -19, 0, 5, 116, 0, 7, 70, 111, 111, 32, 98, 97, 114 }; + expected = "Foo bar"; + actual = (String)SerializedObjectExtensions.toObject(byteArray); + assertEquals(expected, actual); + } + + /** + * Test method for {@link SerializedObjectExtensions} + */ + @Test(expectedExceptions = { BeanTestException.class, ObjectCreationException.class }) + public void testWithBeanTester() + { + final BeanTester beanTester = new BeanTester(); + beanTester.testBean(SerializedObjectExtensions.class); + } + + /** + * Test method for {@link SerializedObjectExtensions#writeSerializedObjectToFile(Object, File)} + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test(enabled = true) + public void testWriteSerializedObjectToFile() throws IOException, ClassNotFoundException + { + + final Date birthdayFromNiko = CreateDateExtensions.newDate(2007, 11, 8); + final File writeInMe = new File(".", "testWriteSerializedObjectToFile.dat"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromNiko, + writeInMe); + assertTrue("", actual); + final Object readedObjectFromFile = SerializedObjectExtensions + .readSerializedObjectFromFile(writeInMe); + final Date readedObj = (Date)readedObjectFromFile; + actual = birthdayFromNiko.equals(readedObj); + assertTrue("", actual); + + } + +} diff --git a/src/test/java/de/alpharogroup/io/StreamExtensionsTest.java b/src/test/java/de/alpharogroup/io/StreamExtensionsTest.java new file mode 100644 index 0000000..7463e06 --- /dev/null +++ b/src/test/java/de/alpharogroup/io/StreamExtensionsTest.java @@ -0,0 +1,320 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.io; + +import static org.testng.Assert.assertNotNull; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Date; + +import org.apache.commons.io.FileUtils; +import org.meanbean.factories.ObjectCreationException; +import org.meanbean.test.BeanTestException; +import org.meanbean.test.BeanTester; +import org.testng.annotations.Test; + +import de.alpharogroup.BaseTestCase; +import de.alpharogroup.date.CreateDateExtensions; +import de.alpharogroup.lang.ClassExtensions; +import de.alpharogroup.test.objects.Person; + +/** + * The unit test class for the class {@link StreamExtensions}. + */ +public class StreamExtensionsTest extends BaseTestCase +{ + /** The file. */ + final String propertiesFilename = "de/alpharogroup/lang/resources.properties"; + + /** + * Test get byte array input stream. + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test(enabled = true) + public void testGetByteArrayInputStream() throws IOException, ClassNotFoundException + { + final Date birthdayFromLeonardo = CreateDateExtensions.newDate(2012, 4, 19); + final File writeInMe = new File(".", "testGetByteArrayInputStream.dat"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromLeonardo, + writeInMe); + assertTrue("", actual); + final InputStream is = writeInMe.toURI().toURL().openStream(); + final byte[] ba = StreamExtensions.getByteArray(is); + assertTrue(ba.length > 0); + final Object obj = SerializedObjectExtensions.toObject(ba); + final Date readedObj = (Date)obj; + assertEquals(birthdayFromLeonardo, readedObj); + FileUtils.deleteQuietly(writeInMe); + } + + /** + * Test get byte array input stream byte array output stream. + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * Signals that an ClassNotFoundException has occurred. if a class was not found. + */ + @Test(enabled = true) + public void testGetByteArrayInputStreamByteArrayOutputStream() + throws IOException, ClassNotFoundException + { + final Date birthdayFromLeonardo = CreateDateExtensions.newDate(2012, 4, 19); + final File writeInMe = new File(".", + "testGetByteArrayInputStreamByteArrayOutputStream.dat"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromLeonardo, + writeInMe); + assertTrue("", actual); + final InputStream is = writeInMe.toURI().toURL().openStream(); + final byte[] ba = StreamExtensions.getByteArray(is, + new ByteArrayOutputStream(is.available())); + assertTrue(ba.length > 0); + final Object obj = SerializedObjectExtensions.toObject(ba); + final Date readedObj = (Date)obj; + assertEquals(birthdayFromLeonardo, readedObj); + FileUtils.deleteQuietly(writeInMe); + } + + /** + * Test method for {@link StreamExtensions#getInputStream(File)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test + public void testGetInputStreamFile() throws IOException + { + final Date birthdayFromLeonardo = CreateDateExtensions.newDate(2012, 4, 19); + final File writeInMe = new File(".", "testGetInputStreamFile.dat"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromLeonardo, + writeInMe); + assertTrue("", actual); + InputStream inputStream = StreamExtensions.getInputStream(writeInMe); + assertNotNull(inputStream); + FileUtils.deleteQuietly(writeInMe); + } + + /** + * Test method for {@link StreamExtensions#getInputStream(File, boolean)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test + public void testGetInputStreamFileBoolean() throws IOException + { + final File writeInMe = new File(".", "testGetInputStreamFileBoolean.dat"); + + InputStream inputStream = StreamExtensions.getInputStream(writeInMe, true); + assertNotNull(inputStream); + FileUtils.deleteQuietly(writeInMe); + } + + + /** + * Test method for {@link StreamExtensions#getInputStream(File, boolean)} in case of the file + * not exists and the flag createFile is false. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test(expectedExceptions = FileNotFoundException.class) + public void testGetInputStreamFileBooleanFalse() throws IOException + { + StreamExtensions.getInputStream(new File(".", "testGetInputStreamFileBooleanFalse.dat"), + false); + } + + /** + * Test method for {@link StreamExtensions#getOutputStream(File)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test + public void testGetOutputStreamFile() throws IOException + { + File fileout = new File(".", "testGetOutputStreamFile.out"); + OutputStream outputStream = StreamExtensions.getOutputStream(fileout, true); + assertNotNull(outputStream); + + FileUtils.deleteQuietly(fileout); + } + + /** + * Test method for {@link StreamExtensions#getOutputStream(File, boolean)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test(expectedExceptions = FileNotFoundException.class) + public void testGetOutputStreamFileBooleanFalse() throws IOException + { + StreamExtensions.getOutputStream(new File(".", "testGetOutputStreamFileBooleanFalse.dat"), + false); + } + + /** + * Test method for {@link StreamExtensions#getReader(File)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test + public void testGetReaderFile() throws IOException + { + File inputFile = new File(".", "testGetReaderFile.in"); + Reader reader = StreamExtensions.getReader(inputFile, null, true); + assertNotNull(reader); + + reader = StreamExtensions.getReader(inputFile, "UTF-8", true); + assertNotNull(reader); + + FileUtils.deleteQuietly(inputFile); + } + + /** + * Test method for {@link StreamExtensions#getReader(File, String, boolean)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test(expectedExceptions = FileNotFoundException.class) + public void testGetReaderFileStringBooleanFalse() throws IOException + { + StreamExtensions.getReader(new File(".", "testGetReaderFileStringBooleanFalse.out"), null, + false); + } + + /** + * Test get serial version uid. + */ + @SuppressWarnings("unchecked") + @Test(enabled = true) + public void testGetSerialVersionUID() + { + final Class personClass = (Class)new Person().getClass(); + final long serialVersionUID = StreamExtensions.getSerialVersionUID(personClass); + assertTrue("serialVersionUID should be 1L.", serialVersionUID == 1L); + } + + /** + * Test method for {@link StreamExtensions#getWriter(File)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws URISyntaxException + * if this URL is not formatted strictly according to to RFC2396 and cannot be + * converted to a URI. + */ + @Test + public void testGetWriterFile() throws IOException, URISyntaxException + { + final URL url = ClassExtensions.getResource(propertiesFilename); + Writer writer = StreamExtensions.getWriter(new File(url.toURI())); + assertNotNull(writer); + + File inputFile = new File(".", "testGetWriterFile.in"); + writer = StreamExtensions.getWriter(inputFile, "UTF-8", true); + assertNotNull(writer); + FileUtils.deleteQuietly(inputFile); + } + + /** + * Test method for {@link StreamExtensions#getWriter(File, String, boolean)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + */ + @Test(expectedExceptions = FileNotFoundException.class) + public void testGetWriterFileStringBooleanFalse() throws IOException + { + StreamExtensions.getWriter(new File(".", "testGetWriterFileStringBooleanFalse.out"), null, + false); + } + + + /** + * Test method for {@link StreamExtensions} + */ + @Test(expectedExceptions = { BeanTestException.class, ObjectCreationException.class }) + public void testWithBeanTester() + { + final BeanTester beanTester = new BeanTester(); + beanTester.testBean(StreamExtensions.class); + } + + /** + * Test method for + * {@link StreamExtensions#writeInputStreamToOutputStream(InputStream, OutputStream)}. + * + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws ClassNotFoundException + * is thrown when a class is not found in the classloader or no definition for the + * class with the specified name could be found. + */ + @Test + public void testWriteInputStreamToOutputStreamInputStreamOutputStream() + throws IOException, ClassNotFoundException + { + final Date birthdayFromLeonardo = CreateDateExtensions.newDate(2012, 4, 19); + final File writeInMe = new File(".", + "testWriteInputStreamToOutputStreamInputStreamOutputStream.in"); + actual = SerializedObjectExtensions.writeSerializedObjectToFile(birthdayFromLeonardo, + writeInMe); + InputStream inputStream = StreamExtensions.getInputStream(writeInMe, true); + + File fileout = new File(".", + "testWriteInputStreamToOutputStreamInputStreamOutputStream.out"); + try (final OutputStream outputStream = StreamExtensions.getOutputStream(fileout, true);) + { + StreamExtensions.writeInputStreamToOutputStream(inputStream, outputStream); + } + + final Object readedObjectFromFile = SerializedObjectExtensions + .readSerializedObjectFromFile(fileout); + assertEquals(readedObjectFromFile, birthdayFromLeonardo); + FileUtils.deleteQuietly(fileout); + FileUtils.deleteQuietly(writeInMe); + } + +} diff --git a/src/test/resources/de/alpharogroup/lang/resources.properties b/src/test/resources/de/alpharogroup/lang/resources.properties new file mode 100644 index 0000000..9af544f --- /dev/null +++ b/src/test/resources/de/alpharogroup/lang/resources.properties @@ -0,0 +1,3 @@ +testkey3=testvalue3 +testkey2=testvalue2 +testkey1=testvalue1