Skip to content

Commit d5116e4

Browse files
committed
fixed instances of Date that should be LocalDate
1 parent 13aa1e9 commit d5116e4

2 files changed

Lines changed: 32 additions & 34 deletions

File tree

src/main/java/org/dspace/pack/bagit/BagItPolicyUtil.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010
import java.io.IOException;
1111
import java.sql.SQLException;
12-
import java.text.DateFormat;
13-
import java.text.ParseException;
14-
import java.text.SimpleDateFormat;
12+
import java.time.LocalDate;
13+
import java.time.format.DateTimeFormatter;
14+
import java.time.format.DateTimeParseException;
1515
import java.util.ArrayList;
16-
import java.util.Date;
1716
import java.util.List;
1817

1918
import com.google.common.collect.BiMap;
@@ -63,7 +62,6 @@ private BagItPolicyUtil() {}
6362
public static Policies getPolicy(final Context context, final DSpaceObject dso) throws IOException {
6463
final Policies policies = new Policies();
6564
final BiMap<Integer, String> actions = actionMapper().inverse();
66-
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
6765

6866
for (ResourcePolicy resourcePolicy : dso.getResourcePolicies()) {
6967
final Policy policy = new Policy();
@@ -72,13 +70,13 @@ public static Policies getPolicy(final Context context, final DSpaceObject dso)
7270
policy.setName(resourcePolicy.getRpName());
7371
policy.setDescription(resourcePolicy.getRpDescription());
7472

75-
final Date endDate = resourcePolicy.getEndDate();
76-
final Date startDate = resourcePolicy.getStartDate();
73+
final LocalDate endDate = resourcePolicy.getEndDate();
74+
final LocalDate startDate = resourcePolicy.getStartDate();
7775
if (startDate != null) {
78-
policy.setStartDate(dateFormat.format(startDate));
76+
policy.setStartDate(startDate.toString());
7977
}
8078
if (endDate != null) {
81-
policy.setEndDate(dateFormat.format(endDate));
79+
policy.setEndDate(endDate.toString());
8280
}
8381

8482
// attributes for determining if adding policies on a group + what type of group or policies for a user
@@ -128,7 +126,8 @@ public static Policies getPolicy(final Context context, final DSpaceObject dso)
128126
*/
129127
public static void registerPolicies(final Context context, final DSpaceObject dSpaceObject, final Policies policies)
130128
throws SQLException, AuthorizeException, PackageException {
131-
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
129+
final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
130+
132131
final GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
133132
final EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
134133
final AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService();
@@ -193,19 +192,19 @@ public static void registerPolicies(final Context context, final DSpaceObject dS
193192
final String rpStartDate = policy.getStartDate();
194193
if (rpStartDate != null) {
195194
try {
196-
final Date date = dateFormat.parse(rpStartDate);
195+
final LocalDate date = LocalDate.parse(rpStartDate, dateFormat);
197196
resourcePolicy.setStartDate(date);
198-
} catch (ParseException ignored) {
197+
} catch (DateTimeParseException ignored) {
199198
logger.warn("Failed to parse rp-start-date. The date needs to be in the format 'yyyy-MM-dd'.");
200199
}
201200
}
202201

203202
final String rpEndDate = policy.getEndDate();
204203
if (rpEndDate != null) {
205204
try {
206-
final Date date = dateFormat.parse(rpEndDate);
205+
final LocalDate date = LocalDate.parse(rpEndDate, dateFormat);
207206
resourcePolicy.setEndDate(date);
208-
} catch (ParseException ignored) {
207+
} catch (DateTimeParseException ignored) {
209208
logger.warn("Failed to parse rp-end-date. The date needs to be in the format 'yyyy-MM-dd'.");
210209
}
211210
}

src/test/java/org/dspace/pack/bagit/BagItPolicyUtilTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
import java.nio.file.Path;
2323
import java.nio.file.Paths;
2424
import java.sql.SQLException;
25-
import java.text.DateFormat;
26-
import java.text.SimpleDateFormat;
2725
import java.time.Instant;
26+
import java.time.LocalDate;
27+
import java.time.ZoneId;
2828
import java.time.temporal.ChronoUnit;
29-
import java.util.Date;
3029
import java.util.List;
3130

3231
import org.dspace.authorize.ResourcePolicy;
@@ -73,14 +72,14 @@ public void setup() throws SQLException {
7372

7473
@Test
7574
public void getPolicyForAdmin() throws Exception {
76-
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
77-
78-
// Setup group
75+
// Set up the Group
7976
final Group adminGroup = mock(Group.class);
8077
when(adminGroup.getName()).thenReturn(Group.ADMIN);
8178

82-
// set up an admin ResourcePolicy for the Community
83-
final Date groupDate = Date.from(Instant.now().minus(1, ChronoUnit.DAYS));
79+
// Set up an admin ResourcePolicy for the Community
80+
final LocalDate groupDate = Instant.now().minus(1, ChronoUnit.DAYS)
81+
.atZone(ZoneId.systemDefault())
82+
.toLocalDate();
8483
final ResourcePolicy adminGroupPolicy = initReloadable(ResourcePolicy.class);
8584
adminGroupPolicy.setGroup(adminGroup);
8685
adminGroupPolicy.setRpType(TYPE_CUSTOM);
@@ -102,7 +101,7 @@ public void getPolicyForAdmin() throws Exception {
102101
assertThat(child.getAction()).isEqualTo("READ");
103102
assertThat(child.getType()).isEqualTo(TYPE_CUSTOM);
104103
assertThat(child.getGroup()).isEqualTo(Group.ADMIN);
105-
assertThat(child.getStartDate()).isEqualTo(dateFormat.format(groupDate));
104+
assertThat(child.getStartDate()).isEqualTo(groupDate.toString());
106105

107106
assertThat(child.getName()).isNull();
108107
assertThat(child.getEndDate()).isNull();
@@ -114,14 +113,14 @@ public void getPolicyForAdmin() throws Exception {
114113

115114
@Test
116115
public void getPolicyForAnonymous() throws Exception {
117-
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
118-
119-
// setup the Group
116+
// Set up the Group
120117
final Group anonGroup = mock(Group.class);
121118
when(anonGroup.getName()).thenReturn(Group.ANONYMOUS);
122119

123120
// set up the ResourcePolicy
124-
final Date groupDate = Date.from(Instant.now().minus(1, ChronoUnit.DAYS));
121+
final LocalDate groupDate = Instant.now().minus(1, ChronoUnit.DAYS)
122+
.atZone(ZoneId.systemDefault())
123+
.toLocalDate();
125124
final ResourcePolicy anonGroupPolicy = initReloadable(ResourcePolicy.class);
126125
anonGroupPolicy.setGroup(anonGroup);
127126
anonGroupPolicy.setRpType(TYPE_CUSTOM);
@@ -142,7 +141,7 @@ public void getPolicyForAnonymous() throws Exception {
142141
assertThat(child.getAction()).isEqualTo("READ");
143142
assertThat(child.getType()).isEqualTo(TYPE_CUSTOM);
144143
assertThat(child.getGroup()).isEqualTo(Group.ANONYMOUS);
145-
assertThat(child.getEndDate()).isEqualTo(dateFormat.format(groupDate));
144+
assertThat(child.getEndDate()).isEqualTo(groupDate.toString());
146145

147146
assertThat(child.getName()).isNull();
148147
assertThat(child.getEperson()).isNull();
@@ -154,15 +153,15 @@ public void getPolicyForAnonymous() throws Exception {
154153

155154
@Test
156155
public void getPolicyForEPerson() throws Exception {
157-
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
158-
159-
// setup the EPerson
156+
// Set up the EPerson
160157
final String epersonEmail = "bagit-policy-util-test";
161158
final EPerson ePerson = mock(EPerson.class);
162159
when(ePerson.getEmail()).thenReturn(epersonEmail);
163160

164161
// Create the ePerson policy
165-
final Date ePersonDate = Date.from(Instant.now().plus(1, ChronoUnit.DAYS));
162+
final LocalDate ePersonDate = Instant.now().plus(1, ChronoUnit.DAYS)
163+
.atZone(ZoneId.systemDefault())
164+
.toLocalDate();
166165
final ResourcePolicy ePersonPolicy = initReloadable(ResourcePolicy.class);
167166
ePersonPolicy.setEPerson(ePerson);
168167
ePersonPolicy.setRpType(TYPE_CUSTOM);
@@ -183,7 +182,7 @@ public void getPolicyForEPerson() throws Exception {
183182
assertThat(child.getAction()).isEqualTo("READ");
184183
assertThat(child.getType()).isEqualTo(TYPE_CUSTOM);
185184
assertThat(child.getEperson()).isEqualTo(epersonEmail);
186-
assertThat(child.getStartDate()).isEqualTo(dateFormat.format(ePersonDate));
185+
assertThat(child.getStartDate()).isEqualTo(ePersonDate.toString());
187186

188187
assertThat(child.getName()).isNull();
189188
assertThat(child.getGroup()).isNull();
@@ -195,7 +194,7 @@ public void getPolicyForEPerson() throws Exception {
195194

196195
@Test
197196
public void registerPolicies() throws Exception {
198-
// Read an aip in order to load a policy.xml
197+
// Read an AIP in order to load a policy.xml
199198
final URL resources = BagItPolicyUtilTest.class.getClassLoader().getResource("");
200199
assertThat(resources).isNotNull();
201200

0 commit comments

Comments
 (0)