Skip to content

Commit 86c7289

Browse files
committed
API Change: Delete Locals (Request and Session will be affected) fix #60
1 parent c5046a7 commit 86c7289

11 files changed

Lines changed: 300 additions & 66 deletions

File tree

jooby-hbs/src/main/java/org/jooby/internal/hbs/RequestValueResolver.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,38 @@
2222
import java.util.Map.Entry;
2323
import java.util.Set;
2424

25-
import org.jooby.Locals;
25+
import org.jooby.Request;
2626

2727
import com.github.jknack.handlebars.ValueResolver;
2828

2929
/**
30-
* Handlebars value resolver for accessing to {@link Locals}, like request and session objects.
30+
* Handlebars value resolver for accessing to {@link Request}, like request and session objects.
3131
*
3232
* @author edgar
3333
*/
34-
public class LocalsValueResolver implements ValueResolver {
34+
public class RequestValueResolver implements ValueResolver {
3535

3636
@Override
3737
public Object resolve(final Object context, final String name) {
3838
Object value = null;
39-
if (context instanceof Locals) {
40-
value = ((Locals) context).get(name).orElse(null);
39+
if (context instanceof Request) {
40+
value = ((Request) context).get(name).orElse(null);
4141
}
4242
return value == null ? UNRESOLVED : value;
4343
}
4444

4545
@Override
4646
public Object resolve(final Object context) {
47-
if (context instanceof Locals) {
47+
if (context instanceof Request) {
4848
return context;
4949
}
5050
return UNRESOLVED;
5151
}
5252

5353
@Override
5454
public Set<Entry<String, Object>> propertySet(final Object context) {
55-
if (context instanceof Locals) {
56-
return ((Locals) context).attributes().entrySet();
55+
if (context instanceof Request) {
56+
return ((Request) context).attributes().entrySet();
5757
}
5858
return Collections.emptySet();
5959
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby.internal.hbs;
20+
21+
import java.util.Collections;
22+
import java.util.Map;
23+
import java.util.Map.Entry;
24+
import java.util.Set;
25+
26+
import org.jooby.Session;
27+
28+
import com.github.jknack.handlebars.ValueResolver;
29+
30+
/**
31+
* Handlebars value resolver for accessing to {@link Session}, like request and session objects.
32+
*
33+
* @author edgar
34+
*/
35+
public class SessionValueResolver implements ValueResolver {
36+
37+
@Override
38+
public Object resolve(final Object context, final String name) {
39+
Object value = null;
40+
if (context instanceof Session) {
41+
value = ((Session) context).get(name).toOptional().orElse(null);
42+
}
43+
return value == null ? UNRESOLVED : value;
44+
}
45+
46+
@Override
47+
public Object resolve(final Object context) {
48+
if (context instanceof Session) {
49+
return context;
50+
}
51+
return UNRESOLVED;
52+
}
53+
54+
@SuppressWarnings({"unchecked", "rawtypes" })
55+
@Override
56+
public Set<Entry<String, Object>> propertySet(final Object context) {
57+
if (context instanceof Session) {
58+
Map session = ((Session) context).attributes();
59+
return session.entrySet();
60+
}
61+
return Collections.emptySet();
62+
}
63+
64+
}

jooby-hbs/src/test/java/org/jooby/internal/hbs/RequestValueResolverTest.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,91 @@
1010
import java.util.Optional;
1111
import java.util.Set;
1212

13-
import org.jooby.Locals;
1413
import org.jooby.MockUnit;
15-
import org.jooby.internal.hbs.LocalsValueResolver;
14+
import org.jooby.Request;
1615
import org.junit.Test;
1716

1817
import com.github.jknack.handlebars.ValueResolver;
1918

20-
public class LocalsValueResolverTest {
19+
public class RequestValueResolverTest {
2120

2221
@Test
2322
public void resolveProperty() throws Exception {
24-
new MockUnit(Locals.class)
23+
new MockUnit(Request.class)
2524
.expect(unit -> {
26-
Locals locals = unit.get(Locals.class);
27-
expect(locals.get("prop")).andReturn(Optional.of("x"));
25+
Request Request = unit.get(Request.class);
26+
expect(Request.get("prop")).andReturn(Optional.of("x"));
2827
})
2928
.run(unit -> {
30-
assertEquals("x", new LocalsValueResolver().resolve(unit.get(Locals.class), "prop"));
29+
assertEquals("x", new RequestValueResolver().resolve(unit.get(Request.class), "prop"));
3130
});
3231
}
3332

3433
@Test
3534
public void resolveMissingProperty() throws Exception {
36-
new MockUnit(Locals.class)
35+
new MockUnit(Request.class)
3736
.expect(unit -> {
38-
Locals locals = unit.get(Locals.class);
39-
expect(locals.get("prop")).andReturn(Optional.empty());
37+
Request Request = unit.get(Request.class);
38+
expect(Request.get("prop")).andReturn(Optional.empty());
4039
})
4140
.run(
4241
unit -> {
4342
assertEquals(ValueResolver.UNRESOLVED,
44-
new LocalsValueResolver().resolve(unit.get(Locals.class), "prop"));
43+
new RequestValueResolver().resolve(unit.get(Request.class), "prop"));
4544
});
4645
}
4746

4847
@Test
49-
public void skipNoLocals() throws Exception {
50-
new MockUnit(Locals.class)
48+
public void skipNoRequest() throws Exception {
49+
new MockUnit(Request.class)
5150
.run(
5251
unit -> {
5352
assertEquals(ValueResolver.UNRESOLVED,
54-
new LocalsValueResolver().resolve(new Object(), "prop"));
53+
new RequestValueResolver().resolve(new Object(), "prop"));
5554
});
5655
}
5756

5857
@Test
5958
public void resolveContext() throws Exception {
60-
new MockUnit(Locals.class)
59+
new MockUnit(Request.class)
6160
.run(unit -> {
62-
assertEquals(unit.get(Locals.class),
63-
new LocalsValueResolver().resolve(unit.get(Locals.class)));
61+
assertEquals(unit.get(Request.class),
62+
new RequestValueResolver().resolve(unit.get(Request.class)));
6463
});
6564
}
6665

6766
@Test
68-
public void resolveNoLocals() throws Exception {
67+
public void resolveNoRequest() throws Exception {
6968
new MockUnit()
7069
.run(unit -> {
7170
assertEquals(ValueResolver.UNRESOLVED,
72-
new LocalsValueResolver().resolve(new Object()));
71+
new RequestValueResolver().resolve(new Object()));
7372
});
7473
}
7574

7675
@SuppressWarnings("unchecked")
7776
@Test
7877
public void propertySet() throws Exception {
7978
Set<Entry<String, Object>> entries = new HashSet<>();
80-
new MockUnit(Locals.class)
79+
new MockUnit(Request.class)
8180
.expect(unit -> {
82-
Locals locals = unit.get(Locals.class);
81+
Request Request = unit.get(Request.class);
8382
Map<String, Object> attributes = unit.mock(Map.class);
84-
expect(locals.attributes()).andReturn(attributes);
83+
expect(Request.attributes()).andReturn(attributes);
8584
expect(attributes.entrySet()).andReturn(entries);
8685
})
8786
.run(unit -> {
8887
assertEquals(entries,
89-
new LocalsValueResolver().propertySet(unit.get(Locals.class)));
88+
new RequestValueResolver().propertySet(unit.get(Request.class)));
9089
});
9190
}
9291

9392
@Test
9493
public void propertySetAnything() throws Exception {
95-
new MockUnit(Locals.class)
94+
new MockUnit(Request.class)
9695
.run(unit -> {
9796
assertEquals(Collections.emptySet(),
98-
new LocalsValueResolver().propertySet(new Object()));
97+
new RequestValueResolver().propertySet(new Object()));
9998
});
10099
}
101100

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.jooby.internal.hbs;
2+
3+
import static org.easymock.EasyMock.expect;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import java.util.Collections;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
import java.util.Optional;
10+
import java.util.Set;
11+
12+
import org.jooby.MockUnit;
13+
import org.jooby.Mutant;
14+
import org.jooby.Session;
15+
import org.junit.Test;
16+
17+
import com.github.jknack.handlebars.ValueResolver;
18+
import com.google.common.collect.ImmutableMap;
19+
import com.google.common.collect.ImmutableSet;
20+
import com.google.common.collect.Maps;
21+
22+
public class SessionValueResolverTest {
23+
24+
@Test
25+
public void resolveProperty() throws Exception {
26+
new MockUnit(Session.class)
27+
.expect(unit -> {
28+
Session session = unit.get(Session.class);
29+
Mutant v = unit.mock(Mutant.class);
30+
expect(v.toOptional()).andReturn(Optional.of("x"));
31+
expect(session.get("prop")).andReturn(v);
32+
})
33+
.run(unit -> {
34+
assertEquals("x", new SessionValueResolver().resolve(unit.get(Session.class), "prop"));
35+
});
36+
}
37+
38+
@Test
39+
public void resolveMissingProperty() throws Exception {
40+
new MockUnit(Session.class)
41+
.expect(unit -> {
42+
Session session = unit.get(Session.class);
43+
Mutant v = unit.mock(Mutant.class);
44+
expect(v.toOptional()).andReturn(Optional.empty());
45+
expect(session.get("prop")).andReturn(v);
46+
})
47+
.run(
48+
unit -> {
49+
assertEquals(ValueResolver.UNRESOLVED,
50+
new SessionValueResolver().resolve(unit.get(Session.class), "prop"));
51+
});
52+
}
53+
54+
@Test
55+
public void skipSession() throws Exception {
56+
new MockUnit(Session.class)
57+
.run(
58+
unit -> {
59+
assertEquals(ValueResolver.UNRESOLVED,
60+
new SessionValueResolver().resolve(new Object(), "prop"));
61+
});
62+
}
63+
64+
@Test
65+
public void resolveContext() throws Exception {
66+
new MockUnit(Session.class)
67+
.run(unit -> {
68+
assertEquals(unit.get(Session.class),
69+
new SessionValueResolver().resolve(unit.get(Session.class)));
70+
});
71+
}
72+
73+
@Test
74+
public void resolveNoSession() throws Exception {
75+
new MockUnit()
76+
.run(unit -> {
77+
assertEquals(ValueResolver.UNRESOLVED,
78+
new SessionValueResolver().resolve(new Object()));
79+
});
80+
}
81+
82+
@Test
83+
public void propertySet() throws Exception {
84+
Map<String, String> attributes = ImmutableMap.of("k", "v");
85+
Set<Entry<String, Object>> entries = ImmutableSet.of(Maps.immutableEntry("k", "v"));
86+
new MockUnit(Session.class)
87+
.expect(unit -> {
88+
Session Rsession = unit.get(Session.class);
89+
90+
expect(Rsession.attributes()).andReturn(attributes);
91+
})
92+
.run(unit -> {
93+
assertEquals(entries,
94+
new SessionValueResolver().propertySet(unit.get(Session.class)));
95+
});
96+
}
97+
98+
@Test
99+
public void propertySetAnything() throws Exception {
100+
new MockUnit(Session.class)
101+
.run(unit -> {
102+
assertEquals(Collections.emptySet(),
103+
new SessionValueResolver().propertySet(new Object()));
104+
});
105+
}
106+
107+
}

jooby/src/main/java/org/jooby/Mutant.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ default byte byteValue() {
7373
return to(byte.class);
7474
}
7575

76+
/**
77+
* @return Get a byte when possible.
78+
*/
79+
default char charValue() {
80+
return to(char.class);
81+
}
82+
7683
/**
7784
* @return Get a short when possible.
7885
*/

0 commit comments

Comments
 (0)