-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGNL-102 Improves performance when null was returned
- Loading branch information
1 parent
fc879b1
commit 3c76136
Showing
4 changed files
with
199 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package ognl.test; | ||
|
||
import ognl.DefaultMemberAccess; | ||
import ognl.Ognl; | ||
import ognl.OgnlContext; | ||
import ognl.OgnlException; | ||
import ognl.OgnlRuntime; | ||
import ognl.PropertyAccessor; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class PropertyAccessorTest { | ||
|
||
private OgnlContext context; | ||
|
||
@Test | ||
public void shouldAccessProperty_usingCustomAccessor() throws Exception { | ||
// given | ||
Parent root = new Parent(new Child("Luk")); | ||
String expectedResult = "Luk"; | ||
|
||
// then | ||
assertEquals(expectedResult, Ognl.getValue("child", context, root)); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
this.context = Ognl.createDefaultContext(null, new DefaultMemberAccess(false)); | ||
OgnlRuntime.setPropertyAccessor(Parent.class, new ChildPropertyAccessor()); | ||
} | ||
|
||
public static class Child { | ||
String name; | ||
|
||
public Child(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
public static class Parent { | ||
Child child; | ||
|
||
public Parent(Child child) { | ||
this.child = child; | ||
} | ||
|
||
public Child getChild() { | ||
return child; | ||
} | ||
} | ||
|
||
public static class ChildPropertyAccessor implements PropertyAccessor { | ||
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException { | ||
} | ||
|
||
public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException { | ||
if (target instanceof Parent && "child".equals(name)) { | ||
return OgnlRuntime.getProperty(context, ((Parent) target).getChild(), "name"); | ||
} | ||
return null; | ||
} | ||
|
||
public String getSourceAccessor(OgnlContext context, Object target, Object index) { | ||
return index.toString(); | ||
} | ||
|
||
public String getSourceSetter(OgnlContext context, Object target, Object index) { | ||
return index.toString(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.