http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java deleted file mode 100644 index 9d04204..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import org.junit.Test; - -public class PortTest { - @Test - public void assertValidShouldThrowOnNegativePort() { - assertThatThrownBy(() -> Port.assertValid(-1)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void assertValidShouldThrowOnZeroPort() { - assertThatThrownBy(() -> Port.assertValid(0)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void assertValidShouldAcceptOne() { - Port.assertValid(1); - } - - @Test - public void assertValidShouldAcceptMaxValue() { - Port.assertValid(Port.MAX_PORT_VALUE); - } - - @Test - public void assertValidShouldThrowOnTooBigValue() { - assertThatThrownBy(() -> Port.assertValid(Port.MAX_PORT_VALUE + 1)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void isValidShouldReturnFalseWhenNegative() { - assertThat(Port.isValid(-1)) - .isFalse(); - } - - @Test - public void isValidShouldReturnFalseWhenZero() { - assertThat(Port.isValid(0)) - .isFalse(); - } - - @Test - public void isValidShouldReturnTrueWhenOne() { - assertThat(Port.isValid(1)) - .isTrue(); - } - - @Test - public void isValidShouldReturnTrueWhenMaxValue() { - assertThat(Port.isValid(Port.MAX_PORT_VALUE)) - .isTrue(); - } - - @Test - public void isValidShouldReturnFalseWhenAboveMaxValue() { - assertThat(Port.isValid(Port.MAX_PORT_VALUE + 1)) - .isFalse(); - } - - @Test - public void generateValidUnprivilegedPortShouldReturnAValidPort() { - assertThat(Port.generateValidUnprivilegedPort()) - .isBetween(Port.PRIVILEGED_PORT_BOUND, Port.MAX_PORT_VALUE); - } - -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java deleted file mode 100644 index 1b2e86c..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.util.stream.Stream; - -import org.junit.Test; -import org.testcontainers.shaded.com.google.common.collect.ImmutableList; - -import com.github.steveash.guavate.Guavate; - -public class StreamUtilsTest { - - @Test - public void flattenShouldReturnEmptyWhenEmptyStreams() { - assertThat( - StreamUtils.<Integer>flatten(ImmutableList.of()) - .collect(Guavate.toImmutableList())) - .isEmpty(); - } - - @Test - public void flattenShouldPreserveSingleStreams() { - assertThat( - StreamUtils.flatten(ImmutableList.of( - Stream.of(1, 2, 3))) - .collect(Guavate.toImmutableList())) - .containsExactly(1, 2, 3); - } - - @Test - public void flattenShouldMergeSeveralStreamsTogether() { - assertThat( - StreamUtils.flatten(ImmutableList.of( - Stream.of(1, 2, 3), - Stream.of(4, 5))) - .collect(Guavate.toImmutableList())) - .containsExactly(1, 2, 3, 4, 5); - } - - @Test - public void flattenShouldAcceptEmptyStreams() { - assertThat( - StreamUtils.flatten(ImmutableList.of( - Stream.of())) - .collect(Guavate.toImmutableList())) - .isEmpty(); - } - - @Test - public void flattenShouldMergeEmptyStreamsWithOtherData() { - assertThat( - StreamUtils.flatten(ImmutableList.of( - Stream.of(1, 2), - Stream.of(), - Stream.of(3))) - .collect(Guavate.toImmutableList())) - .containsExactly(1, 2, 3); - } - - @Test - public void flattenShouldAcceptEmptyVarArg() { - assertThat( - StreamUtils.flatten() - .collect(Guavate.toImmutableList())) - .isEmpty(); - } - - @Test - public void flattenShouldThrowOnNullVarArg() { - Stream<String>[] streams = null; - assertThatThrownBy(() -> StreamUtils.flatten(streams).collect(Guavate.toImmutableList())) - .isInstanceOf(NullPointerException.class); - } - - @Test - public void flattenShouldFlattenNonEmptyVarArg() { - assertThat(StreamUtils.flatten(Stream.of(1), Stream.of(2)).collect(Guavate.toImmutableList())) - .containsExactly(1, 2); - } - - @Test - public void ofNullableShouldReturnEmptyStreamWhenNull() { - assertThat(StreamUtils.ofNullable(null) - .collect(Guavate.toImmutableList())) - .isEmpty(); - } - - @Test - public void ofNullableShouldReturnAStreamWithElementsOfTheArray() { - assertThat(StreamUtils.ofNullable(ImmutableList.of(1, 2).toArray()) - .collect(Guavate.toImmutableList())) - .containsExactly(1, 2); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/ValuePatchTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/ValuePatchTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/ValuePatchTest.java deleted file mode 100644 index ef84393..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/ValuePatchTest.java +++ /dev/null @@ -1,220 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy modifyTo the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.util.NoSuchElementException; -import java.util.Optional; - -import org.junit.Test; - -public class ValuePatchTest { - - public static final int REPLACEMENT_VALUE = 24; - public static final Optional<Integer> REPLACEMENT = Optional.of(REPLACEMENT_VALUE); - public static final int VALUE = 12; - public static final Optional<Integer> OPTIONAL_OF_VALUE = Optional.of(VALUE); - - @Test - public void keepShouldProduceKeptValues() { - assertThat(ValuePatch.<Integer>keep().isKept()).isTrue(); - } - - @Test - public void keepShouldThrowOnGet() { - assertThatThrownBy(() -> ValuePatch.<Integer>keep().get()).isInstanceOf(NoSuchElementException.class); - } - - @Test - public void keepShouldNotBeModified() { - assertThat(ValuePatch.<Integer>keep().isModified()).isFalse(); - } - - @Test - public void keepShouldNotBeRemoved() { - assertThat(ValuePatch.<Integer>keep().isRemoved()).isFalse(); - } - - @Test - public void removeShouldNotBeKept() { - assertThat(ValuePatch.<Integer>remove().isKept()).isFalse(); - } - - @Test - public void removeShouldBeRemoved() { - assertThat(ValuePatch.<Integer>remove().isRemoved()).isTrue(); - } - - @Test - public void removedShouldNotBeModified() { - assertThat(ValuePatch.<Integer>remove().isModified()).isFalse(); - } - - @Test - public void removeShouldThrowOnGet() { - assertThatThrownBy(() -> ValuePatch.<Integer>remove().get()).isInstanceOf(NoSuchElementException.class); - } - - @Test - public void ofNullableShouldBeEquivalentToRemoveWhenNullParameter() { - assertThat(ValuePatch.<Integer>ofNullable(null)).isEqualTo(ValuePatch.<Integer>remove()); - } - - @Test - public void ofNullableShouldBeEquivalentToModifyWhenNonNullParameter() { - assertThat(ValuePatch.ofNullable(VALUE)).isEqualTo(ValuePatch.modifyTo(VALUE)); - } - - @Test - public void modifyToShouldNotBeKept() { - assertThat(ValuePatch.modifyTo(VALUE).isKept()).isFalse(); - } - - @Test - public void modifyToShouldNotBeRemoved() { - assertThat(ValuePatch.modifyTo(VALUE).isRemoved()).isFalse(); - } - - @Test - public void modifyToShouldBeModified() { - assertThat(ValuePatch.modifyTo(VALUE).isModified()).isTrue(); - } - - @Test - public void modifyToShouldThrowOnNullValue() { - assertThatThrownBy(() -> ValuePatch.modifyTo(null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void modifyToShouldBeRetrievedByGet() { - assertThat(ValuePatch.modifyTo(VALUE).get()).isEqualTo(VALUE); - } - - @Test - public void ofOptionalShouldThrowOnNullValue() { - assertThatThrownBy(() -> ValuePatch.ofOptional(null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void ofOptionalShouldBeEquivalentToModifyToWhenPresent() { - assertThat(ValuePatch.ofOptional(OPTIONAL_OF_VALUE)).isEqualTo(ValuePatch.modifyTo(VALUE)); - } - - @Test - public void ofOptionalShouldBeEquivalentToRemoveWhenEmpty() { - assertThat(ValuePatch.ofOptional(Optional.empty())).isEqualTo(ValuePatch.remove()); - } - - @Test - public void notKeptOrElseShouldReturnElseWhenKept() { - assertThat(ValuePatch.<Integer>keep().notKeptOrElse(REPLACEMENT)).isEqualTo(REPLACEMENT); - } - - @Test - public void notKeptOrElseShouldReturnEmptyWhenRemoved() { - assertThat(ValuePatch.<Integer>remove().notKeptOrElse(REPLACEMENT)).isEqualTo(Optional.empty()); - } - - @Test - public void notKeptOrElseShouldReturnOptionalWhenModified() { - assertThat(ValuePatch.modifyTo(VALUE).notKeptOrElse(REPLACEMENT)).isEqualTo(OPTIONAL_OF_VALUE); - } - - @Test - public void toOptionalShouldReturnElseWhenKept() { - assertThat(ValuePatch.<Integer>keep().toOptional()).isEqualTo(Optional.empty()); - } - - @Test - public void toOptionalShouldReturnEmptyWhenRemoved() { - assertThat(ValuePatch.<Integer>remove().toOptional()).isEqualTo(Optional.empty()); - } - - @Test - public void toOptionalShouldReturnOptionalWhenModified() { - assertThat(ValuePatch.modifyTo(VALUE).toOptional()).isEqualTo(OPTIONAL_OF_VALUE); - } - - @Test - public void getOrElseShouldReturnReplacementWhenKept() { - assertThat(ValuePatch.<Integer>keep().getOrElse(REPLACEMENT_VALUE)).isEqualTo(REPLACEMENT_VALUE); - } - - @Test - public void getOrElseShouldReturnReplacementWhenRemoved() { - assertThat(ValuePatch.<Integer>remove().getOrElse(REPLACEMENT_VALUE)).isEqualTo(REPLACEMENT_VALUE); - } - - @Test - public void getOrElseShouldReturnValueWhenPresent() { - assertThat(ValuePatch.modifyTo(VALUE).getOrElse(REPLACEMENT_VALUE)).isEqualTo(VALUE); - } - - @Test - public void getOrElseShouldReturnNullWhenKeptAndNullSpecified() { - assertThat(ValuePatch.<Integer>keep().getOrElse(null)).isNull(); - } - - @Test - public void getOrElseShouldReturnNullWhenRemovedAndNullSpecified() { - assertThat(ValuePatch.<Integer>remove().getOrElse(null)).isNull(); - } - - @Test - public void getOrElseShouldReturnValueWhenPresentAndNullSpecified() { - assertThat(ValuePatch.modifyTo(VALUE).getOrElse(null)).isEqualTo(VALUE); - } - - @Test - public void mapNotKeptToValueShouldPreserveKept() { - assertThat( - ValuePatch.<Integer>keep() - .mapNotKeptToOptional(optional -> optional.map(i -> i + 1).orElse(REPLACEMENT_VALUE))) - .isEmpty(); - } - - @Test - public void mapNotKeptToValueShouldTransformOf() { - assertThat( - ValuePatch.modifyTo(VALUE) - .mapNotKeptToOptional(optional -> optional.map(i -> i + 1).orElse(REPLACEMENT_VALUE))) - .contains(VALUE + 1); - } - - @Test - public void mapNotKeptToValueShouldTransformRemoved() { - assertThat( - ValuePatch.<Integer>remove() - .mapNotKeptToOptional(optional -> optional.map(i -> i + 1).orElse(REPLACEMENT_VALUE))) - .contains(REPLACEMENT_VALUE); - } - - @Test - public void mapNotKeptToValueShouldThrowWhenNull() { - assertThatThrownBy( - () -> ValuePatch.modifyTo(12) - .mapNotKeptToOptional(any -> null) - .isPresent()) - .isInstanceOf(NullPointerException.class); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/date/ImapDateTimeFormatterTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/date/ImapDateTimeFormatterTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/date/ImapDateTimeFormatterTest.java deleted file mode 100644 index ec970fb..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/date/ImapDateTimeFormatterTest.java +++ /dev/null @@ -1,252 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.util.date; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.time.DayOfWeek; -import java.time.Month; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class ImapDateTimeFormatterTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void dayOfWeekShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("Wed, 28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getDayOfWeek()).isEqualTo(DayOfWeek.WEDNESDAY); - } - - @Test - public void parseShouldNotThrowWhenDayOfWeekIsAbsent() { - ZonedDateTime.parse("28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenDayOfWeekIsWrong() { - expectedException.expect(DateTimeParseException.class); - // must be wednesday - ZonedDateTime.parse("Mon, 28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenDayOfWeekIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Abc, 28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void dayOfWeekShouldBeParsedWhenOneDigit() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getDayOfMonth()).isEqualTo(3); - } - - @Test - public void dayOfWeekShouldBeParsedWhenTwoDigits() { - ZonedDateTime dateTime = ZonedDateTime.parse("13 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getDayOfMonth()).isEqualTo(13); - } - - @Test - public void parseShouldThrowWhenDayOfMonthIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenDayOfMonthIsNegative() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("-2 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenDayOfMonthIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("64 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void monthOfYearShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("Wed, 28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getMonth()).isEqualTo(Month.JUNE); - } - - @Test - public void parseShouldThrowWhenMonthOfYearIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Wed, 28 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenMonthOfYearIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Wed, 28 Abc 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void yearShouldBeParsedWhenFourDigits() { - ZonedDateTime dateTime = ZonedDateTime.parse("Wed, 28 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getYear()).isEqualTo(2017); - } - - @Test - public void yearShouldBeParsedWhenTwoDigitsGreaterThanInitialYear() { - ZonedDateTime dateTime = ZonedDateTime.parse("28 Jun 77 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getYear()).isEqualTo(1977); - } - - @Test - public void yearShouldBeParsedWhenTwoDigitsLesserThanInitialYear() { - ZonedDateTime dateTime = ZonedDateTime.parse("28 Jun 64 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getYear()).isEqualTo(2064); - } - - @Test - public void parseShouldThrowWhenYearIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Wed, 28 Jun 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenYearIsLesserThanTwoDigits() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Wed, 28 Jun 1 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenYearIsGreaterThanFourDigits() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("Wed, 28 Jun 12345 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void hourOfDayShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getHour()).isEqualTo(4); - } - - @Test - public void parseShouldNotThrowWhenHourOfDayIsLesserThanTwoDigits() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 4:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getHour()).isEqualTo(4); - } - - @Test - public void parseShouldThrowWhenHourOfDayIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 :35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenHourOfDayIsGreaterThanTwoDigits() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 123:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenHourOfDayIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 48:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void minuteOfHourShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getMinute()).isEqualTo(35); - } - - @Test - public void parseShouldNotThrowWhenMinuteOfHourIsLesserThanTwoDigits() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:5:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getMinute()).isEqualTo(5); - } - - @Test - public void parseShouldThrowWhenMinuteOfHourIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04::11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenMinuteOfHourIsGreaterThanTwoDigits() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:123:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenMinuteOfHourDayIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:72:11 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void secondOfMinuteShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:11 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getSecond()).isEqualTo(11); - } - - @Test - public void parseShouldNotThrowWhenSecondOfMinuteIsLesserThanTwoDigits() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:1 -0700", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getSecond()).isEqualTo(1); - } - - @Test - public void parseShouldNotThrowWhenSecondOfMinuteIsAbsent() { - ZonedDateTime.parse("28 Jun 2017 04:35 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenSecondOfMinuteIsGreaterThanTwoDigits() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:35:123 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenSecondOfMinuteDayIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:35:78 -0700", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void offsetShouldBeParsed() { - ZonedDateTime dateTime = ZonedDateTime.parse("3 Jun 2017 04:35:11 -0712", ImapDateTimeFormatter.rfc5322()); - assertThat(dateTime.getOffset()).isEqualTo(ZoneOffset.ofHoursMinutes(-7, -12)); - } - - @Test - public void parseShouldThrowWhenOffsetIsAbsent() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:35:11", ImapDateTimeFormatter.rfc5322()); - } - - @Test - public void parseShouldThrowWhenOffsetIsUnknow() { - expectedException.expect(DateTimeParseException.class); - ZonedDateTime.parse("3 Jun 2017 04:35:11 +7894", ImapDateTimeFormatter.rfc5322()); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/mime/MessageContentExtractorTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/mime/MessageContentExtractorTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/mime/MessageContentExtractorTest.java deleted file mode 100644 index deae849..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/mime/MessageContentExtractorTest.java +++ /dev/null @@ -1,514 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.util.mime; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.Optional; - -import javax.mail.internet.MimeMessage; - -import org.apache.james.mime4j.dom.Message; -import org.apache.james.mime4j.dom.Multipart; -import org.apache.james.mime4j.field.Fields; -import org.apache.james.mime4j.message.BasicBodyFactory; -import org.apache.james.mime4j.message.BodyPart; -import org.apache.james.mime4j.message.BodyPartBuilder; -import org.apache.james.mime4j.message.HeaderImpl; -import org.apache.james.mime4j.message.MultipartBuilder; -import org.apache.james.mime4j.stream.Field; -import org.apache.james.mime4j.util.ByteSequence; -import org.apache.james.util.mime.MessageContentExtractor.MessageContent; -import org.junit.Before; -import org.junit.Test; - -public class MessageContentExtractorTest { - private static final String BINARY_CONTENT = "binary"; - private static final String TEXT_CONTENT = "text content"; - private static final String HTML_CONTENT = "<b>html</b> content"; - private static final String TEXT_CONTENT2 = "other text content"; - private static final String HTML_CONTENT2 = "other <b>html</b> content"; - private static final String ATTACHMENT_CONTENT = "attachment content"; - private static final String ANY_VALUE = "anyValue"; - private static final Field CONTENT_ID_FIELD = new Field() { - @Override - public String getName() { - return MessageContentExtractor.CONTENT_ID; - } - - @Override - public String getBody() { - return ANY_VALUE; - } - - @Override - public ByteSequence getRaw() { - return ByteSequence.EMPTY; - } - }; - - private MessageContentExtractor testee; - - private BodyPartBuilder htmlPart; - private BodyPartBuilder textPart; - private BodyPartBuilder textAttachment; - private BodyPartBuilder inlineText; - private BodyPartBuilder inlineImage; - - @Before - public void setup() throws IOException { - testee = new MessageContentExtractor(); - textPart = BodyPartBuilder.create().setBody(TEXT_CONTENT, "plain", StandardCharsets.UTF_8); - htmlPart = BodyPartBuilder.create().setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8); - textAttachment = BodyPartBuilder.create() - .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) - .setContentDisposition("attachment"); - inlineText = BodyPartBuilder.create() - .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) - .setContentDisposition("inline"); - inlineImage = BodyPartBuilder.create() - .setBody(new byte[0], "image/png") - .setContentDisposition("inline"); - } - - @Test - public void extractShouldReturnEmptyWhenBinaryContentOnly() throws IOException { - Message message = Message.Builder.of() - .setBody(BasicBodyFactory.INSTANCE.binaryBody(BINARY_CONTENT, StandardCharsets.UTF_8)) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).isEmpty(); - } - - @Test - public void extractShouldReturnTextOnlyWhenTextOnlyBody() throws IOException { - Message message = Message.Builder.of() - .setBody(TEXT_CONTENT, StandardCharsets.UTF_8) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).isEmpty(); - } - - @Test - public void extractShouldReturnHtmlOnlyWhenHtmlOnlyBody() throws IOException { - Message message = Message.Builder.of() - .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnHtmlAndTextWhenMultipartAlternative() throws IOException { - Multipart multipart = MultipartBuilder.create("alternative") - .addBodyPart(textPart) - .addBodyPart(htmlPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnHtmlWhenMultipartAlternativeWithoutPlainPart() throws IOException { - Multipart multipart = MultipartBuilder.create("alternative") - .addBodyPart(htmlPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnTextWhenMultipartAlternativeWithoutHtmlPart() throws IOException { - Multipart multipart = MultipartBuilder.create("alternative") - .addBodyPart(textPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).isEmpty(); - } - - @Test - public void extractShouldReturnFirstNonAttachmentPartWhenMultipartMixed() throws IOException { - Multipart multipart = MultipartBuilder.create("mixed") - .addBodyPart(textAttachment) - .addBodyPart(htmlPart) - .addBodyPart(textPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - assertThat(actual.getTextBody()).isEmpty(); - } - - @Test - public void extractShouldReturnInlinedTextBodyWithoutCIDWhenNoOtherValidParts() throws IOException { - String textBody = "body 1"; - Multipart multipart = MultipartBuilder.create("report") - .addBodyPart(BodyPartBuilder.create() - .setBody(textBody, "plain", StandardCharsets.UTF_8) - .setContentDisposition("inline")) - .addBodyPart(BodyPartBuilder.create() - .setBody("body 2", "rfc822-headers", StandardCharsets.UTF_8) - .setContentDisposition("inline")) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - - MessageContent actual = testee.extract(message); - - assertThat(actual.getTextBody()).contains(textBody); - } - - @Test - public void extractShouldReturnEmptyWhenMultipartMixedAndFirstPartIsATextAttachment() throws IOException { - Multipart multipart = MultipartBuilder.create("mixed") - .addBodyPart(textAttachment) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).isEmpty(); - } - - @Test - public void extractShouldReturnFirstPartOnlyWhenMultipartMixedAndFirstPartIsHtml() throws IOException { - Multipart multipart = MultipartBuilder.create("mixed") - .addBodyPart(htmlPart) - .addBodyPart(textPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnHtmlAndTextWhenMultipartMixedAndFirstPartIsMultipartAlternative() throws IOException { - BodyPart multipartAlternative = BodyPartBuilder.create() - .setBody(MultipartBuilder.create("alternative") - .addBodyPart(htmlPart) - .addBodyPart(textPart) - .build()) - .build(); - Multipart multipartMixed = MultipartBuilder.create("mixed") - .addBodyPart(multipartAlternative) - .build(); - Message message = Message.Builder.of() - .setBody(multipartMixed) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnHtmlWhenMultipartRelated() throws IOException { - Multipart multipart = MultipartBuilder.create("related") - .addBodyPart(htmlPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipart) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).isEmpty(); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldReturnHtmlAndTextWhenMultipartAlternativeAndFirstPartIsMultipartRelated() throws IOException { - BodyPart multipartRelated = BodyPartBuilder.create() - .setBody(MultipartBuilder.create("related") - .addBodyPart(htmlPart) - .build()) - .build(); - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(multipartRelated) - .build(); - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { - //Given - BodyPart inlinedHTMLPart = BodyPartBuilder.create() - .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) - .build(); - HeaderImpl inlinedHeader = new HeaderImpl(); - inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); - inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); - inlinedHTMLPart.setHeader(inlinedHeader); - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(inlinedHTMLPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - - //When - MessageContent actual = testee.extract(message); - - //Then - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException { - //Given - BodyPart inlinedHTMLPart = BodyPartBuilder.create() - .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) - .build(); - HeaderImpl inlinedHeader = new HeaderImpl(); - inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); - inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); - inlinedHeader.addField(CONTENT_ID_FIELD); - inlinedHTMLPart.setHeader(inlinedHeader); - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(inlinedHTMLPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - - //When - MessageContent actual = testee.extract(message); - - //Then - assertThat(actual.getHtmlBody()).isEmpty(); - } - - - @Test - public void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { - //Given - BodyPart inlinedTextPart = BodyPartBuilder.create() - .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) - .build(); - HeaderImpl inlinedHeader = new HeaderImpl(); - inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); - inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); - inlinedTextPart.setHeader(inlinedHeader); - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(inlinedTextPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - - //When - MessageContent actual = testee.extract(message); - - //Then - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - } - - @Test - public void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException { - //Given - BodyPart inlinedTextPart = BodyPartBuilder.create() - .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) - .build(); - HeaderImpl inlinedHeader = new HeaderImpl(); - inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); - inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); - inlinedHeader.addField(CONTENT_ID_FIELD); - inlinedTextPart.setHeader(inlinedHeader); - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(inlinedTextPart) - .build(); - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - - //When - MessageContent actual = testee.extract(message); - - //Then - assertThat(actual.getTextBody()).isEmpty(); - } - - @Test - public void extractShouldRetrieveTextAndHtmlBodyWhenOneInlinedTextAttachmentAndMainContentInMultipart() throws IOException { - BodyPart multipartAlternative = BodyPartBuilder.create() - .setBody(MultipartBuilder.create("alternative") - .addBodyPart(textPart) - .addBodyPart(htmlPart) - .build()) - .build(); - - Multipart multipartMixed = MultipartBuilder.create("mixed") - .addBodyPart(multipartAlternative) - .addBodyPart(inlineText) - .build(); - - Message message = Message.Builder.of() - .setBody(multipartMixed) - .build(); - - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void extractShouldRetrieveTextBodyAndHtmlBodyWhenTextBodyInMainMultipartAndHtmlBodyInInnerMultipart() throws IOException { - BodyPart multipartRelated = BodyPartBuilder.create() - .setBody(MultipartBuilder.create("related") - .addBodyPart(htmlPart) - .addBodyPart(inlineImage) - .build()) - .build(); - - Multipart multipartAlternative = MultipartBuilder.create("alternative") - .addBodyPart(textPart) - .addBodyPart(multipartRelated) - .build(); - - Message message = Message.Builder.of() - .setBody(multipartAlternative) - .build(); - - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(TEXT_CONTENT); - assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); - } - - @Test - public void mergeMessageContentShouldReturnEmptyWhenAllEmpty() { - MessageContent messageContent1 = MessageContent.empty(); - MessageContent messageContent2 = MessageContent.empty(); - MessageContent expected = MessageContent.empty(); - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void mergeMessageContentShouldReturnFirstWhenSecondEmpty() { - MessageContent messageContent1 = new MessageContent(Optional.of(TEXT_CONTENT), Optional.of(HTML_CONTENT)); - MessageContent messageContent2 = MessageContent.empty(); - MessageContent expected = messageContent1; - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void mergeMessageContentShouldReturnSecondWhenFirstEmpty() { - MessageContent messageContent1 = MessageContent.empty(); - MessageContent messageContent2 = new MessageContent(Optional.of(TEXT_CONTENT), Optional.of(HTML_CONTENT)); - MessageContent expected = messageContent2; - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void mergeMessageContentShouldReturnMixWhenFirstTextOnlyAndSecondHtmlOnly() { - MessageContent messageContent1 = MessageContent.ofTextOnly(Optional.of(TEXT_CONTENT)); - MessageContent messageContent2 = MessageContent.ofHtmlOnly(Optional.of(HTML_CONTENT)); - MessageContent expected = new MessageContent(Optional.of(TEXT_CONTENT), Optional.of(HTML_CONTENT)); - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void mergeMessageContentShouldReturnMixWhenFirstHtmlOnlyAndSecondTextOnly() { - MessageContent messageContent1 = MessageContent.ofHtmlOnly(Optional.of(HTML_CONTENT)); - MessageContent messageContent2 = MessageContent.ofTextOnly(Optional.of(TEXT_CONTENT)); - MessageContent expected = new MessageContent(Optional.of(TEXT_CONTENT), Optional.of(HTML_CONTENT)); - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void mergeMessageContentShouldReturnFirstWhenTwiceAreComplete() { - MessageContent messageContent1 = new MessageContent(Optional.of(TEXT_CONTENT), Optional.of(HTML_CONTENT)); - MessageContent messageContent2 = new MessageContent(Optional.of(TEXT_CONTENT2), Optional.of(HTML_CONTENT2)); - MessageContent expected = messageContent1; - - MessageContent actual = messageContent1.merge(messageContent2); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void extractShouldRespectCharsetWhenOtherThanUTF8() throws IOException { - String text = "éééé\r\nèèèè\r\nà à à à "; - Message message = Message.Builder.of() - .setBody(text, Charset.forName("windows-1252")) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(text); - } - - @Test - public void extractShouldRespectCharsetWhenUTF8() throws IOException { - String text = "éééé\r\nèèèè\r\nà à à à "; - Message message = Message.Builder.of() - .setBody(text, StandardCharsets.UTF_8) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains(text); - } - - @Test - public void extractShouldUseUSASCIIWhenNoCharset() throws IOException { - String text = "éééé\r\nèèèè\r\nà à à à "; - Message message = Message.Builder.of() - .setBody(text, null) - .build(); - MessageContent actual = testee.extract(message); - assertThat(actual.getTextBody()).contains("????\r\n????\r\n????"); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/streams/ImmutableCollectorsTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/ImmutableCollectorsTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/ImmutableCollectorsTest.java deleted file mode 100644 index 8c9c98b..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/ImmutableCollectorsTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util.streams; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.data.MapEntry.entry; - -import java.util.Arrays; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import org.junit.Test; - -import com.github.steveash.guavate.Guavate; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; - -public class ImmutableCollectorsTest { - - @Test - public void immutableListCollectorShouldReturnEmptyImmutableListWhenEmptyStream() { - String[] data = {}; - List<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableList()); - assertThat(actual).isInstanceOf(ImmutableList.class); - assertThat(actual).isEmpty(); - } - - @Test - public void immutableListCollectorShouldReturnImmutableListWhenOneElementStream() { - String[] data = {"a"}; - List<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableList()); - assertThat(actual).isInstanceOf(ImmutableList.class); - assertThat(actual).containsExactly("a"); - } - - @Test - public void immutableListCollectorShouldReturnImmutableListWhen3ElementsStream() { - String[] data = {"a", "b", "c"}; - List<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableList()); - assertThat(actual).isInstanceOf(ImmutableList.class); - assertThat(actual).containsExactly("a", "b", "c"); - } - - @Test - public void immutableSetCollectorShouldReturnEmptyImmutableSetWhenEmptyStream() { - String[] data = {}; - Set<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableSet()); - assertThat(actual).isInstanceOf(ImmutableSet.class); - assertThat(actual).isEmpty(); - } - - @Test - public void immutableSetCollectorShouldReturnImmutableSetWhenOneElementStream() { - String[] data = {"a"}; - Set<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableSet()); - assertThat(actual).isInstanceOf(ImmutableSet.class); - assertThat(actual).containsExactly("a"); - } - - @Test - public void immutableSetCollectorShouldReturnImmutableSetWhen3ElementsStream() { - String[] data = {"a", "b", "c"}; - Set<String> actual = Arrays.stream(data) - .collect(Guavate.toImmutableSet()); - assertThat(actual).isInstanceOf(ImmutableSet.class); - assertThat(actual).containsExactly("a", "b", "c"); - } - - - @Test - public void immutableMapCollectorShouldReturnEmptyImmutableMapWhenEmptyStream() { - String[] data = {}; - Map<String, Integer> actual = Arrays.stream(data) - .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); - assertThat(actual).isInstanceOf(ImmutableMap.class); - assertThat(actual).isEmpty(); - } - - @Test - public void immutableMapCollectorShouldReturnAppliedImmutableMapWhenOneElementStream() { - String[] data = {"a"}; - Map<String, Integer> actual = Arrays.stream(data) - .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); - assertThat(actual).isInstanceOf(ImmutableMap.class); - assertThat(actual).containsExactly(entry("A", 1)); - } - - @Test - public void immutableMapCollectorShouldReturnAppliedImmutableMapWhen3ElementsStream() { - String[] data = {"a", "bb", "ccc"}; - Map<String, Integer> actual = Arrays.stream(data) - .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); - assertThat(actual).isInstanceOf(ImmutableMap.class); - assertThat(actual).containsExactly(entry("A", 1), entry("BB", 2), entry("CCC", 3)); - } - -} - http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/streams/IteratorsTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/IteratorsTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/IteratorsTest.java deleted file mode 100644 index 4c63f1a..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/IteratorsTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util.streams; - -import static java.util.stream.Collectors.toList; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.stream.Stream; - -import org.junit.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.UnmodifiableIterator; - -public class IteratorsTest { - - @Test - public void toStreamShouldReturnEmptyStreamWhenEmptyIterator() { - //Given - UnmodifiableIterator<String> emptyIterator = ImmutableList.<String>of().iterator(); - - //When - Stream<String> actual = Iterators.toStream(emptyIterator); - - //Then - assertThat(actual.count()).isEqualTo(0); - } - - @Test - public void toStreamShouldReturnSameContent() { - //Given - UnmodifiableIterator<String> iterator = ImmutableList.of("a", "b", "c").iterator(); - - //When - Stream<String> actual = Iterators.toStream(iterator); - - //Then - assertThat(actual.collect(toList())).containsExactly("a", "b", "c"); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/streams/JamesCollectorsTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/JamesCollectorsTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/JamesCollectorsTest.java deleted file mode 100644 index 52f1c3a..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/JamesCollectorsTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util.streams; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import java.util.stream.Stream; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import com.github.steveash.guavate.Guavate; -import com.google.common.collect.ImmutableList; - -public class JamesCollectorsTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void chunkerShouldAcceptEmptyStrem() { - Stream<Integer> emptyStream = Stream.of(); - - assertThat(emptyStream.collect(JamesCollectors.chunker(10)) - .collect(Guavate.toImmutableList())) - .isEmpty(); - } - - @Test - public void chunkerShouldThrowOnZeroChunkSize() { - expectedException.expect(IllegalArgumentException.class); - - JamesCollectors.chunker(0); - } - - @Test - public void chunkerShouldThrowOnNegativeChunkSize() { - expectedException.expect(IllegalArgumentException.class); - - JamesCollectors.chunker(-1); - } - - @Test - public void chunkerShouldChunkMonoValueStreams() { - Stream<Integer> monoValueStream = Stream.of(1); - - List<List<Integer>> values = monoValueStream.collect(JamesCollectors.chunker(10)) - .map(ImmutableList::copyOf) - .collect(Guavate.toImmutableList()); - assertThat(values) - .isEqualTo(ImmutableList.of(ImmutableList.of(1))); - } - - @Test - public void chunkerShouldChunkStreamsSmallerThanChunkSize() { - Stream<Integer> stream = Stream.of(1, 2); - - List<List<Integer>> values = stream.collect(JamesCollectors.chunker(3)) - .map(ImmutableList::copyOf) - .collect(Guavate.toImmutableList()); - assertThat(values) - .isEqualTo(ImmutableList.of(ImmutableList.of(1, 2))); - } - - @Test - public void chunkerShouldChunkStreamsAsBigAsChunkSize() { - Stream<Integer> stream = Stream.of(1, 2, 3); - - List<List<Integer>> values = stream.collect(JamesCollectors.chunker(3)) - .map(ImmutableList::copyOf) - .collect(Guavate.toImmutableList()); - assertThat(values) - .isEqualTo(ImmutableList.of(ImmutableList.of(1, 2, 3))); - } - - @Test - public void chunkerShouldChunkStreamsBiggerThanChunkSize() { - Stream<Integer> stream = Stream.of(1, 2, 3, 4); - - List<List<Integer>> values = stream.collect(JamesCollectors.chunker(3)) - .map(ImmutableList::copyOf) - .collect(Guavate.toImmutableList()); - assertThat(values) - .isEqualTo(ImmutableList.of( - ImmutableList.of(1, 2, 3), - ImmutableList.of(4))); - } - - @Test - public void chunkerShouldChunkInSeveralBuckets() { - Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7); - - List<List<Integer>> values = stream.collect(JamesCollectors.chunker(3)) - .map(ImmutableList::copyOf) - .collect(Guavate.toImmutableList()); - assertThat(values) - .isEqualTo(ImmutableList.of( - ImmutableList.of(1, 2, 3), - ImmutableList.of(4, 5, 6), - ImmutableList.of(7))); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/streams/LimitTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/LimitTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/LimitTest.java deleted file mode 100644 index fcd92d8..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/LimitTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util.streams; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import java.util.Optional; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import com.github.steveash.guavate.Guavate; -import com.google.common.collect.ImmutableList; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public class LimitTest { - - private final List<Integer> aList = ImmutableList.of(1, 2, 3, 4, 5, 6); - - @Rule - public final ExpectedException expectedException = ExpectedException.none(); - - @Test - public void unlimitedShouldCreateLimitWithNoLimit() { - Limit testee = Limit.unlimited(); - assertThat(testee.getLimit()).isEqualTo(Optional.empty()); - } - - @Test - public void beanShouldRespectBeanContract() { - EqualsVerifier.forClass(Limit.class) - .verify(); - } - - @Test - public void unlimitedShouldCreateLimitThatDoesNotAffectStream() { - - Limit testee = Limit.unlimited(); - assertThat( - testee - .applyOnStream(aList.stream()) - .collect(Guavate.toImmutableList()) - ).isEqualTo(aList); - } - - @Test - public void limitShouldCreateLimitWithNoLimit() { - int expected = 3; - - Limit testee = Limit.limit(expected); - assertThat(testee.getLimit()) - .isEqualTo(Optional.of(expected)); - } - - @Test - public void limitShouldCreateLimitThatCorrectlyTruncateStream() { - Limit testee = Limit.limit(3); - - assertThat(testee - .applyOnStream(aList.stream()) - .collect(Guavate.toImmutableList()) - ).isEqualTo(ImmutableList.of(1, 2, 3)); - } - - @Test - public void limitShouldThrowAnErrorWhenCalledWithZero() { - expectedException.expect(IllegalArgumentException.class); - Limit.limit(0); - } - - - @Test - public void limitShouldThrowAnErrorWhenCalledWithNegativeValue() { - expectedException.expect(IllegalArgumentException.class); - Limit.limit(-1); - } - - @Test - public void ofShouldTakePositiveValueAsLimit() { - assertThat(Limit.from(3)) - .isEqualTo(Limit.limit(3)); - } - - @Test - public void ofShouldTakeNegativeValueAsUnlimited() { - assertThat(Limit.from(-1)) - .isEqualTo(Limit.unlimited()); - } - - @Test - public void ofShouldTakeZeroValueAsUnlimited() { - assertThat(Limit.from(0)) - .isEqualTo(Limit.unlimited()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/java/org/apache/james/util/streams/OffsetTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/OffsetTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/OffsetTest.java deleted file mode 100644 index 45d8e6d..0000000 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/OffsetTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.util.streams; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Optional; - -import org.junit.Test; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public class OffsetTest { - - public static final int VALUE = 18; - - @Test - public void shouldMatchBeanContract() { - EqualsVerifier.forClass(Offset.class) - .verify(); - } - - @Test - public void fromZeroShouldBeEquivalentToNone() { - assertThat(Offset.from(0)) - .isEqualTo(Offset.none()); - } - - @Test - public void getOffsetShouldReturnContainedValue() { - assertThat(Offset.from(VALUE).getOffset()) - .isEqualTo(VALUE); - } - - @Test - public void fromOptionalShouldBeEquivalentToFromValueWhenPresent() { - assertThat(Offset.from(Optional.of(VALUE))) - .isEqualTo(Offset.from(VALUE)); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util-java8/src/test/resources/testcontainers.properties ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/resources/testcontainers.properties b/server/container/util-java8/src/test/resources/testcontainers.properties deleted file mode 100644 index ec999c5..0000000 --- a/server/container/util-java8/src/test/resources/testcontainers.properties +++ /dev/null @@ -1,4 +0,0 @@ -# Checks are disable due to space parsing error -# We should remove this file when upgrading to upcoming 1.4.0 version of testscontainer -# See -checks.disable=true \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/util/pom.xml b/server/container/util/pom.xml index a7af883..d10a795 100644 --- a/server/container/util/pom.xml +++ b/server/container/util/pom.xml @@ -34,6 +34,23 @@ <dependencies> <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>apache-mime4j-dom</artifactId> + </dependency> + <dependency> + <groupId>com.github.dpaukov</groupId> + <artifactId>combinatoricslib3</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.github.fge</groupId> + <artifactId>throwing-lambdas</artifactId> + </dependency> + <dependency> + <groupId>com.github.steveash.guavate</groupId> + <artifactId>guavate</artifactId> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> @@ -46,6 +63,15 @@ <artifactId>commons-io</artifactId> </dependency> <dependency> + <groupId>javax.inject</groupId> + <artifactId>javax.inject</artifactId> + </dependency> + <dependency> + <groupId>nl.jqno.equalsverifier</groupId> + <artifactId>equalsverifier</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util/src/main/java/org/apache/james/util/ClassLoaderUtils.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/main/java/org/apache/james/util/ClassLoaderUtils.java b/server/container/util/src/main/java/org/apache/james/util/ClassLoaderUtils.java new file mode 100644 index 0000000..68d9b08 --- /dev/null +++ b/server/container/util/src/main/java/org/apache/james/util/ClassLoaderUtils.java @@ -0,0 +1,54 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import javax.mail.util.SharedByteArrayInputStream; + +import org.apache.commons.io.IOUtils; + +public class ClassLoaderUtils { + public static String getSystemResourceAsString(String filename, Charset charset) { + try { + return IOUtils.toString(ClassLoader.getSystemResourceAsStream(filename), charset); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static String getSystemResourceAsString(String filename) { + return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); + } + + public static byte[] getSystemResourceAsByteArray(String filename) { + try { + return IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream(filename)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static SharedByteArrayInputStream getSystemResourceAsSharedStream(String filename) { + return new SharedByteArrayInputStream(getSystemResourceAsByteArray(filename)); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/630dcab1/server/container/util/src/main/java/org/apache/james/util/CompletableFutureUtil.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/main/java/org/apache/james/util/CompletableFutureUtil.java b/server/container/util/src/main/java/org/apache/james/util/CompletableFutureUtil.java new file mode 100644 index 0000000..d2e83af --- /dev/null +++ b/server/container/util/src/main/java/org/apache/james/util/CompletableFutureUtil.java @@ -0,0 +1,124 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.function.BiFunction; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Stream; + +public class CompletableFutureUtil { + + public static <T> CompletableFuture<Optional<T>> unwrap(CompletableFuture<Optional<CompletableFuture<T>>> base) { + return base.thenCompose( + optional -> optional.map(future -> future.thenApply(Optional::of)) + .orElse(CompletableFuture.completedFuture(Optional.empty()))); + } + + @SafeVarargs + public static <T> CompletableFuture<Stream<T>> allOfArray(CompletableFuture<T>... futures) { + return allOf(Stream.of(futures)); + } + + public static <T, U, V> CompletableFuture<V> combine(CompletableFuture<T> t, CompletableFuture<U> u, BiFunction<T,U,V> combiner) { + return t.thenCompose(valueT -> + u.thenApply(valueU -> combiner.apply(valueT, valueU))); + } + + public static <T> CompletableFuture<Stream<T>> allOf(Stream<CompletableFuture<T>> futureStream) { + return futureStream + .map((CompletableFuture<T> future) -> future.thenApply(Stream::of)) + .parallel() + .reduce((future1, future2) -> + future1.thenCompose( + stream1 -> future2.thenCompose( + stream2 -> { + Stream<T> concatStream = Stream.concat(stream1, stream2); + return CompletableFuture.completedFuture(concatStream); + }))) + .orElse(CompletableFuture.completedFuture(Stream.of())); + } + + public static <R, T> CompletableFuture<Stream<R>> chainAll(Stream<T> futureStream, + Function<T, CompletableFuture<R>> transformationToChain) { + return futureStream + .map(t -> (Supplier<CompletableFuture<R>>) (() -> transformationToChain.apply(t))) + .reduce(CompletableFuture.<Stream<R>>completedFuture(Stream.of()), + (accumulator, supplier) -> + accumulator.thenCompose( + accumulatedStream -> + supplier.get() + .thenCompose(r -> + CompletableFuture.completedFuture(Stream.<R>concat(accumulatedStream, Stream.of(r)))) + ), + getCompletableFutureBinaryOperator()); + } + + private static <R> BinaryOperator<CompletableFuture<Stream<R>>> getCompletableFutureBinaryOperator() { + return (future1, future2) -> + future1.thenCompose(stream1 -> + future2.<Stream<R>>thenCompose(stream2 -> + CompletableFuture.completedFuture(Stream.concat(stream1, stream2)))); + } + + public static <T> CompletableFuture<Stream<T>> performOnAll(CompletableFuture<Stream<T>> futurStream, Function<T, CompletableFuture<Void>> action) { + return thenComposeOnAll(futurStream, value -> + keepValue(() -> + action.apply(value), + value)); + } + + public static <T, U> CompletableFuture<Stream<U>> thenComposeOnAll(CompletableFuture<Stream<T>> futurStream, Function<T, CompletableFuture<U>> action) { + return futurStream + .thenCompose(stream -> + CompletableFutureUtil.allOf( + stream.map(action))); + } + + public static <T, U> CompletableFuture<Stream<U>> map(CompletableFuture<Stream<T>> futurStream, Function<T, U> action) { + return futurStream + .thenApply(stream -> + stream.map(action)); + } + + public static <T> CompletableFuture<Optional<T>> reduce(BinaryOperator<T> binaryOperator, CompletableFuture<Stream<T>> futureStream) { + return futureStream.thenApply(stream -> stream.reduce(binaryOperator)); + } + + public static <T> CompletableFuture<T> reduce(BinaryOperator<T> binaryOperator, CompletableFuture<Stream<T>> futureStream, T emptyAccumulator) { + return futureStream.thenApply(stream -> stream.reduce(binaryOperator).orElse(emptyAccumulator)); + } + + public static <T> CompletableFuture<T> keepValue(Supplier<CompletableFuture<Void>> supplier, T value) { + return supplier.get().thenApply(any -> value); + } + + public static <T> Function<Boolean, CompletableFuture<Boolean>> composeIfTrue(Supplier<CompletableFuture<T>> composeOperation) { + return b -> { + if (b) { + return composeOperation.get().thenApply(any -> b); + } + return CompletableFuture.completedFuture(b); + }; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org