Skip to content

Commit 4170958

Browse files
committed
backfill some basic tests
1 parent f9a76ec commit 4170958

4 files changed

Lines changed: 96 additions & 56 deletions

File tree

src/main/java/org/jenkinsci/plugins/parameterizedschedular/FieldAccessor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
import java.lang.reflect.Field;
44

5+
/**
6+
* lets work around some final classes I couldn't subclass, but don't want to duplicate everything
7+
*
8+
* @author jameswilson
9+
*
10+
*/
511
public class FieldAccessor {
612

7-
public static Object access(Object instance, String fieldName) {
13+
@SuppressWarnings("unchecked")
14+
public <T> T access(Object instance, String fieldName) {
815

916
try {
1017
Field field = instance.getClass().getDeclaredField(fieldName);
1118
boolean isAccessible = field.isAccessible();
1219
field.setAccessible(true);
1320
Object value = field.get(instance);
1421
field.setAccessible(isAccessible);
15-
return value;
22+
return (T)value;
1623
} catch (Exception e) {
1724
e.printStackTrace();
1825
}

src/main/java/org/jenkinsci/plugins/parameterizedschedular/ParameterizedCronTab.java

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
import hudson.scheduler.CronTab;
88
import hudson.scheduler.Hash;
99

10-
import java.lang.reflect.Method;
1110
import java.util.Calendar;
12-
import java.util.HashMap;
1311
import java.util.Map;
1412

15-
import com.google.common.collect.Maps;
16-
1713
import antlr.ANTLRException;
1814

15+
import com.google.common.collect.Maps;
16+
1917
/**
2018
* this is a copy of {@link CronTab} with added parameters map support
2119
*
@@ -24,15 +22,22 @@
2422
public class ParameterizedCronTab {
2523

2624
private final Map<String, String> parameterValues;
27-
// private final CronTab cronTab;
28-
private long[] bits;
29-
private Integer dayOfWeek;
25+
private final long[] bits;
26+
private final Integer dayOfWeek;
27+
private final CronTab cronTab;
3028

29+
/**
30+
* look at this fragile happy-crappy! Is this better than duplicating all of the Crontab source-tree? you decide.
31+
*
32+
* @param cronTab the crontab to use as a template
33+
* @param parameters the parameters in name=value key pairings
34+
*/
3135
public ParameterizedCronTab(CronTab cronTab, Map<String, String> parameters) {
32-
// this.cronTab = cronTab;
33-
bits = (long[])FieldAccessor.access(cronTab, "bits");
34-
dayOfWeek = (Integer)FieldAccessor.access(cronTab, "dayOfWeek");
35-
36+
this.cronTab = cronTab;
37+
FieldAccessor fieldAccessor = new FieldAccessor();
38+
bits = fieldAccessor.access(cronTab, "bits");
39+
dayOfWeek = fieldAccessor.access(cronTab, "dayOfWeek");
40+
3641
parameterValues = parameters;
3742
}
3843

@@ -55,54 +60,29 @@ public Map<String, String> getParameterValues() {
5560
return parameterValues;
5661
}
5762

58-
/**
59-
* Returns true if n-th bit is on.
60-
*/
61-
private boolean checkBits(long bitMask, int n) {
62-
return (bitMask|(1L<<n))==bitMask;
63-
}
63+
/**
64+
* Returns true if n-th bit is on.
65+
*/
66+
private boolean checkBits(long bitMask, int n) {
67+
return (bitMask | (1L << n)) == bitMask;
68+
}
6469

6570
public boolean check(Calendar calendar) {
66-
if(!checkBits(bits[0],calendar.get(MINUTE)))
67-
return false;
68-
if(!checkBits(bits[1],calendar.get(HOUR_OF_DAY)))
69-
return false;
70-
if(!checkBits(bits[2],calendar.get(DAY_OF_MONTH)))
71-
return false;
72-
if(!checkBits(bits[3],calendar.get(MONTH)+1))
73-
return false;
74-
if(!checkBits(dayOfWeek,calendar.get(Calendar.DAY_OF_WEEK)-1))
75-
return false;
76-
77-
return true;
71+
if (!checkBits(bits[0], calendar.get(MINUTE)))
72+
return false;
73+
if (!checkBits(bits[1], calendar.get(HOUR_OF_DAY)))
74+
return false;
75+
if (!checkBits(bits[2], calendar.get(DAY_OF_MONTH)))
76+
return false;
77+
if (!checkBits(bits[3], calendar.get(MONTH) + 1))
78+
return false;
79+
if (!checkBits(dayOfWeek, calendar.get(Calendar.DAY_OF_WEEK) - 1))
80+
return false;
7881

79-
// Object result;
80-
// try {
81-
// Method method = CronTab.class.getMethod("check", Calendar.class);
82-
// result = method.invoke(cronTab, calendar);
83-
// } catch (Exception e) {
84-
// e.printStackTrace();
85-
// return false;
86-
// }
87-
// return (Boolean) result;
88-
// return cronTab.check(calendar);
82+
return true;
8983
}
9084

9185
public String checkSanity() {
92-
// for( int i=0; i<5; i++ ) {
93-
// long bitMask = (i<4)?bits[i]:(long)dayOfWeek;
94-
// for( int j=BaseParser.LOWER_BOUNDS[i]; j<=BaseParser.UPPER_BOUNDS[i]; j++ ) {
95-
// if(!checkBits(bitMask,j)) {
96-
// // this rank has a sparse entry.
97-
// // if we have a sparse rank, one of them better be the left-most.
98-
// if(i>0)
99-
// return Messages.CronTab_do_you_really_mean_every_minute_when_you(spec, "0 " + spec.substring(spec.indexOf(' ')+1));
100-
// // once we find a sparse rank, upper ranks don't matter
101-
// return null;
102-
// }
103-
// }
104-
// }
105-
106-
return null;
86+
return cronTab.checkSanity();
10787
}
10888
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jenkinsci.plugins.parameterizedschedular;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertTrue;
6+
import hudson.scheduler.CronTab;
7+
8+
import java.util.GregorianCalendar;
9+
import java.util.Map;
10+
11+
import org.junit.Test;
12+
13+
import com.google.common.collect.Maps;
14+
15+
public class ParameterizedCronTabTest {
16+
17+
@Test
18+
public void ctor_happyPath() throws Exception {
19+
Map<String, String> parameters = Maps.newHashMap();
20+
parameters.put("one", "onevalue");
21+
CronTab testCronTab = new CronTab("* * * * *");
22+
ParameterizedCronTab testObject = new ParameterizedCronTab(testCronTab, parameters);
23+
24+
assertEquals(parameters, testObject.getParameterValues());
25+
assertTrue(testObject.check(new GregorianCalendar()));
26+
assertNull(testObject.checkSanity());
27+
}
28+
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.jenkinsci.plugins.parameterizedschedular;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertSame;
5+
6+
import org.junit.Test;
7+
8+
public class ParameterizedStaplerRequestTest {
9+
10+
@Test
11+
public void getParameter() {
12+
String testValue = "testvalue";
13+
ParameterizedStaplerRequest testObject = new ParameterizedStaplerRequest(testValue);
14+
assertSame(testValue, testObject.getParameter(null));
15+
}
16+
17+
@Test
18+
public void getParameterValues() {
19+
String testValue = "testvalue";
20+
ParameterizedStaplerRequest testObject = new ParameterizedStaplerRequest(testValue);
21+
assertArrayEquals(new String[] { testValue }, testObject.getParameterValues(null));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)