[JAVA-22519] Move articles from generic libraries module to specific library modules (#15416)
This commit is contained in:
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
|
||||
|
||||
public class ConvertContainerToAnother {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List convertToList() {
|
||||
UnifiedSet<String> cars = new UnifiedSet<>();
|
||||
|
||||
cars.add("Toyota");
|
||||
cars.add("Mercedes");
|
||||
cars.add("Volkswagen");
|
||||
|
||||
return cars.toList();
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Student {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private List<String> addresses;
|
||||
|
||||
public Student(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Student(String firstName, String lastName, List<String> addresses) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return this.firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return this.lastName;
|
||||
}
|
||||
|
||||
public List<String> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public void setAddresses(List<String> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.rome;
|
||||
|
||||
import com.sun.syndication.feed.synd.*;
|
||||
import com.sun.syndication.io.FeedException;
|
||||
import com.sun.syndication.io.SyndFeedInput;
|
||||
import com.sun.syndication.io.SyndFeedOutput;
|
||||
import com.sun.syndication.io.XmlReader;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class RSSRomeExample {
|
||||
|
||||
public static void main(String[] args) throws IOException, FeedException {
|
||||
SyndFeed feed = createFeed();
|
||||
addEntryToFeed(feed);
|
||||
publishFeed(feed);
|
||||
readFeed();
|
||||
}
|
||||
|
||||
private static SyndFeed createFeed() {
|
||||
SyndFeed feed = new SyndFeedImpl();
|
||||
feed.setFeedType("rss_1.0");
|
||||
feed.setTitle("Test title");
|
||||
feed.setLink("http://www.somelink.com");
|
||||
feed.setDescription("Basic description");
|
||||
|
||||
return feed;
|
||||
}
|
||||
|
||||
private static void addEntryToFeed(SyndFeed feed) {
|
||||
SyndEntry entry = new SyndEntryImpl();
|
||||
entry.setTitle("Entry title");
|
||||
entry.setLink("http://www.somelink.com/entry1");
|
||||
|
||||
addDescriptionToEntry(entry);
|
||||
addCategoryToEntry(entry);
|
||||
|
||||
feed.setEntries(Arrays.asList(entry));
|
||||
}
|
||||
|
||||
private static void addDescriptionToEntry(SyndEntry entry) {
|
||||
SyndContent description = new SyndContentImpl();
|
||||
description.setType("text/html");
|
||||
description.setValue("First entry");
|
||||
|
||||
entry.setDescription(description);
|
||||
}
|
||||
|
||||
private static void addCategoryToEntry(SyndEntry entry) {
|
||||
List<SyndCategory> categories = new ArrayList<>();
|
||||
SyndCategory category = new SyndCategoryImpl();
|
||||
category.setName("Sophisticated category");
|
||||
categories.add(category);
|
||||
|
||||
entry.setCategories(categories);
|
||||
}
|
||||
|
||||
private static void publishFeed(SyndFeed feed) throws IOException, FeedException {
|
||||
Writer writer = new FileWriter("xyz.txt");
|
||||
SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
|
||||
syndFeedOutput.output(feed, writer);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static SyndFeed readFeed() throws IOException, FeedException {
|
||||
URL feedSource = new URL("http://rssblog.whatisrss.com/feed/");
|
||||
SyndFeedInput input = new SyndFeedInput();
|
||||
return input.build(new XmlReader(feedSource));
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AllSatisfyPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
boolean result = list.allSatisfy(Predicates.greaterThan(0));
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnySatisfyPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
boolean result = list.anySatisfy(Predicates.greaterThan(30));
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CollectPatternUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCollect_thenCorrect() {
|
||||
Student student1 = new Student("John", "Hopkins");
|
||||
Student student2 = new Student("George", "Adams");
|
||||
|
||||
MutableList<Student> students = FastList.newListWith(student1, student2);
|
||||
|
||||
MutableList<String> lastNames = students.collect(Student::getLastName);
|
||||
|
||||
Assertions.assertThat(lastNames).containsExactly("Hopkins", "Adams");
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConvertContainerToAnotherUnitTest {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test
|
||||
public void whenConvertContainerToAnother_thenCorrect() {
|
||||
MutableList<String> cars = (MutableList) ConvertContainerToAnother.convertToList();
|
||||
|
||||
Assertions.assertThat(cars).containsExactlyElementsOf(FastList.newListWith("Volkswagen", "Toyota", "Mercedes"));
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DetectPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDetect_thenCorrect() {
|
||||
Integer result = list.detect(Predicates.greaterThan(30));
|
||||
|
||||
Assertions.assertThat(result).isEqualTo(41);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FlatCollectUnitTest {
|
||||
|
||||
MutableList<String> addresses1;
|
||||
MutableList<String> addresses2;
|
||||
MutableList<String> addresses3;
|
||||
MutableList<String> addresses4;
|
||||
|
||||
List<String> expectedAddresses;
|
||||
MutableList<Student> students;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
String address1 = "73 Pacific St., Forest Hills, NY 11375";
|
||||
String address2 = "93 Bayport Ave., South Richmond Hill, NY 11419";
|
||||
String address3 = "548 Market St, San Francisco, CA 94104";
|
||||
String address4 = "8605 Santa Monica Blvd, West Hollywood, CA 90069";
|
||||
|
||||
this.addresses1 = FastList.newListWith(address1, address2);
|
||||
this.addresses2 = FastList.newListWith(address3, address4);
|
||||
Student student1 = new Student("John", "Hopkins", addresses1);
|
||||
Student student2 = new Student("George", "Adams", addresses2);
|
||||
this.addresses2 = FastList.newListWith(address3, address4);
|
||||
this.students = FastList.newListWith(student1, student2);
|
||||
this.expectedAddresses = new ArrayList<>();
|
||||
this.expectedAddresses.add("73 Pacific St., Forest Hills, NY 11375");
|
||||
this.expectedAddresses.add("93 Bayport Ave., South Richmond Hill, NY 11419");
|
||||
this.expectedAddresses.add("548 Market St, San Francisco, CA 94104");
|
||||
this.expectedAddresses.add("8605 Santa Monica Blvd, West Hollywood, CA 90069");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlatCollect_thenCorrect() {
|
||||
MutableList<String> addresses = students.flatCollect(Student::getAddresses);
|
||||
|
||||
Assertions.assertThat(addresses).containsExactlyElementsOf(this.expectedAddresses);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.eclipse.collections.api.tuple.Pair;
|
||||
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
|
||||
import org.eclipse.collections.impl.tuple.Tuples;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ForEachPatternUnitTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void whenInstantiateAndChangeValues_thenCorrect() {
|
||||
Pair<Integer, String> pair1 = Tuples.pair(1, "One");
|
||||
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
|
||||
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
|
||||
|
||||
UnifiedMap<Integer, String> map = UnifiedMap.newMapWith(pair1, pair2, pair3);
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
map.put(i + 1, "New Value");
|
||||
}
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
Assert.assertEquals("New Value", map.get(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InjectIntoPatternUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenInjectInto_thenCorrect() {
|
||||
List<Integer> list = Lists.mutable.of(1, 2, 3, 4);
|
||||
int result = 5;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Integer v = list.get(i);
|
||||
result = result + v.intValue();
|
||||
}
|
||||
|
||||
assertEquals(15, result);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.LazyIterable;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LazyIterationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenLazyIteration_thenCorrect() {
|
||||
Student student1 = new Student("John", "Hopkins");
|
||||
Student student2 = new Student("George", "Adams");
|
||||
Student student3 = new Student("Jennifer", "Rodriguez");
|
||||
|
||||
MutableList<Student> students = Lists.mutable.with(student1, student2, student3);
|
||||
LazyIterable<Student> lazyStudents = students.asLazy();
|
||||
LazyIterable<String> lastNames = lazyStudents.collect(Student::getLastName);
|
||||
|
||||
Assertions.assertThat(lastNames).containsAll(Lists.mutable.with("Hopkins", "Adams", "Rodriguez"));
|
||||
}
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.block.predicate.Predicate;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.api.partition.list.PartitionMutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PartitionPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
MutableList<Integer> numbers = list;
|
||||
PartitionMutableList<Integer> partitionedFolks = numbers.partition(new Predicate<Integer>() {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -1551138743683678406L;
|
||||
|
||||
public boolean accept(Integer each) {
|
||||
return each > 30;
|
||||
}
|
||||
});
|
||||
MutableList<Integer> greaterThanThirty = partitionedFolks.getSelected().sortThis();
|
||||
MutableList<Integer> smallerThanThirty = partitionedFolks.getRejected().sortThis();
|
||||
|
||||
Assertions.assertThat(smallerThanThirty).containsExactly(1, 5, 8, 17, 23);
|
||||
Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RejectPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
MutableList<Integer> expectedList;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
this.expectedList = FastList.newListWith(1, 5, 8, 17, 23);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReject_thenCorrect() {
|
||||
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30)).sortThis();
|
||||
|
||||
Assertions.assertThat(notGreaterThanThirty).containsExactlyElementsOf(this.expectedList);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SelectPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListwhenSelect_thenCorrect() {
|
||||
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30)).sortThis();
|
||||
|
||||
Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public MutableList selectUsingLambda() {
|
||||
return list.select(each -> each > 30).sortThis();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void givenListwhenSelectUsingLambda_thenCorrect() {
|
||||
MutableList<Integer> greaterThanThirty = selectUsingLambda();
|
||||
|
||||
Assertions.assertThat(greaterThanThirty).containsExactly(31, 38, 41);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.api.tuple.Pair;
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.eclipse.collections.impl.tuple.Tuples;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ZipUnitTest {
|
||||
|
||||
MutableList<Pair<String, String>> expectedPairs;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setup() {
|
||||
Pair<String, String> pair1 = Tuples.pair("1", "Porsche");
|
||||
Pair<String, String> pair2 = Tuples.pair("2", "Volvo");
|
||||
Pair<String, String> pair3 = Tuples.pair("3", "Toyota");
|
||||
expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenZip_thenCorrect() {
|
||||
MutableList<String> numbers = Lists.mutable.with("1", "2", "3", "Ignored");
|
||||
MutableList<String> cars = Lists.mutable.with("Porsche", "Volvo", "Toyota");
|
||||
MutableList<Pair<String, String>> pairs = numbers.zip(cars);
|
||||
|
||||
Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.api.tuple.Pair;
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.eclipse.collections.impl.tuple.Tuples;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ZipWithIndexUnitTest {
|
||||
|
||||
MutableList<Pair<String, Integer>> expectedPairs;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setup() {
|
||||
Pair<String, Integer> pair1 = Tuples.pair("Porsche", 0);
|
||||
Pair<String, Integer> pair2 = Tuples.pair("Volvo", 1);
|
||||
Pair<String, Integer> pair3 = Tuples.pair("Toyota", 2);
|
||||
expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenZip_thenCorrect() {
|
||||
MutableList<String> cars = FastList.newListWith("Porsche", "Volvo", "Toyota");
|
||||
MutableList<Pair<String, Integer>> pairs = cars.zipWithIndex();
|
||||
|
||||
Assertions.assertThat(pairs).containsExactlyElementsOf(this.expectedPairs);
|
||||
}
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
package com.baeldung.io;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
public class JavaDirectoryDeleteUnitTest {
|
||||
private static Path TEMP_DIRECTORY;
|
||||
private static final String DIRECTORY_NAME = "toBeDeleted";
|
||||
|
||||
private static final List<String> ALL_LINES = Arrays.asList("This is line 1", "This is line 2", "This is line 3", "This is line 4", "This is line 5", "This is line 6");
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeTempDirectory() throws IOException {
|
||||
TEMP_DIRECTORY = Files.createTempDirectory("tmpForJUnit");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanTempDirectory() throws IOException {
|
||||
FileUtils.deleteDirectory(TEMP_DIRECTORY.toFile());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setupDirectory() throws IOException {
|
||||
Path tempPathForEachTest = Files.createDirectory(TEMP_DIRECTORY.resolve(DIRECTORY_NAME));
|
||||
|
||||
// Create a directory structure
|
||||
Files.write(tempPathForEachTest.resolve("file1.txt"), ALL_LINES.subList(0, 2));
|
||||
Files.write(tempPathForEachTest.resolve("file2.txt"), ALL_LINES.subList(2, 4));
|
||||
|
||||
Files.createDirectories(tempPathForEachTest.resolve("Empty"));
|
||||
|
||||
Path aSubDir = Files.createDirectories(tempPathForEachTest.resolve("notEmpty"));
|
||||
Files.write(aSubDir.resolve("file3.txt"), ALL_LINES.subList(3, 5));
|
||||
Files.write(aSubDir.resolve("file4.txt"), ALL_LINES.subList(0, 3));
|
||||
|
||||
aSubDir = Files.createDirectories(aSubDir.resolve("anotherSubDirectory"));
|
||||
Files.write(aSubDir.resolve("file5.txt"), ALL_LINES.subList(4, 5));
|
||||
Files.write(aSubDir.resolve("file6.txt"), ALL_LINES.subList(0, 2));
|
||||
}
|
||||
|
||||
@After
|
||||
public void checkAndCleanupIfRequired() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
if (Files.exists(pathToBeDeleted)) {
|
||||
FileUtils.deleteDirectory(pathToBeDeleted.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean deleteDirectory(File directoryToBeDeleted) {
|
||||
File[] allContents = directoryToBeDeleted.listFiles();
|
||||
|
||||
if (allContents != null) {
|
||||
for (File file : allContents) {
|
||||
deleteDirectory(file);
|
||||
}
|
||||
}
|
||||
|
||||
return directoryToBeDeleted.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithRecursion_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
boolean result = deleteDirectory(pathToBeDeleted.toFile());
|
||||
|
||||
assertTrue("Could not delete directory", result);
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithCommonsIOFileUtils_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
FileUtils.deleteDirectory(pathToBeDeleted.toFile());
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile());
|
||||
|
||||
assertTrue("Could not delete directory", result);
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithNIO2WalkFileTree_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
Files.walkFileTree(pathToBeDeleted, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.pcollections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.pcollections.HashPMap;
|
||||
import org.pcollections.HashTreePMap;
|
||||
import org.pcollections.HashTreePSet;
|
||||
import org.pcollections.MapPSet;
|
||||
import org.pcollections.PVector;
|
||||
import org.pcollections.TreePVector;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenEmpty_thenCreateEmptyHashPMap() {
|
||||
HashPMap<String, String> pmap = HashTreePMap.empty();
|
||||
assertEquals(pmap.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeyValue_whenSingleton_thenCreateNonEmptyHashPMap() {
|
||||
HashPMap<String, String> pmap1 = HashTreePMap.singleton("key1", "value1");
|
||||
assertEquals(pmap1.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingHashMap_whenFrom_thenCreateHashPMap() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("mkey1", "mval1");
|
||||
map.put("mkey2", "mval2");
|
||||
|
||||
HashPMap<String, String> pmap2 = HashTreePMap.from(map);
|
||||
assertEquals(pmap2.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenHashPMapMethods_thenPerformOperations() {
|
||||
|
||||
HashPMap<String, String> pmap = HashTreePMap.empty();
|
||||
HashPMap<String, String> pmap0 = pmap.plus("key1", "value1");
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("key2", "val2");
|
||||
map.put("key3", "val3");
|
||||
|
||||
HashPMap<String, String> pmap1 = pmap0.plusAll(map);
|
||||
HashPMap<String, String> pmap2 = pmap1.minus("key1");
|
||||
HashPMap<String, String> pmap3 = pmap2.minusAll(map.keySet());
|
||||
|
||||
assertEquals(pmap0.size(), 1);
|
||||
assertEquals(pmap1.size(), 3);
|
||||
assertFalse(pmap2.containsKey("key1"));
|
||||
assertEquals(pmap3.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTreePVectorMethods_thenPerformOperations() {
|
||||
TreePVector<String> pVector = TreePVector.empty();
|
||||
|
||||
TreePVector<String> pV1 = pVector.plus("e1");
|
||||
TreePVector<String> pV2 = pV1.plusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
|
||||
assertEquals(1, pV1.size());
|
||||
assertEquals(4, pV2.size());
|
||||
|
||||
TreePVector<String> pV3 = pV2.minus("e1");
|
||||
TreePVector<String> pV4 = pV3.minusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
|
||||
assertEquals(pV3.size(), 3);
|
||||
assertEquals(pV4.size(), 0);
|
||||
|
||||
TreePVector<String> pSub = pV2.subList(0, 2);
|
||||
assertTrue(pSub.contains("e1") && pSub.contains("e2"));
|
||||
|
||||
PVector<String> pVW = pV2.with(0, "e10");
|
||||
assertEquals(pVW.get(0), "e10");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapPSetMethods_thenPerformOperations() {
|
||||
|
||||
MapPSet pSet = HashTreePSet.empty().plusAll(Arrays.asList("e1", "e2", "e3", "e4"));
|
||||
assertEquals(pSet.size(), 4);
|
||||
|
||||
MapPSet pSet1 = pSet.minus("e4");
|
||||
assertFalse(pSet1.contains("e4"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user