Skip to content

Commit befe936

Browse files
committed
test cases for Map bindings
1 parent f3f0e39 commit befe936

24 files changed

Lines changed: 919 additions & 165 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testapp.endpoint;
2+
3+
import act.controller.Controller;
4+
import org.osgl.http.H;
5+
import org.osgl.mvc.annotation.Action;
6+
import testapp.model.Contact;
7+
8+
/**
9+
* Test binding to a POJO object
10+
*/
11+
@Controller("/pojo")
12+
public class PojoBinding {
13+
14+
@Action(value = "ctct", methods = {H.Method.POST, H.Method.PUT})
15+
public Contact createContact(Contact contact) {
16+
return contact;
17+
}
18+
19+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package testapp.endpoint;
2+
3+
import act.controller.Controller;
4+
import org.osgl.mvc.annotation.Action;
5+
import testapp.model.RGB;
6+
7+
import java.util.Map;
8+
import java.util.TreeMap;
9+
10+
/**
11+
* Used to verify the parameter binding for Map data
12+
*/
13+
@Controller("/smpr")
14+
@SuppressWarnings("unused")
15+
public class SimpleMapParameterResolver {
16+
17+
@Action("bool_v")
18+
public Map<String, Boolean> boolVal(Map<String, Boolean> v) {
19+
return new TreeMap<String, Boolean>(v);
20+
}
21+
22+
@Action("byte_v")
23+
public Map<String, Byte> byteVal(Map<String, Byte> v) {
24+
return new TreeMap<String, Byte>(v);
25+
}
26+
27+
@Action("char_v")
28+
public Map<String, Character> charVal(Map<String, Character> v) {
29+
return new TreeMap<String, Character>(v);
30+
}
31+
32+
@Action("short_v")
33+
public Map<String, Short> shortVal(Map<String, Short> v) {
34+
return new TreeMap<String, Short>(v);
35+
}
36+
37+
@Action("int_v")
38+
public Map<String, Integer> intVal(Map<String, Integer> v) {
39+
return new TreeMap<String, Integer>(v);
40+
}
41+
42+
@Action("float_v")
43+
public Map<String, Float> floatVal(Map<String, Float> v) {
44+
return new TreeMap<String, Float>(v);
45+
}
46+
47+
48+
@Action("long_v")
49+
public Map<String, Long> longVal(Map<String, Long> v) {
50+
return new TreeMap<String, Long>(v);
51+
}
52+
53+
@Action("double_v")
54+
public Map<String, Double> doubleVal(Map<String, Double> v) {
55+
return new TreeMap<String, Double>(v);
56+
}
57+
58+
@Action("string_v")
59+
public Map<String, String> stringVal(Map<String, String> v) {
60+
return new TreeMap<String, String>(v);
61+
}
62+
63+
64+
@Action("enum_v")
65+
public Map<String, RGB> enumVal(Map<String, RGB> v) {
66+
return new TreeMap<String, RGB>(v);
67+
}
68+
69+
// --- testing simple type keys ----
70+
71+
@Action("bool_k")
72+
public Map<Boolean, String> boolKey(Map<Boolean, String> v) {
73+
return new TreeMap<Boolean, String>(v);
74+
}
75+
76+
@Action("byte_k")
77+
public Map<Byte, String> byteKey(Map<Byte, String> v) {
78+
return new TreeMap<Byte, String>(v);
79+
}
80+
81+
@Action("char_k")
82+
public Map<Character, String> charKey(Map<Character, String> v) {
83+
return new TreeMap<Character, String>(v);
84+
}
85+
86+
@Action("short_k")
87+
public Map<Short, String> shortKey(Map<Short, String> v) {
88+
return new TreeMap<Short, String>(v);
89+
}
90+
91+
@Action("int_k")
92+
public Map<Integer, String> intKey(Map<Integer, String> v) {
93+
return new TreeMap<Integer, String>(v);
94+
}
95+
96+
@Action("float_k")
97+
public Map<Float, String> floatKey(Map<Float, String> v) {
98+
return new TreeMap<Float, String>(v);
99+
}
100+
101+
102+
@Action("long_k")
103+
public Map<Long, String> longKey(Map<Long, String> v) {
104+
return new TreeMap<Long, String>(v);
105+
}
106+
107+
@Action("double_k")
108+
public Map<Double, String> doubleKey(Map<Double, String> v) {
109+
return new TreeMap<Double, String>(v);
110+
}
111+
112+
@Action("string_k")
113+
public Map<String, String> stringKey(Map<String, String> v) {
114+
return new TreeMap<String, String>(v);
115+
}
116+
117+
@Action("enum_k")
118+
public Map<RGB, String> enumKey(Map<RGB, String> v) {
119+
return new TreeMap<RGB, String>(v);
120+
}
121+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package testapp.model;
2+
3+
import act.data.Data;
4+
import org.osgl.util.C;
5+
6+
import java.util.Collection;
7+
import java.util.Set;
8+
9+
/**
10+
* A Contact
11+
*/
12+
@Data
13+
public class Contact extends ModelBase {
14+
15+
private String address;
16+
private String phone;
17+
private Set<String> emails;
18+
private String email;
19+
20+
public String getAddress() {
21+
return address;
22+
}
23+
24+
public void setAddress(String address) {
25+
this.address = address;
26+
}
27+
28+
public String getPhone() {
29+
return phone;
30+
}
31+
32+
public void setPhone(String phone) {
33+
this.phone = phone;
34+
}
35+
36+
public String getEmail() {
37+
return email;
38+
}
39+
40+
public void setEmail(String email) {
41+
this.email = email;
42+
}
43+
44+
public void setEmails(Collection<String> emails) {
45+
this.emails = C.newSet(emails);
46+
}
47+
48+
public Set<String> getEmails() {
49+
return emails;
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package testapp.model;
2+
3+
import act.Act;
4+
import org.osgl.util.KVStore;
5+
6+
public abstract class ModelBase {
7+
8+
private String id;
9+
10+
private KVStore kv;
11+
12+
public ModelBase() {
13+
id = Act.cuid();
14+
kv = new KVStore();
15+
}
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public void setId(String id) {
22+
this.id = id;
23+
}
24+
25+
public KVStore getKv() {
26+
return new KVStore(kv);
27+
}
28+
29+
public void setKv(KVStore kv) {
30+
this.kv = new KVStore(kv);
31+
}
32+
}
Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
package testapp.model;
22

33
/**
4-
* Created by luog on 18/06/16.
4+
* A model used to test parameter binding
55
*/
6-
public class User {
6+
public class User extends ModelBase {
7+
8+
private String name;
9+
private int age;
10+
private float f;
11+
private double d;
12+
13+
private Contact contact;
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public int getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(int age) {
28+
this.age = age;
29+
}
30+
31+
public float getF() {
32+
return f;
33+
}
34+
35+
public void setF(float f) {
36+
this.f = f;
37+
}
38+
39+
public double getD() {
40+
return d;
41+
}
42+
43+
public void setD(double d) {
44+
this.d = d;
45+
}
46+
47+
public Contact getContact() {
48+
return contact;
49+
}
50+
51+
public void setContact(Contact contact) {
52+
this.contact = contact;
53+
}
54+
755
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testapp.benchmark;
2+
3+
import act.util.IdGenerator;
4+
import org.junit.Test;
5+
import org.osgl.$;
6+
import testapp.TestBase;
7+
8+
import java.util.UUID;
9+
10+
public class CuidBenchmark extends TestBase {
11+
12+
@Test
13+
public void cuidVsUuid() {
14+
final int TIMES = 1000;
15+
IdGenerator idGenerator = new IdGenerator(1);
16+
long ts1 = $.ns();
17+
for (int i = 0; i < TIMES; ++i) {
18+
idGenerator.genId();
19+
}
20+
long ts2 = $.ns();
21+
ts1 = ts2 - ts1;
22+
for (int i = 0; i < TIMES; ++i) {
23+
UUID.randomUUID();
24+
}
25+
ts2 = $.ns() - ts2;
26+
System.out.printf("cuid: %sns\nuuid: %sns\n", ts1 / TIMES, ts2 / TIMES);
27+
yes(ts2 > ts1);
28+
System.out.println(idGenerator.genId());
29+
System.out.println(UUID.randomUUID());
30+
}
31+
}

0 commit comments

Comments
 (0)