Add wildcard search functionality from BATCH-1797 to MapJobInstanceDao and tests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2013 the original author or authors.
|
||||
* Copyright 2006-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -305,11 +305,9 @@ JobInstanceDao, InitializingBean {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JobInstance> findJobInstancesByName(String jobName,final int start,
|
||||
final int count) {
|
||||
public List<JobInstance> findJobInstancesByName(String jobName, final int start, final int count) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
ResultSetExtractor extractor = new ResultSetExtractor() {
|
||||
|
||||
private List<JobInstance> list = new ArrayList<JobInstance>();
|
||||
|
||||
@Override
|
||||
@@ -326,14 +324,11 @@ JobInstanceDao, InitializingBean {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//check if the name contains a wildcard
|
||||
if (jobName.contains(STAR_WILDCARD)) {
|
||||
//swap for sql wildcard
|
||||
jobName = jobName.replaceAll("\\" + STAR_WILDCARD, SQL_WILDCARD);
|
||||
}//end if
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<JobInstance> result = (List<JobInstance>) getJdbcTemplate().query(getQuery(FIND_LAST_JOBS_LIKE_NAME),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2013 the original author or authors.
|
||||
* Copyright 2006-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,6 +36,8 @@ import org.springframework.util.Assert;
|
||||
* In-memory implementation of {@link JobInstanceDao}.
|
||||
*/
|
||||
public class MapJobInstanceDao implements JobInstanceDao {
|
||||
private static final String STAR_WILDCARD = "\\*";
|
||||
private static final String STAR_WILDCARD_PATTERN = ".*";
|
||||
|
||||
// JDK6 Make a ConcurrentSkipListSet: tends to add on end
|
||||
private final Map<String, JobInstance> jobInstances = new ConcurrentHashMap<String, JobInstance>();
|
||||
@@ -95,17 +97,10 @@ public class MapJobInstanceDao implements JobInstanceDao {
|
||||
result.add(instance);
|
||||
}
|
||||
}
|
||||
Collections.sort(result, new Comparator<JobInstance>() {
|
||||
// sort by ID descending
|
||||
@Override
|
||||
public int compare(JobInstance o1, JobInstance o2) {
|
||||
return Long.signum(o2.getId() - o1.getId());
|
||||
}
|
||||
});
|
||||
|
||||
int startIndex = Math.min(start, result.size());
|
||||
int endIndex = Math.min(start + count, result.size());
|
||||
return result.subList(startIndex, endIndex);
|
||||
sortDescending(result);
|
||||
|
||||
return subset(result, start, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -135,6 +130,35 @@ public class MapJobInstanceDao implements JobInstanceDao {
|
||||
|
||||
@Override
|
||||
public List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
|
||||
return getJobInstances(jobName,start,count);
|
||||
List<JobInstance> result = new ArrayList<JobInstance>();
|
||||
String convertedJobName = jobName.replaceAll(STAR_WILDCARD, STAR_WILDCARD_PATTERN);
|
||||
|
||||
for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) {
|
||||
JobInstance instance = instanceEntry.getValue();
|
||||
|
||||
if(instance.getJobName().matches(convertedJobName)) {
|
||||
result.add(instance);
|
||||
}
|
||||
}
|
||||
|
||||
sortDescending(result);
|
||||
|
||||
return subset(result, start, count);
|
||||
}
|
||||
|
||||
private void sortDescending(List<JobInstance> result) {
|
||||
Collections.sort(result, new Comparator<JobInstance>() {
|
||||
@Override
|
||||
public int compare(JobInstance o1, JobInstance o2) {
|
||||
return Long.signum(o2.getId() - o1.getId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<JobInstance> subset(List<JobInstance> jobInstances, int start, int count) {
|
||||
int startIndex = Math.min(start, jobInstances.size());
|
||||
int endIndex = Math.min(start + count, jobInstances.size());
|
||||
|
||||
return jobInstances.subList(startIndex, endIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
* Copyright 2008-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -91,18 +91,16 @@ public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
|
||||
@Test
|
||||
public void testJobInstanceWildcard() {
|
||||
//look up a job using a wildcard (* substituted to %)
|
||||
// unrelated job instance that should be ignored by the query
|
||||
dao.createJobInstance("anotherJob", new JobParameters());
|
||||
// we need two instances of the same job to check ordering
|
||||
dao.createJobInstance("someJob", new JobParameters());
|
||||
//now look for them
|
||||
|
||||
List<JobInstance> jobInstances = dao.findJobInstancesByName("*Job", 0, 2);
|
||||
assertEquals(2, jobInstances.size());
|
||||
|
||||
for (JobInstance instance : jobInstances) {
|
||||
assertTrue(instance.getJobName().contains("Job"));
|
||||
}//end for
|
||||
//try with after wildcards
|
||||
}
|
||||
|
||||
jobInstances = dao.getJobInstances("Job*", 0, 2);
|
||||
assertTrue(jobInstances.isEmpty());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2012 the original author or authors.
|
||||
* Copyright 2008-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,8 +15,15 @@
|
||||
*/
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class MapJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
@@ -26,4 +33,30 @@ public class MapJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
return new MapJobInstanceDao();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardPrefix() {
|
||||
MapJobInstanceDao mapJobInstanceDao = new MapJobInstanceDao();
|
||||
mapJobInstanceDao.createJobInstance("testJob", new JobParameters());
|
||||
mapJobInstanceDao.createJobInstance("Jobtest", new JobParameters());
|
||||
List<JobInstance> jobInstances = mapJobInstanceDao.findJobInstancesByName("*Job", 0, 2);
|
||||
assertTrue("Invalid matching job instances found, expected 1, got: " + jobInstances.size(), jobInstances.size() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardSuffix() {
|
||||
MapJobInstanceDao mapJobInstanceDao = new MapJobInstanceDao();
|
||||
mapJobInstanceDao.createJobInstance("testJob", new JobParameters());
|
||||
mapJobInstanceDao.createJobInstance("Jobtest", new JobParameters());
|
||||
List<JobInstance> jobInstances = mapJobInstanceDao.findJobInstancesByName("Job*", 0, 2);
|
||||
assertTrue("No matching job instances found, expected 1, got: " + jobInstances.size(), jobInstances.size() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardRange() {
|
||||
MapJobInstanceDao mapJobInstanceDao = new MapJobInstanceDao();
|
||||
mapJobInstanceDao.createJobInstance("testJob", new JobParameters());
|
||||
mapJobInstanceDao.createJobInstance("Jobtest", new JobParameters());
|
||||
List<JobInstance> jobInstances = mapJobInstanceDao.findJobInstancesByName("*Job*", 0, 2);
|
||||
assertTrue("No matching job instances found, expected 2, got: " + jobInstances.size(), jobInstances.size() == 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user