-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(XmlHandler): get element from xpath
build(pom): updated, minor enhancement test(XmlHandler: tested get element from xpath use-cases chore: resource update
- Loading branch information
rikkarth
committed
Feb 24, 2024
1 parent
5fa1145
commit 00fc2a0
Showing
7 changed files
with
244 additions
and
10 deletions.
There are no files selected for viewing
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
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
102 changes: 102 additions & 0 deletions
102
src/main/java/pt/codeforge/toolertools/zip/BaseZipBuilder.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,102 @@ | ||
package pt.codeforge.toolertools.zip; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
public class BaseZipBuilder implements ZipBuilder { | ||
|
||
private final List<File> files = new ArrayList<>(); | ||
private Path targetPath; | ||
|
||
public BaseZipBuilder() { | ||
} | ||
|
||
public BaseZipBuilder(Path targetPath) { | ||
this.targetPath = targetPath; | ||
} | ||
|
||
public BaseZipBuilder(String targetPath) { | ||
this.targetPath = Paths.get(targetPath); | ||
} | ||
|
||
@Override | ||
public void createZip() { | ||
if (this.targetPath == null) { | ||
throw new IllegalStateException("Target path is not defined."); | ||
} | ||
|
||
try (FileOutputStream fos = new FileOutputStream(this.targetPath.toString()); | ||
ZipOutputStream zos = new ZipOutputStream(fos, StandardCharsets.UTF_8)) { | ||
|
||
/* if(this.files.get(0).isDirectory()){ | ||
String name = this.files.get(0).getAbsolutePath() + File.separator + "output\\test2.properties"; | ||
System.out.println(name); | ||
insertInZip(new File(name), zos); | ||
}*/ | ||
|
||
this.files.forEach(file -> insertInZip(file, zos)); | ||
} catch (IOException ioe) { | ||
throw new ZipBuilderException("Unable to create zip.", ioe); | ||
} | ||
} | ||
|
||
@Override | ||
public void addToZip(File file) { | ||
if (file == null) { | ||
throw new IllegalArgumentException("File cannot be null."); | ||
} | ||
|
||
this.files.add(file); | ||
} | ||
|
||
@Override | ||
public void addAllToZip(List<File> files) { | ||
if (files == null) { | ||
throw new IllegalArgumentException("File's list cannot be null."); | ||
} | ||
|
||
List<File> filteredFiles = files.stream().filter(Objects::nonNull).collect(Collectors.toList()); | ||
|
||
this.files.addAll(filteredFiles); | ||
} | ||
|
||
@Override | ||
public BaseZipBuilder setTargetPath(Path targetPath) { | ||
this.targetPath = targetPath; | ||
return this; | ||
} | ||
|
||
@Override | ||
public BaseZipBuilder setTargetPath(String targetPath) { | ||
this.targetPath = Paths.get(targetPath); | ||
return this; | ||
} | ||
|
||
@Override | ||
public int zipSize() { | ||
return this.files.size(); | ||
} | ||
|
||
private void insertInZip(File file, ZipOutputStream zos) { | ||
try { | ||
ZipEntry zipEntry = new ZipEntry(file.getName()); | ||
zos.putNextEntry(zipEntry); | ||
Files.copy(file.getAbsoluteFile().toPath(), zos); | ||
zos.closeEntry(); | ||
} catch (IOException ioe) { | ||
throw new ZipBuilderException("unable to add to zip", ioe); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/pt/codeforge/toolertools/zip/ZipBuilder.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,21 @@ | ||
package pt.codeforge.toolertools.zip; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public interface ZipBuilder { | ||
|
||
void createZip(); | ||
|
||
void addToZip(File file); | ||
|
||
void addAllToZip(List<File> files); | ||
|
||
int zipSize(); | ||
|
||
ZipBuilder setTargetPath(Path targetPath); | ||
|
||
ZipBuilder setTargetPath(String targetPath); | ||
|
||
} |
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
54 changes: 54 additions & 0 deletions
54
src/test/java/pt/codeforge/toolertools/zip/BaseZipBuilderTest.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,54 @@ | ||
package pt.codeforge.toolertools.zip; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BaseZipBuilderTest { | ||
|
||
@Test | ||
void givenNoTargetPath_testCreateZip_shouldThrowIllegalStateException() { | ||
BaseZipBuilder baseZipBuilder = new BaseZipBuilder(); | ||
|
||
assertThrows(IllegalStateException.class, baseZipBuilder::createZip); | ||
} | ||
|
||
@Test | ||
void givenIncorrectPath_testCreateZip_shouldThrowZipBuilderException(){ | ||
BaseZipBuilder baseZipBuilder = new BaseZipBuilder("src/test/resources/output"); | ||
|
||
assertThrows(ZipBuilderException.class, baseZipBuilder::createZip); | ||
} | ||
|
||
@Test | ||
void givenValidPath_testCreateZip(){ | ||
BaseZipBuilder baseZipBuilder = new BaseZipBuilder("src/test/resources/output/my.zip"); | ||
|
||
baseZipBuilder.createZip(); | ||
} | ||
|
||
@Test | ||
@Disabled("wip") | ||
void test() { | ||
BaseZipBuilder zipBuilder = new BaseZipBuilder().setTargetPath("src/test/resources/output/my.zip"); | ||
|
||
/*List<File> files = Arrays.asList( | ||
new File("src/test/resources/test_resource_1.xml"), | ||
new File("src/test/resources/test_resource_2.xml"), | ||
new File("src/test/resources/test.properties") | ||
);*/ | ||
|
||
List<File> files = Arrays.asList( | ||
new File("src/test/resources/") | ||
); | ||
|
||
zipBuilder.addAllToZip(files); | ||
|
||
zipBuilder.createZip(); | ||
} | ||
|
||
} |
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