Skip to content

Commit

Permalink
WW-5233 Copies Tiles OGNL related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Oct 2, 2022
1 parent 044021c commit 3207bfd
Show file tree
Hide file tree
Showing 7 changed files with 929 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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 org.apache.tiles.ognl;

import org.apache.tiles.request.Request;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Map;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* Tests {@link AnyScopePropertyAccessor}.
*/
public class AnyScopePropertyAccessorTest {

/**
* The accessor to test.
*/
private AnyScopePropertyAccessor accessor;

/**
* Sets up the test.
*/
@Before
public void setUp() {
accessor = new AnyScopePropertyAccessor();
}

/**
* Test method for {@link AnyScopePropertyAccessor#getProperty(Map, Object, Object)}.
*/
@Test
public void testGetProperty() {
Request request = createMock(Request.class);
Map<String, Object> oneScope = createMock(Map.class);
Map<String, Object> twoScope = createMock(Map.class);

expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes();
expect(request.getContext("one")).andReturn(oneScope).anyTimes();
expect(request.getContext("two")).andReturn(twoScope).anyTimes();
expect(oneScope.containsKey("name1")).andReturn(true);
expect(oneScope.get("name1")).andReturn("value1");
expect(oneScope.containsKey("name2")).andReturn(false);
expect(oneScope.containsKey("name3")).andReturn(false);
expect(twoScope.containsKey("name2")).andReturn(true);
expect(twoScope.get("name2")).andReturn("value2");
expect(twoScope.containsKey("name3")).andReturn(false);

replay(request, oneScope, twoScope);
assertEquals("value1", accessor.getProperty(null, request, "name1"));
assertEquals("value2", accessor.getProperty(null, request, "name2"));
assertNull(accessor.getProperty(null, request, "name3"));
verify(request, oneScope, twoScope);
}

@Test
public void testGetSourceAccessor() {
Request request = createMock(Request.class);
Map<String, Object> oneScope = createMock(Map.class);
Map<String, Object> twoScope = createMock(Map.class);

expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes();
expect(request.getContext("one")).andReturn(oneScope).anyTimes();
expect(request.getContext("two")).andReturn(twoScope).anyTimes();
expect(oneScope.containsKey("name1")).andReturn(true);
expect(oneScope.containsKey("name2")).andReturn(false);
expect(oneScope.containsKey("name3")).andReturn(false);
expect(twoScope.containsKey("name2")).andReturn(true);
expect(twoScope.containsKey("name3")).andReturn(false);

replay(request, oneScope, twoScope);
assertEquals(".getContext(\"one\").get(index)", accessor.getSourceAccessor(null, request, "name1"));
assertEquals(".getContext(\"two\").get(index)", accessor.getSourceAccessor(null, request, "name2"));
assertNull(accessor.getSourceAccessor(null, request, "name3"));
verify(request, oneScope, twoScope);
}

@Test
public void testGetSourceSetter() {
Request request = createMock(Request.class);
Map<String, Object> oneScope = createMock(Map.class);
Map<String, Object> twoScope = createMock(Map.class);

expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes();
expect(request.getContext("one")).andReturn(oneScope).anyTimes();
expect(request.getContext("two")).andReturn(twoScope).anyTimes();
expect(oneScope.containsKey("name1")).andReturn(true);
expect(oneScope.containsKey("name2")).andReturn(false);
expect(oneScope.containsKey("name3")).andReturn(false);
expect(twoScope.containsKey("name2")).andReturn(true);
expect(twoScope.containsKey("name3")).andReturn(false);

replay(request, oneScope, twoScope);
assertEquals(".getContext(\"one\").put(index, target)", accessor.getSourceSetter(null, request, "name1"));
assertEquals(".getContext(\"two\").put(index, target)", accessor.getSourceSetter(null, request, "name2"));
assertEquals(".getContext(\"one\").put(index, target)", accessor.getSourceSetter(null, request, "name3"));
verify(request, oneScope, twoScope);
}

/**
* Test method for {@link AnyScopePropertyAccessor#setProperty(Map, Object, Object, Object)}.
*/
@Test
public void testSetProperty() {
Request request = createMock(Request.class);
Map<String, Object> oneScope = createMock(Map.class);
Map<String, Object> twoScope = createMock(Map.class);

expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes();
expect(request.getContext("one")).andReturn(oneScope).anyTimes();
expect(request.getContext("two")).andReturn(twoScope).anyTimes();
expect(oneScope.containsKey("name1")).andReturn(true);
expect(oneScope.put("name1", "otherValue1")).andReturn("value1");
expect(oneScope.containsKey("name2")).andReturn(false);
expect(oneScope.containsKey("name3")).andReturn(false);
expect(twoScope.containsKey("name2")).andReturn(true);
expect(twoScope.put("name2", "otherValue2")).andReturn("value2");
expect(twoScope.containsKey("name3")).andReturn(false);
expect(oneScope.put("name3", "otherValue3")).andReturn(null);

replay(request, oneScope, twoScope);
accessor.setProperty(null, request, "name1", "otherValue1");
accessor.setProperty(null, request, "name2", "otherValue2");
accessor.setProperty(null, request, "name3", "otherValue3");
verify(request, oneScope, twoScope);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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 org.apache.tiles.ognl;

import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.PropertyAccessor;
import org.junit.Test;

import java.util.Map;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;

/**
* Tests {@link DelegatePropertyAccessor}.
*/
public class DelegatePropertyAccessorTest {

/**
* Test method for {@link DelegatePropertyAccessor#getProperty(Map, Object, Object)}.
*
* @throws OgnlException If something goes wrong.
*/
@Test
public void testGetProperty() throws OgnlException {
PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class);
PropertyAccessor mockAccessor = createMock(PropertyAccessor.class);
Map<String, Object> context = createMock(Map.class);
expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor);
expect(mockAccessor.getProperty(context, 1, "property")).andReturn("value");

replay(factory, mockAccessor, context);
PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory);
assertEquals("value", accessor.getProperty(context, 1, "property"));
verify(factory, mockAccessor, context);
}

/**
* Test method for {@link DelegatePropertyAccessor#setProperty(Map, Object, Object, Object)}.
*
* @throws OgnlException If something goes wrong.
*/
@Test
public void testSetProperty() throws OgnlException {
PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class);
PropertyAccessor mockAccessor = createMock(PropertyAccessor.class);
Map<String, Object> context = createMock(Map.class);
expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor);
mockAccessor.setProperty(context, 1, "property", "value");

replay(factory, mockAccessor, context);
PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory);
accessor.setProperty(context, 1, "property", "value");
verify(factory, mockAccessor, context);
}

/**
* Test method for {@link DelegatePropertyAccessor#getSourceAccessor(OgnlContext, Object, Object)}.
*/
@Test
public void testGetSourceAccessor() {
PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class);
PropertyAccessor mockAccessor = createMock(PropertyAccessor.class);
OgnlContext context = createMock(OgnlContext.class);
expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor);
expect(mockAccessor.getSourceAccessor(context, 1, "property")).andReturn("method");

replay(factory, mockAccessor, context);
PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory);
assertEquals("method", accessor.getSourceAccessor(context, 1, "property"));
verify(factory, mockAccessor, context);
}

/**
* Test method for {@link DelegatePropertyAccessor#getSourceSetter(OgnlContext, Object, Object)}.
*/
@Test
public void testGetSourceSetter() {
PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class);
PropertyAccessor mockAccessor = createMock(PropertyAccessor.class);
OgnlContext context = createMock(OgnlContext.class);
expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor);
expect(mockAccessor.getSourceSetter(context, 1, "property")).andReturn("method");

replay(factory, mockAccessor, context);
PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory);
assertEquals("method", accessor.getSourceSetter(context, 1, "property"));
verify(factory, mockAccessor, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 org.apache.tiles.ognl;

import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.PropertyAccessor;
import org.junit.Test;

import java.util.Map;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;

/**
* Tests {@link NestedObjectDelegatePropertyAccessor}.
*/
public class NestedObjectDelegatePropertyAccessorTest {

/**
* Test method for {@link NestedObjectDelegatePropertyAccessor#getProperty(Map, Object, Object)}.
* @throws OgnlException If something goes wrong.
*/
@Test
public void testGetProperty() throws OgnlException {
NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class);
PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class);
Map<String, Object> context = createMock(Map.class);
expect(propertyAccessor.getProperty(context, "nested", "property")).andReturn("value");
expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested");

replay(nestedObjectExtractor, propertyAccessor, context);
PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor);
assertEquals("value", accessor.getProperty(context, 1, "property"));
verify(nestedObjectExtractor, propertyAccessor, context);
}

/**
* Test method for {@link NestedObjectDelegatePropertyAccessor#setProperty(Map, Object, Object, Object)}.
* @throws OgnlException If something goes wrong.
*/
@Test
public void testSetProperty() throws OgnlException {
NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class);
PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class);
Map<String, Object> context = createMock(Map.class);
propertyAccessor.setProperty(context, "nested", "property", "value");
expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested");

replay(nestedObjectExtractor, propertyAccessor, context);
PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor);
accessor.setProperty(context, 1, "property", "value");
verify(nestedObjectExtractor, propertyAccessor, context);
}

/**
* Test method for {@link NestedObjectDelegatePropertyAccessor#getSourceAccessor(OgnlContext, Object, Object)}.
*/
@Test
public void testGetSourceAccessor() {
NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class);
PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class);
OgnlContext context = createMock(OgnlContext.class);
expect(propertyAccessor.getSourceAccessor(context, "nested", "property")).andReturn("method");
expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested");

replay(nestedObjectExtractor, propertyAccessor, context);
PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor);
assertEquals("method", accessor.getSourceAccessor(context, 1, "property"));
verify(nestedObjectExtractor, propertyAccessor, context);
}

/**
* Test method for {@link NestedObjectDelegatePropertyAccessor#getSourceSetter(OgnlContext, Object, Object)}.
*/
@Test
public void testGetSourceSetter() {
NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class);
PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class);
OgnlContext context = createMock(OgnlContext.class);
expect(propertyAccessor.getSourceSetter(context, "nested", "property")).andReturn("method");
expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested");

replay(nestedObjectExtractor, propertyAccessor, context);
PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor);
assertEquals("method", accessor.getSourceSetter(context, 1, "property"));
verify(nestedObjectExtractor, propertyAccessor, context);
}

}
Loading

0 comments on commit 3207bfd

Please sign in to comment.