You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2013/12/23 08:32:20 UTC

git commit: [OLINGO-86] Test for value access

Updated Branches:
  refs/heads/master 636eed236 -> d9a7f2db4


[OLINGO-86] Test for value access


Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/d9a7f2db
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/d9a7f2db
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/d9a7f2db

Branch: refs/heads/master
Commit: d9a7f2db447410b0d1b842d8f4b3697890cbed80
Parents: 636eed2
Author: Michael Bolz <mi...@apache.org>
Authored: Mon Dec 23 08:18:43 2013 +0100
Committer: Michael Bolz <mi...@apache.org>
Committed: Mon Dec 23 08:18:43 2013 +0100

----------------------------------------------------------------------
 .../processor/core/ListsProcessorTest.java      |  40 +++
 .../datasource/AnnotationValueAccessTest.java   | 237 +++++++++++++++++
 .../core/datasource/BeanPropertyAccessTest.java | 261 +++++++++++++++++++
 3 files changed, 538 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/d9a7f2db/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessorTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessorTest.java b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessorTest.java
new file mode 100644
index 0000000..b7642fb
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessorTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ * 
+ * Licensed 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.olingo.odata2.annotation.processor.core;
+
+import org.apache.olingo.odata2.annotation.processor.core.datasource.AnnotationInMemoryDs;
+import org.apache.olingo.odata2.annotation.processor.core.datasource.AnnotationValueAccess;
+import org.apache.olingo.odata2.annotation.processor.core.datasource.DataSource;
+import org.apache.olingo.odata2.annotation.processor.core.datasource.ValueAccess;
+import org.apache.olingo.odata2.annotation.processor.core.model.Building;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class ListsProcessorTest {
+
+  @Test
+  public void init() throws ODataException {
+    DataSource dataSource = new AnnotationInMemoryDs(Building.class.getPackage().getName());
+    ValueAccess valueAccess = new AnnotationValueAccess();
+    ListsProcessor lp = new ListsProcessor(dataSource, valueAccess);
+    
+    Assert.assertNotNull(lp);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/d9a7f2db/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationValueAccessTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationValueAccessTest.java b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationValueAccessTest.java
new file mode 100644
index 0000000..5e9147f
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationValueAccessTest.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ * 
+ * Licensed 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.olingo.odata2.annotation.processor.core.datasource;
+
+import junit.framework.Assert;
+
+import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataNotImplementedException;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ *
+ */
+public class AnnotationValueAccessTest {
+
+  @Test
+  public void getPropertyType() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Class<?> type = ava.getPropertyType(data, property);
+    
+    Assert.assertEquals(String.class, type);
+  }
+
+  @Test(expected=ODataNotImplementedException.class)
+  public void getPropertyTypeNotAnnotated() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    NotAnnotatedBean data = new NotAnnotatedBean();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Class<?> type = ava.getPropertyType(data, property);
+    
+    Assert.assertEquals(String.class, type);
+  }
+
+  @Test
+  public void getPropertyValue() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertEquals(String.class, value.getClass());
+    Assert.assertEquals("A Name", value);
+  }
+
+  @Test
+  public void getPropertyValueNull() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void getPropertyValueNullData() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = null;
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test(expected=ODataNotImplementedException.class)
+  public void getPropertyValueNotAnnotated() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    NotAnnotatedBean data = new NotAnnotatedBean();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    Assert.assertEquals("A Name", value);
+  }
+
+  @Test
+  public void setPropertyValue() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+    
+    Object value = "Another Name";
+    ava.setPropertyValue(data, property, value);
+    
+    Assert.assertEquals("Another Name", data.name);
+  }
+
+  @Test(expected=ODataNotImplementedException.class)
+  public void setPropertyValueNotAnnotated() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    NotAnnotatedBean data = new NotAnnotatedBean();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+    
+    Object value = "Another Name";
+    ava.setPropertyValue(data, property, value);
+  }
+
+  @Test
+  public void setPropertyValueNull() throws ODataException {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+    
+    ava.setPropertyValue(data, property, null);
+    
+    Assert.assertNull(null, data.name);
+  }
+
+  @Test
+  public void getMappingValue() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("MyMappedProperty");
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertEquals(String.class, value.getClass());
+    Assert.assertEquals("mapped property value", value);
+  }
+
+  @Test
+  public void getMappingValueNullMapping() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "property";
+    EdmMapping mapping = null;
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void getMappingValueNullValue() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = null;
+    EdmMapping mapping = mockMapping("MyMappedProperty");
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void setMappingValue() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("MyMappedProperty");
+    
+    Object value = "Changed mapped property value";
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertEquals("Changed mapped property value", data.myMappedProperty);
+  }
+
+  @Test
+  public void setMappingValueNullValue() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("MyMappedProperty");
+    
+    Object value = null;
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertNull(data.myMappedProperty);
+  }
+
+  @Test
+  public void setMappingValueNullMapping() throws Exception {
+    AnnotationValueAccess ava = new AnnotationValueAccess();
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = null;
+    
+    Object value = null;
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertEquals("mapped property value", data.myMappedProperty);
+  }
+  
+  private EdmProperty mockProperty(String name) throws EdmException {
+    EdmProperty property = Mockito.mock(EdmProperty.class);
+    Mockito.when(property.getName()).thenReturn(name);
+    return property;
+  }
+
+  private EdmMapping mockMapping(String mimeTypeKey) throws EdmException {
+    EdmMapping mapping = Mockito.mock(EdmMapping.class);
+    Mockito.when(mapping.getMediaResourceMimeTypeKey()).thenReturn(mimeTypeKey);
+    return mapping;
+  }
+
+  @EdmEntityType
+  private class SimpleEntity {
+    @org.apache.olingo.odata2.api.annotation.edm.EdmProperty String name;
+    @org.apache.olingo.odata2.api.annotation.edm.EdmProperty String myMappedProperty;
+  }
+  
+  private class NotAnnotatedBean {
+    private String name;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/d9a7f2db/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/BeanPropertyAccessTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/BeanPropertyAccessTest.java b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/BeanPropertyAccessTest.java
new file mode 100644
index 0000000..322116a
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/BeanPropertyAccessTest.java
@@ -0,0 +1,261 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ * 
+ * Licensed 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.olingo.odata2.annotation.processor.core.datasource;
+
+import junit.framework.Assert;
+
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ *
+ */
+public class BeanPropertyAccessTest {
+
+  private final BeanPropertyAccess ava;
+  
+  public BeanPropertyAccessTest() {
+    ava = new BeanPropertyAccess();
+  }
+  
+  @Test
+  public void getPropertyType() throws ODataException {
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Class<?> type = ava.getPropertyType(data, property);
+    
+    Assert.assertEquals(String.class, type);
+  }
+
+  @Test(expected=ODataNotFoundException.class)
+  public void getPropertyTypeInvalidPropertyName() throws ODataException {
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("invalid.Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void getPropertyValue() throws ODataException {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertEquals(String.class, value.getClass());
+    Assert.assertEquals("A Name", value);
+  }
+
+  @Test
+  public void getPropertyValueBoolean() throws ODataException {
+    
+    SimpleEntity data = new SimpleEntity();
+    EdmProperty property = mockProperty("BooleanProperty", true);
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertEquals(Boolean.class, value.getClass());
+    Assert.assertEquals(true, value);
+  }
+
+  @Test
+  public void getPropertyValueNull() throws ODataException {
+    
+    SimpleEntity data = new SimpleEntity();
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void getPropertyValueNullData() throws ODataException {
+    
+    SimpleEntity data = null;
+    EdmProperty property = mockProperty("Name");
+
+    Object value = ava.getPropertyValue(data, property);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void setPropertyValue() throws ODataException {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+    
+    Object value = "Another Name";
+    ava.setPropertyValue(data, property, value);
+    
+    Assert.assertEquals("Another Name", data.name);
+  }
+
+  @Test
+  public void setPropertyValueNull() throws ODataException {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.name = "A Name";
+    EdmProperty property = mockProperty("Name");
+    
+    ava.setPropertyValue(data, property, null);
+    
+    Assert.assertNull(null, data.name);
+  }
+
+  @Test
+  public void getMappingValue() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("getMyMappedProperty");
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertEquals(String.class, value.getClass());
+    Assert.assertEquals("mapped property value", value);
+  }
+
+  
+  @Test
+  public void getMappingValueNullMapping() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "property";
+    EdmMapping mapping = null;
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void getMappingValueNullValue() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = null;
+    EdmMapping mapping = mockMapping("getMyMappedProperty");
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertNull(value);
+  }
+
+  @Test
+  public void setMappingValue() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("getMyMappedProperty");
+    
+    Object value = "Changed mapped property value";
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertEquals("Changed mapped property value", data.myMappedProperty);
+  }
+
+  @Test
+  public void setMappingValueNullValue() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = mockMapping("getMyMappedProperty");
+    
+    Object value = null;
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertNull(data.myMappedProperty);
+  }
+
+  @Test
+  public void setMappingValueNullMapping() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = "mapped property value";
+    EdmMapping mapping = null;
+    
+    Object value = null;
+    ava.setMappingValue(data, mapping, value);
+    
+    Assert.assertEquals("mapped property value", data.myMappedProperty);
+  }
+
+  @Test(expected=ODataNotFoundException.class)
+  public void invalidMethodName() throws Exception {
+    
+    SimpleEntity data = new SimpleEntity();
+    data.myMappedProperty = null;
+    EdmMapping mapping = mockMapping("MyMappedProperty");
+    
+    Object value = ava.getMappingValue(data, mapping);
+    
+    Assert.assertNull(value);
+  }
+
+  private EdmProperty mockProperty(String name) throws EdmException {
+    return mockProperty(name, false);
+  }
+  
+  private EdmProperty mockProperty(String name, boolean isBoolean) throws EdmException {
+    EdmProperty property = Mockito.mock(EdmProperty.class);
+    Mockito.when(property.getName()).thenReturn(name);
+    Mockito.when(property.isSimple()).thenReturn(Boolean.TRUE);
+    if(isBoolean) {
+      Mockito.when(property.getType()).thenReturn(EdmSimpleTypeKind.Boolean.getEdmSimpleTypeInstance());
+    }
+    return property;
+  }
+
+  private EdmMapping mockMapping(String mimeTypeKey) throws EdmException {
+    EdmMapping mapping = Mockito.mock(EdmMapping.class);
+    Mockito.when(mapping.getMimeType()).thenReturn(mimeTypeKey);
+    return mapping;
+  }
+
+  private class SimpleEntity {
+    private String name;
+    private String myMappedProperty;
+    public final String getName() {
+      return name;
+    }
+    public final void setName(String name) {
+      this.name = name;
+    }
+    public final String getMyMappedProperty() {
+      return myMappedProperty;
+    }
+    public final void setMyMappedProperty(String myMappedProperty) {
+      this.myMappedProperty = myMappedProperty;
+    }
+    public final boolean isBooleanProperty() {
+      return true;
+    }
+  }
+}