Bläddra i källkod

Merge pull request #658 from csmith/times

Switch to new date APIs.
pull/659/head
Greg Holmes 8 år sedan
förälder
incheckning
5be8a9d730

+ 2
- 2
src/com/dmdirc/ChannelEventHandler.java Visa fil

@@ -60,7 +60,7 @@ import com.dmdirc.parser.interfaces.Parser;
60 60
 import com.google.common.base.Strings;
61 61
 
62 62
 import java.time.LocalDateTime;
63
-import java.util.Date;
63
+import java.time.ZoneOffset;
64 64
 import java.util.Optional;
65 65
 import java.util.stream.Collectors;
66 66
 
@@ -139,7 +139,7 @@ public class ChannelEventHandler extends EventHandler {
139 139
 
140 140
         final Topic topic = Topic.create(channel.getTopic(),
141 141
                 owner.getUser(getConnection().getUser(channel.getTopicSetter())).orElse(null),
142
-                new Date(1000 * channel.getTopicTime()));
142
+                LocalDateTime.ofEpochSecond(channel.getTopicTime(), 0, ZoneOffset.UTC));
143 143
 
144 144
         if (event.isJoinTopic()) {
145 145
             if (Strings.isNullOrEmpty(channel.getTopic())) {

+ 3
- 3
src/com/dmdirc/Topic.java Visa fil

@@ -26,7 +26,7 @@ import com.dmdirc.interfaces.GroupChatUser;
26 26
 
27 27
 import com.google.auto.value.AutoValue;
28 28
 
29
-import java.util.Date;
29
+import java.time.LocalDateTime;
30 30
 import java.util.Optional;
31 31
 
32 32
 /**
@@ -42,11 +42,11 @@ public abstract class Topic {
42 42
     /** Topic client. */
43 43
     public abstract Optional<GroupChatUser> getClient();
44 44
     /** Topic time. */
45
-    public abstract Date getDate();
45
+    public abstract LocalDateTime getDate();
46 46
 
47 47
     public static Topic create(final String topic,
48 48
             final GroupChatUser groupChatUser,
49
-            final Date date) {
49
+            final LocalDateTime date) {
50 50
         return new AutoValue_Topic(topic, Optional.ofNullable(groupChatUser), date);
51 51
     }
52 52
 }

+ 6
- 6
src/com/dmdirc/logger/ProgramError.java Visa fil

@@ -29,7 +29,7 @@ import com.google.common.base.MoreObjects;
29 29
 import com.google.common.base.Throwables;
30 30
 
31 31
 import java.io.Serializable;
32
-import java.util.Date;
32
+import java.time.LocalDateTime;
33 33
 import java.util.Objects;
34 34
 import java.util.Optional;
35 35
 
@@ -52,7 +52,7 @@ public class ProgramError implements Serializable {
52 52
     /** Underlying exception. */
53 53
     private final Optional<Throwable> exception;
54 54
     /** Date/time error first occurred. */
55
-    private final Date date;
55
+    private final LocalDateTime date;
56 56
     /** The eventbus to post status changes to. */
57 57
     private final Optional<DMDircMBassador> eventBus;
58 58
     /** Is this an application error? */
@@ -75,7 +75,7 @@ public class ProgramError implements Serializable {
75 75
             @Nonnull final ErrorLevel level,
76 76
             @Nonnull final String message,
77 77
             @Nullable final Throwable exception,
78
-            @Nonnull final Date date,
78
+            @Nonnull final LocalDateTime date,
79 79
             @Nullable final DMDircMBassador eventBus,
80 80
             final boolean appError) {
81 81
         checkNotNull(level);
@@ -86,7 +86,7 @@ public class ProgramError implements Serializable {
86 86
         this.level = level;
87 87
         this.message = message;
88 88
         this.exception = Optional.ofNullable(exception);
89
-        this.date = (Date) date.clone();
89
+        this.date = date;
90 90
         this.reportStatus = ErrorReportStatus.WAITING;
91 91
         this.eventBus = Optional.ofNullable(eventBus);
92 92
         this.appError = appError;
@@ -123,8 +123,8 @@ public class ProgramError implements Serializable {
123 123
      *
124 124
      * @return Error time
125 125
      */
126
-    public Date getDate() {
127
-        return (Date) date.clone();
126
+    public LocalDateTime getDate() {
127
+        return date;
128 128
     }
129 129
 
130 130
     /**

+ 2
- 2
src/com/dmdirc/logger/ProgramErrorFactory.java Visa fil

@@ -24,7 +24,7 @@ package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 
27
-import java.util.Date;
27
+import java.time.LocalDateTime;
28 28
 
29 29
 import javax.annotation.Nullable;
30 30
 import javax.inject.Inject;
@@ -43,7 +43,7 @@ public class ProgramErrorFactory {
43 43
 
44 44
     public ProgramError create(final ErrorLevel level, final String message,
45 45
             @Nullable final Throwable exception,
46
-            final Date date,
46
+            final LocalDateTime date,
47 47
             final boolean appError) {
48 48
         return new ProgramError(level, message, exception, date, eventBus, appError);
49 49
     }

+ 2
- 2
src/com/dmdirc/logger/ProgramErrorManager.java Visa fil

@@ -35,11 +35,11 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
35 35
 
36 36
 import com.google.common.base.Throwables;
37 37
 
38
+import java.time.LocalDateTime;
38 39
 import java.util.ArrayList;
39 40
 import java.util.Arrays;
40 41
 import java.util.Collection;
41 42
 import java.util.Collections;
42
-import java.util.Date;
43 43
 import java.util.HashSet;
44 44
 import java.util.Set;
45 45
 import java.util.concurrent.CopyOnWriteArraySet;
@@ -124,7 +124,7 @@ public class ProgramErrorManager {
124 124
             final Throwable throwable, final boolean appError) {
125 125
 
126 126
         final ProgramError error = programErrorFactory.create(level, message, throwable,
127
-                new Date(), appError);
127
+                LocalDateTime.now(), appError);
128 128
         errors.add(error);
129 129
         return error;
130 130
     }

+ 4
- 2
src/com/dmdirc/logger/SentryErrorReporter.java Visa fil

@@ -25,6 +25,8 @@ package com.dmdirc.logger;
25 25
 import com.dmdirc.interfaces.Connection;
26 26
 import com.dmdirc.util.ClientInfo;
27 27
 
28
+import java.time.LocalDateTime;
29
+import java.time.ZoneOffset;
28 30
 import java.util.Date;
29 31
 import java.util.Optional;
30 32
 
@@ -68,12 +70,12 @@ public class SentryErrorReporter {
68 70
     public void sendException(
69 71
             final String message,
70 72
             final ErrorLevel level,
71
-            final Date timestamp,
73
+            final LocalDateTime timestamp,
72 74
             final Optional<Throwable> exception) {
73 75
         final EventBuilder eventBuilder = newEventBuilder()
74 76
                 .withMessage(message)
75 77
                 .withLevel(getSentryLevel(level))
76
-                .withTimestamp(timestamp);
78
+                .withTimestamp(Date.from(timestamp.toInstant(ZoneOffset.UTC)));
77 79
 
78 80
         exception.ifPresent(e -> eventBuilder.withSentryInterface(new ExceptionInterface(e)));
79 81
 

+ 5
- 5
src/com/dmdirc/ui/core/errors/DisplayableError.java Visa fil

@@ -28,7 +28,7 @@ import com.dmdirc.logger.ProgramError;
28 28
 
29 29
 import com.google.common.base.MoreObjects;
30 30
 
31
-import java.util.Date;
31
+import java.time.LocalDateTime;
32 32
 import java.util.Objects;
33 33
 
34 34
 /**
@@ -36,14 +36,14 @@ import java.util.Objects;
36 36
  */
37 37
 public class DisplayableError {
38 38
 
39
-    private final Date date;
39
+    private final LocalDateTime date;
40 40
     private final String summary;
41 41
     private final String details;
42 42
     private final ErrorLevel severity;
43 43
     private final ProgramError programError;
44 44
     private ErrorReportStatus reportStatus;
45 45
 
46
-    public DisplayableError(final Date date, final String summary, final String details,
46
+    public DisplayableError(final LocalDateTime date, final String summary, final String details,
47 47
             final ErrorLevel severity, final ErrorReportStatus reportStatus,
48 48
             final ProgramError programError) {
49 49
         this.date = date;
@@ -54,8 +54,8 @@ public class DisplayableError {
54 54
         this.reportStatus = reportStatus;
55 55
     }
56 56
 
57
-    public Date getDate() {
58
-        return (Date) date.clone();
57
+    public LocalDateTime getDate() {
58
+        return date;
59 59
     }
60 60
 
61 61
     public String getSummary() {

+ 6
- 6
test/com/dmdirc/TopicTest.java Visa fil

@@ -24,7 +24,8 @@ package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.interfaces.GroupChatUser;
26 26
 
27
-import java.util.Date;
27
+import java.time.LocalDateTime;
28
+import java.time.ZoneOffset;
28 29
 import java.util.Optional;
29 30
 
30 31
 import org.junit.Test;
@@ -37,25 +38,24 @@ import static org.junit.Assert.assertEquals;
37 38
 @RunWith(MockitoJUnitRunner.class)
38 39
 public class TopicTest {
39 40
 
40
-    @Mock
41
-    private GroupChatUser user;
41
+    @Mock private GroupChatUser user;
42 42
 
43 43
     @Test
44 44
     public void testGetClient() {
45
-        final Topic test = Topic.create("abc", user, new Date());
45
+        final Topic test = Topic.create("abc", user, LocalDateTime.now());
46 46
         assertEquals(Optional.of(user), test.getClient());
47 47
     }
48 48
 
49 49
     @Test
50 50
     public void testGetTime() {
51
-        final Date date = new Date(123394432);
51
+        final LocalDateTime date = LocalDateTime.ofEpochSecond(123394432L, 0, ZoneOffset.UTC);
52 52
         final Topic test = Topic.create("abc", user, date);
53 53
         assertEquals(date, test.getDate());
54 54
     }
55 55
 
56 56
     @Test
57 57
     public void testGetTopic() {
58
-        final Topic test = Topic.create("abc", user, new Date());
58
+        final Topic test = Topic.create("abc", user, LocalDateTime.now());
59 59
         assertEquals("abc", test.getTopic());
60 60
     }
61 61
 

+ 14
- 14
test/com/dmdirc/logger/ProgramErrorTest.java Visa fil

@@ -26,7 +26,7 @@ import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.events.ProgramErrorStatusEvent;
27 27
 import com.dmdirc.util.ClientInfo;
28 28
 
29
-import java.util.Date;
29
+import java.time.LocalDateTime;
30 30
 
31 31
 import org.junit.Test;
32 32
 import org.junit.runner.RunWith;
@@ -51,7 +51,7 @@ public class ProgramErrorTest {
51 51
 
52 52
     @Test(expected = NullPointerException.class)
53 53
     public void testConstructorNullErrorLevel() {
54
-        new ProgramError(null, "moo", null, new Date(),eventBus, true);
54
+        new ProgramError(null, "moo", null, LocalDateTime.now(), eventBus, true);
55 55
     }
56 56
 
57 57
     @Test(expected = NullPointerException.class)
@@ -62,26 +62,26 @@ public class ProgramErrorTest {
62 62
     @Test
63 63
     public void testConstructorGood() {
64 64
         new ProgramError(ErrorLevel.HIGH, "moo", new UnsupportedOperationException(),
65
-                new Date(), eventBus, true);
65
+                LocalDateTime.now(), eventBus, true);
66 66
     }
67 67
 
68 68
     @Test
69 69
     public void testGetLevel() {
70 70
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
71
-                new UnsupportedOperationException(), new Date(), eventBus, true);
71
+                new UnsupportedOperationException(), LocalDateTime.now(), eventBus, true);
72 72
         assertEquals(ErrorLevel.HIGH, pe.getLevel());
73 73
     }
74 74
 
75 75
     @Test
76 76
     public void testGetMessage() {
77 77
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
78
-                new UnsupportedOperationException(), new Date(), eventBus, true);
78
+                new UnsupportedOperationException(), LocalDateTime.now(), eventBus, true);
79 79
         assertEquals("moo", pe.getMessage());
80 80
     }
81 81
 
82 82
     @Test
83 83
     public void testGetDate() {
84
-        final Date date = new Date();
84
+        final LocalDateTime date = LocalDateTime.now();
85 85
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
86 86
                 new UnsupportedOperationException(), date, eventBus, true);
87 87
         assertEquals(date, pe.getDate());
@@ -90,14 +90,14 @@ public class ProgramErrorTest {
90 90
     @Test
91 91
     public void testIsAppError() {
92 92
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
93
-                new UnsupportedOperationException(), new Date(), eventBus, true);
93
+                new UnsupportedOperationException(), LocalDateTime.now(), eventBus, true);
94 94
         assertTrue(pe.isAppError());
95 95
     }
96 96
 
97 97
     @Test
98 98
     public void testReportStatus() {
99 99
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
100
-                new UnsupportedOperationException(), new Date(), eventBus, true);
100
+                new UnsupportedOperationException(), LocalDateTime.now(), eventBus, true);
101 101
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
102 102
         pe.setReportStatus(null);
103 103
         assertEquals(ErrorReportStatus.WAITING, pe.getReportStatus());
@@ -114,7 +114,7 @@ public class ProgramErrorTest {
114 114
     @Test
115 115
     public void testToString() {
116 116
         final ProgramError pe = new ProgramError(ErrorLevel.HIGH, "moo",
117
-                new UnsupportedOperationException(), new Date(), eventBus, true);
117
+                new UnsupportedOperationException(), LocalDateTime.now(), eventBus, true);
118 118
         assertTrue(pe.toString().contains("moo"));
119 119
     }
120 120
 
@@ -122,15 +122,15 @@ public class ProgramErrorTest {
122 122
     public void testEquals() {
123 123
         final Exception ex = new UnsupportedOperationException();
124 124
         final ProgramError pe1 = new ProgramError(ErrorLevel.LOW, "moo",
125
-                ex, new Date(), eventBus, true);
125
+                ex, LocalDateTime.now(), eventBus, true);
126 126
         final ProgramError pe2 = new ProgramError(ErrorLevel.LOW, "moo",
127
-                ex, new Date(), eventBus, true);
127
+                ex, LocalDateTime.now(), eventBus, true);
128 128
         final ProgramError pe3 = new ProgramError(ErrorLevel.MEDIUM, "moo",
129
-                ex, new Date(), eventBus, true);
129
+                ex, LocalDateTime.now(), eventBus, true);
130 130
         final ProgramError pe4 = new ProgramError(ErrorLevel.LOW, "bar",
131
-                ex, new Date(), eventBus, true);
131
+                ex, LocalDateTime.now(), eventBus, true);
132 132
         final ProgramError pe5 = new ProgramError(ErrorLevel.LOW, "moo",
133
-                null, new Date(), eventBus, true);
133
+                null, LocalDateTime.now(), eventBus, true);
134 134
 
135 135
         assertFalse(pe1.equals(null)); // NOPMD
136 136
         assertFalse(pe1.equals("moo"));

+ 17
- 10
test/com/dmdirc/logger/SentryErrorReporterTest.java Visa fil

@@ -24,7 +24,8 @@ package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.util.ClientInfo;
26 26
 
27
-import java.util.Date;
27
+import java.time.LocalDateTime;
28
+import java.time.ZoneOffset;
28 29
 import java.util.Optional;
29 30
 
30 31
 import org.junit.Before;
@@ -77,50 +78,56 @@ public class SentryErrorReporterTest {
77 78
 
78 79
     @Test
79 80
     public void testSendsMessage() {
80
-        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), throwable);
81
+        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
82
+                throwable);
81 83
         verify(raven).sendEvent(eventCaptor.capture());
82 84
         assertEquals("message 123", eventCaptor.getValue().getMessage());
83 85
     }
84 86
 
85 87
     @Test
86 88
     public void testSendsTimestamp() {
87
-        final Date date = new Date(561859200000l);
89
+        final LocalDateTime date = LocalDateTime.ofEpochSecond(56185920L, 0, ZoneOffset.UTC);
88 90
         sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, throwable);
89 91
         verify(raven).sendEvent(eventCaptor.capture());
90
-        assertEquals(date, eventCaptor.getValue().getTimestamp());
92
+        assertEquals(56185920000L, eventCaptor.getValue().getTimestamp().getTime());
91 93
     }
92 94
 
93 95
     @Test
94 96
     public void testSendsLowLevelAsInfo() {
95
-        sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), throwable);
97
+        sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, LocalDateTime.now(),
98
+                throwable);
96 99
         verify(raven).sendEvent(eventCaptor.capture());
97 100
         assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
98 101
     }
99 102
 
100 103
     @Test
101 104
     public void testSendsMediumLevelAsWarning() {
102
-        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), throwable);
105
+        sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
106
+                throwable);
103 107
         verify(raven).sendEvent(eventCaptor.capture());
104 108
         assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
105 109
     }
106 110
 
107 111
     @Test
108 112
     public void testSendsHighLevelAsError() {
109
-        sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), throwable);
113
+        sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, LocalDateTime.now(),
114
+                throwable);
110 115
         verify(raven).sendEvent(eventCaptor.capture());
111 116
         assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
112 117
     }
113 118
 
114 119
     @Test
115 120
     public void testSendsFatalLevelAsFatal() {
116
-        sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), throwable);
121
+        sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, LocalDateTime.now(),
122
+                throwable);
117 123
         verify(raven).sendEvent(eventCaptor.capture());
118 124
         assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
119 125
     }
120 126
 
121 127
     @Test
122 128
     public void testSendsUnknownLevelAsInfo() {
123
-        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), throwable);
129
+        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
130
+                throwable);
124 131
         verify(raven).sendEvent(eventCaptor.capture());
125 132
         assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
126 133
     }
@@ -128,7 +135,7 @@ public class SentryErrorReporterTest {
128 135
     @Test
129 136
     public void testAddsExceptionInterface() {
130 137
         final Exception exception = new IndexOutOfBoundsException("Message blah");
131
-        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(),
138
+        sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
132 139
                 Optional.of(exception));
133 140
         verify(raven).sendEvent(eventCaptor.capture());
134 141
         final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()

+ 8
- 9
test/com/dmdirc/logger/SentryLoggingErrorManagerTest.java Visa fil

@@ -29,13 +29,12 @@ import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 
30 30
 import com.google.common.util.concurrent.MoreExecutors;
31 31
 
32
-import java.util.Date;
32
+import java.time.LocalDateTime;
33 33
 import java.util.Optional;
34 34
 
35 35
 import org.junit.Before;
36 36
 import org.junit.Test;
37 37
 import org.junit.runner.RunWith;
38
-import org.mockito.Matchers;
39 38
 import org.mockito.Mock;
40 39
 import org.mockito.runners.MockitoJUnitRunner;
41 40
 
@@ -79,14 +78,14 @@ public class SentryLoggingErrorManagerTest {
79 78
     public void testHandleErrorEvent() throws Exception {
80 79
         instance.handleErrorEvent(appErrorEvent);
81 80
         verify(sentryErrorReporter).sendException(anyString(), any(ErrorLevel.class),
82
-                any(Date.class), Matchers.<Optional<Throwable>>any());
81
+                any(LocalDateTime.class), any());
83 82
     }
84 83
 
85 84
     @Test
86 85
     public void testHandledErrorEvent_UserError() throws Exception {
87 86
         instance.handleErrorEvent(userErrorEvent);
88 87
         verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
89
-                any(Date.class), Matchers.<Optional<Throwable>>any());
88
+                any(LocalDateTime.class), any());
90 89
     }
91 90
 
92 91
     @Test
@@ -95,7 +94,7 @@ public class SentryLoggingErrorManagerTest {
95 94
         when(appError.getThrowable()).thenReturn(Optional.of(throwable));
96 95
         instance.handleErrorEvent(appErrorEvent);
97 96
         verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
98
-                any(Date.class), eq(Optional.of(throwable)));
97
+                any(LocalDateTime.class), eq(Optional.of(throwable)));
99 98
     }
100 99
 
101 100
     @Test
@@ -104,7 +103,7 @@ public class SentryLoggingErrorManagerTest {
104 103
         instance.handleNoErrorReporting(false);
105 104
         instance.handleErrorEvent(appErrorEvent);
106 105
         verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
107
-                any(Date.class), Matchers.<Optional<Throwable>>any());
106
+                any(LocalDateTime.class), any());
108 107
     }
109 108
 
110 109
     @Test
@@ -113,7 +112,7 @@ public class SentryLoggingErrorManagerTest {
113 112
         instance.handleNoErrorReporting(true);
114 113
         instance.handleErrorEvent(appErrorEvent);
115 114
         verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
116
-                any(Date.class), Matchers.<Optional<Throwable>>any());
115
+                any(LocalDateTime.class), any());
117 116
     }
118 117
 
119 118
     @Test
@@ -122,7 +121,7 @@ public class SentryLoggingErrorManagerTest {
122 121
         instance.handleNoErrorReporting(false);
123 122
         instance.handleErrorEvent(appErrorEvent);
124 123
         verify(sentryErrorReporter).sendException(anyString(), any(ErrorLevel.class),
125
-                any(Date.class), Matchers.<Optional<Throwable>>any());
124
+                any(LocalDateTime.class), any());
126 125
     }
127 126
 
128 127
     @Test
@@ -131,6 +130,6 @@ public class SentryLoggingErrorManagerTest {
131 130
         instance.handleNoErrorReporting(true);
132 131
         instance.handleErrorEvent(appErrorEvent);
133 132
         verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
134
-                any(Date.class), Matchers.<Optional<Throwable>>any());
133
+                any(LocalDateTime.class), any());
135 134
     }
136 135
 }

Laddar…
Avbryt
Spara