You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2020/02/13 02:29:27 UTC
[james-project] 16/21: [Refactoring] Tests for
StoreCommandParser::parseDateTime
This is an automated email from the ASF dual-hosted git repository.
rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 30d00acb57661db5a3757ce414cab7bde0ddc727
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Dec 19 15:07:40 2019 +0700
[Refactoring] Tests for StoreCommandParser::parseDateTime
---
.../imap/decode/parser/AppendCommandParser.java | 13 +++-
.../imap/decode/parser/ImapParserFactory.java | 3 +-
.../decode/parser/AppendCommandParserTest.java | 87 ++++++++++++++++++++++
3 files changed, 99 insertions(+), 4 deletions(-)
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
index 239fa80..7f4b520 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
@@ -18,6 +18,7 @@
****************************************************************/
package org.apache.james.imap.decode.parser;
+import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
@@ -34,12 +35,17 @@ import org.apache.james.imap.decode.ImapRequestLineReader;
import org.apache.james.imap.decode.base.AbstractImapCommandParser;
import org.apache.james.imap.message.request.AppendRequest;
+import com.google.common.annotations.VisibleForTesting;
+
/**
* Parses APPEND command
*/
public class AppendCommandParser extends AbstractImapCommandParser {
- public AppendCommandParser(StatusResponseFactory statusResponseFactory) {
+ private final Clock clock;
+
+ public AppendCommandParser(StatusResponseFactory statusResponseFactory, Clock clock) {
super(ImapConstants.APPEND_COMMAND, statusResponseFactory);
+ this.clock = clock;
}
/**
@@ -59,12 +65,13 @@ public class AppendCommandParser extends AbstractImapCommandParser {
* If the next character in the request is a '"', tries to read a DateTime
* argument. If not, returns now.
*/
- private LocalDateTime parseDateTime(ImapRequestLineReader request) throws DecodingException {
+ @VisibleForTesting
+ LocalDateTime parseDateTime(ImapRequestLineReader request) throws DecodingException {
char next = request.nextWordChar();
if (next == '"') {
return request.dateTime();
}
- return LocalDateTime.now();
+ return LocalDateTime.now(clock);
}
@Override
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
index 23e6a75..e65ae8a 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
@@ -19,6 +19,7 @@
package org.apache.james.imap.decode.parser;
+import java.time.Clock;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
@@ -65,7 +66,7 @@ public class ImapParserFactory implements ImapCommandParserFactory {
new XListCommandParser(statusResponseFactory),
new LsubCommandParser(statusResponseFactory),
new StatusCommandParser(statusResponseFactory),
- new AppendCommandParser(statusResponseFactory),
+ new AppendCommandParser(statusResponseFactory, Clock.systemDefaultZone()),
// RFC2342 NAMESPACE
new NamespaceCommandParser(statusResponseFactory),
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/AppendCommandParserTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/AppendCommandParserTest.java
new file mode 100644
index 0000000..ae8cb2f
--- /dev/null
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/AppendCommandParserTest.java
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.imap.decode.parser;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneOffset;
+
+import org.apache.james.imap.api.message.response.StatusResponseFactory;
+import org.apache.james.imap.decode.ImapRequestStreamLineReader;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class AppendCommandParserTest {
+ private static final Instant DATE = Instant.parse("2007-07-03T10:15:30.00Z");
+ private static final Clock CLOCK = Clock.fixed(DATE, ZoneOffset.UTC);
+
+ private AppendCommandParser testee;
+
+ @BeforeEach
+ void setUp() {
+ testee = new AppendCommandParser(mock(StatusResponseFactory.class), CLOCK);
+ }
+
+ @Test
+ void parseDateTimeShouldReturnNowWhenNotADate() throws Exception {
+ ImapRequestStreamLineReader request = toRequest("any\n");
+
+ assertThat(testee.parseDateTime(request).toInstant(ZoneOffset.UTC)).isEqualTo(DATE);
+ }
+
+ @Test
+ void parseDateTimeShouldNotConsumeNonDateLiteral() throws Exception {
+ ImapRequestStreamLineReader request = toRequest("any\n");
+
+ testee.parseDateTime(request);
+
+ assertThat(request.atom()).isEqualTo("any");
+ }
+
+ @Test
+ void parseDateTimeShouldConsumeDateLiteral() throws Exception {
+ ImapRequestStreamLineReader request = toRequest("\"09-Apr-2008 15:17:51 +0200\" any\n");
+
+ testee.parseDateTime(request);
+
+ assertThat(request.atom()).isEqualTo("any");
+ }
+
+ @Test
+ void parseDateTimeShouldReturnSuppliedValue() throws Exception {
+ ImapRequestStreamLineReader request = toRequest("\"09-Apr-2008 15:17:51 +0000\"");
+
+ assertThat(
+ testee.parseDateTime(request)
+ .atZone(ZoneOffset.systemDefault())
+ .withZoneSameInstant(ZoneOffset.UTC))
+ .isEqualTo("2008-04-09T15:17:51Z");
+ }
+
+ private ImapRequestStreamLineReader toRequest(String input) {
+ return new ImapRequestStreamLineReader(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), new ByteArrayOutputStream());
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org