http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPAExpandCallBack.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPAExpandCallBack.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPAExpandCallBack.java new file mode 100644 index 0000000..19bcf77 --- /dev/null +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPAExpandCallBack.java @@ -0,0 +1,222 @@ +/******************************************************************************* + * 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.olingo.odata2.jpa.processor.core.callback; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.apache.olingo.odata2.api.ODataCallback; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.edm.EdmEntityType; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties; +import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties.ODataEntityProviderPropertiesBuilder; +import org.apache.olingo.odata2.api.ep.callback.OnWriteEntryContent; +import org.apache.olingo.odata2.api.ep.callback.OnWriteFeedContent; +import org.apache.olingo.odata2.api.ep.callback.WriteCallbackContext; +import org.apache.olingo.odata2.api.ep.callback.WriteEntryCallbackContext; +import org.apache.olingo.odata2.api.ep.callback.WriteEntryCallbackResult; +import org.apache.olingo.odata2.api.ep.callback.WriteFeedCallbackContext; +import org.apache.olingo.odata2.api.ep.callback.WriteFeedCallbackResult; +import org.apache.olingo.odata2.api.exception.ODataApplicationException; +import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode; +import org.apache.olingo.odata2.api.uri.NavigationPropertySegment; +import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException; +import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAEntityParser; + +public class JPAExpandCallBack implements OnWriteFeedContent, OnWriteEntryContent, ODataCallback { + + private URI baseUri; + private List<ArrayList<NavigationPropertySegment>> expandList; + private EdmEntitySet nextEntitySet = null; + private HashMap<String, List<EdmProperty>> edmPropertyMap = new HashMap<String, List<EdmProperty>>(); + + private JPAExpandCallBack(final URI baseUri, final List<ArrayList<NavigationPropertySegment>> expandList) { + super(); + this.baseUri = baseUri; + this.expandList = expandList; + } + + @Override + public WriteEntryCallbackResult retrieveEntryResult(final WriteEntryCallbackContext context) + throws ODataApplicationException { + WriteEntryCallbackResult result = new WriteEntryCallbackResult(); + Map<String, Object> entry = context.getEntryData(); + Map<String, Object> edmPropertyValueMap = null; + List<EdmNavigationProperty> currentNavPropertyList = null; + Map<String, ExpandSelectTreeNode> navigationLinks = null; + JPAEntityParser jpaResultParser = new JPAEntityParser(); + EdmNavigationProperty currentNavigationProperty = context.getNavigationProperty(); + try { + Object inlinedEntry = entry.get(currentNavigationProperty.getName()); + if (nextEntitySet == null) { + nextEntitySet = context.getSourceEntitySet().getRelatedEntitySet(currentNavigationProperty); + } + edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(inlinedEntry, nextEntitySet.getEntityType()); + result.setEntryData(edmPropertyValueMap); + navigationLinks = context.getCurrentExpandSelectTreeNode().getLinks(); + if (navigationLinks.size() > 0) { + currentNavPropertyList = new ArrayList<EdmNavigationProperty>(); + EdmNavigationProperty nextNavProperty = + getNextNavigationProperty(context.getSourceEntitySet().getEntityType(), context.getNavigationProperty()); + if (nextNavProperty != null) { + currentNavPropertyList.add(nextNavProperty); + } + HashMap<String, Object> navigationMap = + jpaResultParser.parse2EdmNavigationValueMap(inlinedEntry, currentNavPropertyList); + edmPropertyValueMap.putAll(navigationMap); + result.setEntryData(edmPropertyValueMap); + } + result.setInlineProperties(getInlineEntityProviderProperties(context)); + } catch (EdmException e) { + throw new ODataApplicationException(e.getMessage(), Locale.getDefault(), e); + } catch (ODataJPARuntimeException e) { + throw new ODataApplicationException(e.getMessage(), Locale.getDefault(), e); + } + + return result; + } + + private List<EdmProperty> getEdmProperties(final EdmEntitySet entitySet, final ExpandSelectTreeNode expandTreeNode) + throws ODataApplicationException { + + try { + String name = entitySet.getName(); + if (edmPropertyMap.containsKey(name)) { + return edmPropertyMap.get(name); + } + List<EdmProperty> edmProperties = new ArrayList<EdmProperty>(); + edmProperties.addAll(expandTreeNode.getProperties()); + boolean hit = false; + for (EdmProperty keyProperty : entitySet.getEntityType().getKeyProperties()) { + hit = false; + for (EdmProperty property : edmProperties) { + if (property.getName().equals(keyProperty.getName())) { + hit = true; + break; + } + } + if (hit == false) { + edmProperties.add(keyProperty); + } + } + edmPropertyMap.put(name, edmProperties); + return edmProperties; + } catch (EdmException e) { + throw new ODataApplicationException(e.getMessage(), Locale.getDefault(), e); + } + } + + @Override + public WriteFeedCallbackResult retrieveFeedResult(final WriteFeedCallbackContext context) + throws ODataApplicationException { + WriteFeedCallbackResult result = new WriteFeedCallbackResult(); + HashMap<String, Object> inlinedEntry = (HashMap<String, Object>) context.getEntryData(); + List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>(); + JPAEntityParser jpaResultParser = new JPAEntityParser(); + List<EdmNavigationProperty> currentNavPropertyList = null; + EdmNavigationProperty currentNavigationProperty = context.getNavigationProperty(); + ExpandSelectTreeNode currentExpandTreeNode = context.getCurrentExpandSelectTreeNode(); + + try { + @SuppressWarnings({ "unchecked" }) + Collection<Object> listOfItems = (Collection<Object>) inlinedEntry.get(context.getNavigationProperty().getName()); + if (nextEntitySet == null) { + nextEntitySet = context.getSourceEntitySet().getRelatedEntitySet(currentNavigationProperty); + } + if (currentExpandTreeNode.getProperties().size() > 0) { + edmEntityList = + jpaResultParser.parse2EdmEntityList(listOfItems, getEdmProperties(nextEntitySet, + currentExpandTreeNode)); + } else { + edmEntityList = jpaResultParser.parse2EdmEntityList(listOfItems, nextEntitySet.getEntityType()); + } + result.setFeedData(edmEntityList); + + if (currentExpandTreeNode.getLinks().size() > 0) { + currentNavPropertyList = new ArrayList<EdmNavigationProperty>(); + EdmNavigationProperty nextNavProperty = + getNextNavigationProperty(context.getSourceEntitySet().getEntityType(), context.getNavigationProperty()); + if (nextNavProperty != null) { + currentNavPropertyList.add(nextNavProperty); + } + int count = 0; + for (Object object : listOfItems) { + HashMap<String, Object> navigationMap = + jpaResultParser.parse2EdmNavigationValueMap(object, currentNavPropertyList); + edmEntityList.get(count).putAll(navigationMap); + count++; + } + result.setFeedData(edmEntityList); + } + result.setInlineProperties(getInlineEntityProviderProperties(context)); + } catch (EdmException e) { + throw new ODataApplicationException(e.getMessage(), Locale.getDefault(), e); + } catch (ODataJPARuntimeException e) { + throw new ODataApplicationException(e.getMessage(), Locale.getDefault(), e); + } + return result; + } + + private EdmNavigationProperty getNextNavigationProperty(final EdmEntityType sourceEntityType, + final EdmNavigationProperty navigationProperty) throws EdmException { + for (ArrayList<NavigationPropertySegment> navPropSegments : expandList) { + int size = navPropSegments.size(); + for (int i = 0; i < size; i++) { + EdmNavigationProperty navProperty = navPropSegments.get(i).getNavigationProperty(); + if (navProperty.getFromRole().equalsIgnoreCase(sourceEntityType.getName()) + && navProperty.getName().equals(navigationProperty.getName())) { + if (i < size - 1) { + return navPropSegments.get(i + 1).getNavigationProperty(); + } + } + } + } + return null; + } + + public static <T> Map<String, ODataCallback> getCallbacks(final URI baseUri, + final ExpandSelectTreeNode expandSelectTreeNode, final List<ArrayList<NavigationPropertySegment>> expandList) + throws EdmException { + Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>(); + + for (String navigationPropertyName : expandSelectTreeNode.getLinks().keySet()) { + callbacks.put(navigationPropertyName, new JPAExpandCallBack(baseUri, expandList)); + } + + return callbacks; + + } + + private EntityProviderWriteProperties getInlineEntityProviderProperties(final WriteCallbackContext context) + throws EdmException { + ODataEntityProviderPropertiesBuilder propertiesBuilder = EntityProviderWriteProperties.serviceRoot(baseUri); + propertiesBuilder.callbacks(getCallbacks(baseUri, context.getCurrentExpandSelectTreeNode(), expandList)); + propertiesBuilder.expandSelectTree(context.getCurrentExpandSelectTreeNode()); + return propertiesBuilder.build(); + } + +}
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java new file mode 100644 index 0000000..a462696 --- /dev/null +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/callback/JPATombstoneCallBack.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * 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.olingo.odata2.jpa.processor.core.callback; + +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.ep.callback.TombstoneCallback; +import org.apache.olingo.odata2.api.ep.callback.TombstoneCallbackResult; +import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo; + +public class JPATombstoneCallBack implements TombstoneCallback { + + private static final String DELTA_TOKEN_STRING = "?!deltatoken="; + private String baseUri; + private String deltaTokenValue; + private GetEntitySetUriInfo resultsView; + + public JPATombstoneCallBack(final String baseUri, final GetEntitySetUriInfo resultsView, + final String deltaTokenValue) { + this.baseUri = baseUri; + this.deltaTokenValue = deltaTokenValue; + this.resultsView = resultsView; + } + + @Override + public TombstoneCallbackResult getTombstoneCallbackResult() { + TombstoneCallbackResult jpaTombstoneCallBackResult = new TombstoneCallbackResult(); + + jpaTombstoneCallBackResult.setDeltaLink(buildToken()); + return jpaTombstoneCallBackResult; + } + + private String buildToken() { + StringBuilder tokenBuilder = new StringBuilder(); + tokenBuilder.append(baseUri); + try { + tokenBuilder.append(resultsView.getTargetEntitySet().getName()); + } catch (EdmException e) { + // Nothing + } + tokenBuilder.append(DELTA_TOKEN_STRING); + tokenBuilder.append(deltaTokenValue); + return tokenBuilder.toString(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java index 537a4f8..9f35c3a 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociation.java @@ -19,6 +19,7 @@ package org.apache.olingo.odata2.jpa.processor.core.model; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -94,11 +95,13 @@ public class JPAEdmAssociation extends JPAEdmBaseViewImpl implements JPAEdmAssoc if (association != null) { if (view.compare(association.getEnd1(), association.getEnd2())) { JPAEdmAssociationEndView associationEnd = associationEndMap.get(association.getName()); - if (associationEnd.getJoinColumnName() != null && associationEnd.getJoinColumnReferenceColumnName() != null - && view.getJoinColumnName() != null && view.getJoinColumnReferenceColumnName() != null) { - if (view.getJoinColumnName().equals(associationEnd.getJoinColumnName()) - && view.getJoinColumnReferenceColumnName() - .equals(associationEnd.getJoinColumnReferenceColumnName())) { + if (associationEnd.getJoinColumnNames() != null + && associationEnd.getJoinColumnReferenceColumnNames() != null + && view.getJoinColumnNames() != null && view.getJoinColumnReferenceColumnNames() != null) { + if (Arrays.equals(view.getJoinColumnNames(), associationEnd.getJoinColumnNames()) + && + Arrays.equals(view.getJoinColumnReferenceColumnNames(), associationEnd + .getJoinColumnReferenceColumnNames())) { currentAssociation = association; return association; } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java index 0fdaee0..d3af0e9 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmAssociationEnd.java @@ -19,8 +19,9 @@ package org.apache.olingo.odata2.jpa.processor.core.model; import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Array; +import java.util.List; -import javax.persistence.JoinColumn; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.OneToOne; @@ -41,8 +42,8 @@ public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAs private JPAEdmPropertyView propertyView = null; private AssociationEnd currentAssociationEnd1 = null; private AssociationEnd currentAssociationEnd2 = null; - private String columnName; - private String referencedColumnName; + private String[] columnNames; + private String[] referencedColumnNames; private String mappedBy; private String ownerPropertyName; @@ -76,8 +77,6 @@ public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAs @Override public void build() throws ODataJPAModelException { - JoinColumn joinColumn = null; - currentAssociationEnd1 = new AssociationEnd(); currentAssociationEnd2 = new AssociationEnd(); @@ -88,17 +87,17 @@ public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAs setEdmMultiplicity(propertyView.getJPAAttribute().getPersistentAttributeType()); - AnnotatedElement annotatedElement = (AnnotatedElement) propertyView.getJPAAttribute().getJavaMember(); - if (annotatedElement != null) { - joinColumn = annotatedElement.getAnnotation(JoinColumn.class); - if (joinColumn != null) { - columnName = joinColumn.name(); - referencedColumnName = joinColumn.referencedColumnName(); + List<String[]> joinColumnNames = propertyView.getJPAJoinColumns(); + if (joinColumnNames != null) { + int i = 0; + columnNames = (String[]) Array.newInstance(String.class, joinColumnNames.size()); + referencedColumnNames = (String[]) Array.newInstance(String.class, joinColumnNames.size()); + for (String[] jc : joinColumnNames) { + columnNames[i] = jc[0]; + referencedColumnNames[i++] = jc[1]; } - } ownerPropertyName = propertyView.getJPAAttribute().getName(); - } private void setEdmMultiplicity(final PersistentAttributeType type) { @@ -161,13 +160,13 @@ public class JPAEdmAssociationEnd extends JPAEdmBaseViewImpl implements JPAEdmAs } @Override - public String getJoinColumnName() { - return columnName; + public String[] getJoinColumnNames() { + return columnNames; } @Override - public String getJoinColumnReferenceColumnName() { - return referencedColumnName; + public String[] getJoinColumnReferenceColumnNames() { + return referencedColumnNames; } @Override http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java index a127c1e..eea7254 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmEntityType.java @@ -25,13 +25,17 @@ import java.util.List; import java.util.ListIterator; import java.util.Set; +import javax.persistence.EntityListeners; + import org.apache.olingo.odata2.api.edm.provider.EntityType; +import org.apache.olingo.odata2.jpa.processor.api.ODataJPATombstoneEntityListener; import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder; import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmMappingModelAccess; import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException; import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmEntityTypeView; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmKeyView; +import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmMapping; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmNavigationPropertyView; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmSchemaView; @@ -83,6 +87,7 @@ public class JPAEdmEntityType extends JPAEdmBaseViewImpl implements JPAEdmEntity private class JPAEdmEntityTypeBuilder implements JPAEdmBuilder { + @SuppressWarnings("unchecked") @Override public void build() throws ODataJPAModelException, ODataJPARuntimeException { @@ -105,7 +110,20 @@ public class JPAEdmEntityType extends JPAEdmBaseViewImpl implements JPAEdmEntity } JPAEdmNameBuilder.build(JPAEdmEntityType.this); + JPAEdmMapping jpaEdmMapping = (JPAEdmMapping) currentEdmEntityType.getMapping(); + EntityListeners entityListners = currentJPAEntityType.getJavaType().getAnnotation(EntityListeners.class); + if (entityListners != null) { + for (Class<? extends ODataJPATombstoneEntityListener> entityListner : entityListners.value()) + { + try { + jpaEdmMapping.setODataJPATombstoneEntityListener(entityListner); + break; + } catch (ClassCastException e) { + continue; + } + } + } JPAEdmPropertyView propertyView = new JPAEdmProperty(schemaView); propertyView.getBuilder().build(); http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmMappingImpl.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmMappingImpl.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmMappingImpl.java index fc9f3fd..5944191 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmMappingImpl.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmMappingImpl.java @@ -19,12 +19,14 @@ package org.apache.olingo.odata2.jpa.processor.core.model; import org.apache.olingo.odata2.api.edm.provider.Mapping; +import org.apache.olingo.odata2.jpa.processor.api.ODataJPATombstoneEntityListener; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmMapping; public class JPAEdmMappingImpl extends Mapping implements JPAEdmMapping { private String columnName = null; private Class<?> type = null; + private Class<? extends ODataJPATombstoneEntityListener> entityListener = null; @Override public void setJPAColumnName(final String name) { @@ -47,4 +49,15 @@ public class JPAEdmMappingImpl extends Mapping implements JPAEdmMapping { public Class<?> getJPAType() { return type; } + + @Override + public void setODataJPATombstoneEntityListener( + final Class<? extends ODataJPATombstoneEntityListener> entityListener) { + this.entityListener = entityListener; + } + + @Override + public Class<? extends ODataJPATombstoneEntityListener> getODataJPATombstoneEntityListener() { + return entityListener; + } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java index 8ea02f7..fd14794 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java @@ -74,6 +74,7 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements private Attribute<?, ?> currentRefAttribute; private boolean isBuildModeComplexType; private Map<String, Integer> associationCount; + private ArrayList<String[]> joinColumnNames = null; public JPAEdmProperty(final JPAEdmSchemaView view) { super(view); @@ -138,6 +139,11 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements return navigationPropertyView; } + @Override + public List<String[]> getJPAJoinColumns() { + return joinColumnNames; + } + private class JPAEdmPropertyBuilder implements JPAEdmBuilder { /* * @@ -342,6 +348,7 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements ODataJPARuntimeException { AnnotatedElement annotatedElement = (AnnotatedElement) jpaAttribute.getJavaMember(); + joinColumnNames = null; if (annotatedElement == null) { return; } @@ -349,25 +356,53 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements if (joinColumn == null) { JoinColumns joinColumns = annotatedElement.getAnnotation(JoinColumns.class); if (joinColumns != null) { - return; + for (JoinColumn jc : joinColumns.value()) { + buildForeignKey(jc, jpaAttribute); + } + } + } else { + buildForeignKey(joinColumn, jpaAttribute); + } + } + + private void buildForeignKey(final JoinColumn joinColumn, final Attribute<?, ?> jpaAttribute) + throws ODataJPAModelException, + ODataJPARuntimeException { + joinColumnNames = joinColumnNames == null ? new ArrayList<String[]>() : joinColumnNames; + String[] name = { null, null }; + name[0] = joinColumn.name().equals("") == true ? jpaAttribute.getName() : joinColumn.name(); + + EntityType<?> referencedEntityType = metaModel.entity(jpaAttribute.getJavaType()); + if (joinColumn.referencedColumnName().equals("")) { + for (Attribute<?, ?> referencedAttribute : referencedEntityType.getAttributes()) { + if (referencedAttribute.getPersistentAttributeType() == PersistentAttributeType.BASIC && + ((SingularAttribute<?, ?>) referencedAttribute).isId()) { + name[1] = referencedAttribute.getName(); + joinColumnNames.add(name); + currentRefAttribute = referencedAttribute; + break; + } } } else { - if (joinColumn.insertable() && joinColumn.updatable()) { - EntityType<?> referencedEntityType = metaModel.entity(jpaAttribute.getJavaType()); - for (Attribute<?, ?> referencedAttribute : referencedEntityType.getAttributes()) { - AnnotatedElement annotatedElement2 = (AnnotatedElement) referencedAttribute.getJavaMember(); - if (annotatedElement2 != null) { - Column referencedColumn = annotatedElement2.getAnnotation(Column.class); - if (referencedColumn != null && referencedColumn.name().equals((joinColumn.referencedColumnName()))) { - currentRefAttribute = referencedAttribute; - currentSimpleProperty = new SimpleProperty(); - properties.add(buildSimpleProperty(currentRefAttribute, currentSimpleProperty, true)); - break; - } + for (Attribute<?, ?> referencedAttribute : referencedEntityType.getAttributes()) { + AnnotatedElement annotatedElement2 = (AnnotatedElement) referencedAttribute.getJavaMember(); + if (annotatedElement2 != null) { + Column referencedColumn = annotatedElement2.getAnnotation(Column.class); + if (referencedColumn != null && referencedColumn.name().equals((joinColumn.referencedColumnName()))) { + name[1] = referencedColumn.name(); + joinColumnNames.add(name); + currentRefAttribute = referencedAttribute; + break; } } } } + + if (joinColumn.insertable() && joinColumn.updatable()) { + currentSimpleProperty = new SimpleProperty(); + properties.add(buildSimpleProperty(currentRefAttribute, currentSimpleProperty, true)); + } + } @SuppressWarnings("rawtypes") @@ -419,5 +454,4 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements } return isExcluded; } - } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java index 2a2426b..94905e9 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java @@ -18,14 +18,11 @@ ******************************************************************************/ package org.apache.olingo.odata2.jpa.processor.core.model; -import java.lang.reflect.AnnotatedElement; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.metamodel.Attribute; import org.apache.olingo.odata2.api.edm.provider.Association; @@ -43,14 +40,6 @@ import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmPropertyView; import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmReferentialConstraintRoleView; public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implements JPAEdmReferentialConstraintRoleView { - /* - * Static Buffer - */ - private static Attribute<?, ?> bufferedJPAAttribute = null; - private static ArrayList<JoinColumn> bufferedJoinColumns = new ArrayList<JoinColumn>(); - /* - * Static Buffer - */ private boolean firstBuild = true; @@ -58,7 +47,7 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen private JPAEdmReferentialConstraintRoleView.RoleType roleType; private Attribute<?, ?> jpaAttribute; - private ArrayList<String> jpaColumnNames; + private List<String[]> jpaColumnNames; private Association association; private boolean roleExists = false; @@ -75,6 +64,7 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen this.roleType = roleType; jpaAttribute = propertyView.getJPAAttribute(); + jpaColumnNames = propertyView.getJPAJoinColumns(); association = associationView.getEdmAssociation(); } @@ -141,22 +131,12 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen firstBuild = false; isConsistent = false; - extractJoinColumns(); - - if (!roleExists) { + if (jpaColumnNames == null || jpaColumnNames.isEmpty()) { + roleExists = false; return; + } else { + roleExists = true; } - - jpaColumnNames = new ArrayList<String>(); - - for (JoinColumn joinColumn : bufferedJoinColumns) { - if (roleType == RoleType.PRINCIPAL) { - jpaColumnNames.add(joinColumn.referencedColumnName()); - } else if (roleType == RoleType.DEPENDENT) { - jpaColumnNames.add(joinColumn.name()); - } - } - } private void buildRole() throws SecurityException, NoSuchFieldException { @@ -183,9 +163,12 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen List<PropertyRef> propertyRefs = new ArrayList<PropertyRef>(); if (edmEntityType != null) { - for (String columnName : jpaColumnNames) { + for (String[] columnName : jpaColumnNames) { for (Property property : edmEntityType.getProperties()) { - if (columnName.equals(((JPAEdmMapping) property.getMapping()).getJPAColumnName())) { + if (columnName[0].equals(((JPAEdmMapping) property.getMapping()).getJPAColumnName()) || + columnName[0].equals(property.getName()) || + columnName[1].equals(((JPAEdmMapping) property.getMapping()).getJPAColumnName()) || + columnName[1].equals(property.getName())) { PropertyRef propertyRef = new PropertyRef(); propertyRef.setName(property.getName()); propertyRefs.add(propertyRef); @@ -213,46 +196,5 @@ public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl implemen } } - - private void extractJoinColumns() { - /* - * Check against Static Buffer whether the join column was already - * extracted. - */ - if (!jpaAttribute.equals(bufferedJPAAttribute)) { - bufferedJPAAttribute = jpaAttribute; - bufferedJoinColumns.clear(); - } else if (bufferedJoinColumns.isEmpty()) { - roleExists = false; - return; - } else { - roleExists = true; - return; - } - - AnnotatedElement annotatedElement = (AnnotatedElement) jpaAttribute.getJavaMember(); - - if (annotatedElement == null) { - return; - } - - JoinColumn joinColumn = annotatedElement.getAnnotation(JoinColumn.class); - if (joinColumn == null) { - JoinColumns joinColumns = annotatedElement.getAnnotation(JoinColumns.class); - - if (joinColumns != null) { - JoinColumn[] joinColumnArray = joinColumns.value(); - - for (JoinColumn element : joinColumnArray) { - bufferedJoinColumns.add(element); - } - } else { - return; - } - } else { - bufferedJoinColumns.add(joinColumn); - } - roleExists = true; - } } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderTest.java index 1345260..049b365 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderTest.java @@ -386,7 +386,7 @@ public class ODataJPAResponseBuilderTest extends JPAEdmTestModelView { EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class); EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class); EasyMock.expect(edmMapping.getInternalName()).andStubReturn("soId"); - EasyMock.expect(edmMapping.getMimeType()).andReturn(null); + EasyMock.expect(edmMapping.getMediaResourceMimeTypeKey()).andReturn(null); EasyMock.replay(edmMapping); try { EasyMock.expect(edmProperty.getName()).andStubReturn("ID"); http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserForStaticMethodTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserForStaticMethodTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserForStaticMethodTest.java new file mode 100644 index 0000000..e7d4c5f --- /dev/null +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserForStaticMethodTest.java @@ -0,0 +1,330 @@ +/******************************************************************************* + * 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.olingo.odata2.jpa.processor.core.access.data; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.SQLException; + +import javax.sql.rowset.serial.SerialBlob; +import javax.sql.rowset.serial.SerialClob; +import javax.sql.rowset.serial.SerialException; + +import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException; +import org.apache.olingo.odata2.jpa.processor.core.common.ODataJPATestConstants; +import org.junit.Test; + +public class JPAEntityParserForStaticMethodTest { + + @Test + public void testToStringDefault() { + + Character[] input = new Character[] { 'A', 'B' }; + assertEquals("AB", JPAEntityParser.toString(input)); + + } + + @Test + public void testToStringNull() { + Character[] input = null; + assertNull(JPAEntityParser.toString(input)); + } + + @Test + public void testToStringPartialNull() { + Character[] input = new Character[] { 'A', null }; + assertEquals("A", JPAEntityParser.toString(input)); + } + + @Test + public void testToCharacterArrayDefault() { + String input = new String("AB"); + Character[] ch = JPAEntityParser.toCharacterArray(input); + + assertEquals(2, ch.length); + assertTrue(ch[0].equals('A')); + assertTrue(ch[1].equals('B')); + } + + @Test + public void testToCharacterArrayNull() { + String input = null; + Character[] ch = JPAEntityParser.toCharacterArray(input); + + assertNull(ch); + } + + @Test + public void testGetPropertyCharacter() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharacter", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertEquals("A", output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharacterNull() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharacterNull", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertNull(output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharacterArray() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharacterArray", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertEquals("AB", output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharacterArrayNull() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharacterArrayNull", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertNull(output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyChar() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getChar", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertEquals("A", output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharNull() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharNull", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertNull(output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharArray() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharArray", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertEquals("AB", output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharArrayNull() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharArrayNull", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertNull(output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetPropertyCharArrayValueNull() { + try { + Method method = JPAEntityParserForStaticMethodTest.class.getMethod("getCharArrayValueNull", (Class<?>[]) null); + String output = (String) JPAEntityParser.getPropertyValue(method, this); + assertEquals("A\u0000", output); + + } catch (NoSuchMethodException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SecurityException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetString() { + char[] expectedChar = new char[] { 'a', 'b', 'c' }; + try { + Clob clob = new SerialClob(expectedChar); + String actualString = JPAEntityParser.getString(clob); + + assertEquals(new String(expectedChar), actualString); + + } catch (SerialException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SQLException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + @Test + public void testGetBytes() { + final String fileName = "SalesOrderProcessingMappingModels.xml"; + + try { + FileInputStream fis = getFileStream(fileName); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int content = fis.read(); + while (content != -1) { + baos.write(content); + content = fis.read(); + } + Blob blob = new SerialBlob(baos.toByteArray()); + byte[] actualBytes = (byte[]) JPAEntityParser.getBytes(blob); + byte[] expectedBytes = baos.toByteArray(); + + assertEquals(expectedBytes.length, actualBytes.length); + int size = actualBytes.length; + int index = 0; + while (index < size) { + assertEquals(expectedBytes[index], actualBytes[index]); + index++; + } + + } catch (FileNotFoundException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SerialException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (SQLException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (IOException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } catch (ODataJPARuntimeException e) { + fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); + } + } + + private FileInputStream getFileStream(final String name) throws SerialException, FileNotFoundException { + final String fileName = "SalesOrderProcessingMappingModels.xml"; + FileInputStream fis; + + URL fileURL = JPAEntityParserForStaticMethodTest.class.getClassLoader().getResource(fileName); + fis = new FileInputStream(fileURL.getPath()); + + return fis; + + } + + public Character getCharacter() { + return new Character('A'); + } + + public Character getCharacterNull() { + return null; + } + + public Character[] getCharacterArray() { + return new Character[] { 'A', 'B' }; + } + + public Character[] getCharacterArrayNull() { + return null; + } + + public char getChar() { + return 'A'; + } + + public char getCharNull() { + return '\u0000'; + } + + public char[] getCharArray() { + return new char[] { 'A', 'B' }; + } + + public char[] getCharArrayNull() { + return null; + } + + public char[] getCharArrayValueNull() { + return new char[] { 'A', '\u0000' }; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserTestForStaticMethods.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserTestForStaticMethods.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserTestForStaticMethods.java deleted file mode 100644 index efb388d..0000000 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntityParserTestForStaticMethods.java +++ /dev/null @@ -1,330 +0,0 @@ -/******************************************************************************* - * 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.olingo.odata2.jpa.processor.core.access.data; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.lang.reflect.Method; -import java.net.URL; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.SQLException; - -import javax.sql.rowset.serial.SerialBlob; -import javax.sql.rowset.serial.SerialClob; -import javax.sql.rowset.serial.SerialException; - -import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException; -import org.apache.olingo.odata2.jpa.processor.core.common.ODataJPATestConstants; -import org.junit.Test; - -public class JPAEntityParserTestForStaticMethods { - - @Test - public void testToStringDefault() { - - Character[] input = new Character[] { 'A', 'B' }; - assertEquals("AB", JPAEntityParser.toString(input)); - - } - - @Test - public void testToStringNull() { - Character[] input = null; - assertNull(JPAEntityParser.toString(input)); - } - - @Test - public void testToStringPartialNull() { - Character[] input = new Character[] { 'A', null }; - assertEquals("A", JPAEntityParser.toString(input)); - } - - @Test - public void testToCharacterArrayDefault() { - String input = new String("AB"); - Character[] ch = JPAEntityParser.toCharacterArray(input); - - assertEquals(2, ch.length); - assertTrue(ch[0].equals('A')); - assertTrue(ch[1].equals('B')); - } - - @Test - public void testToCharacterArrayNull() { - String input = null; - Character[] ch = JPAEntityParser.toCharacterArray(input); - - assertNull(ch); - } - - @Test - public void testGetPropertyCharacter() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharacter", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertEquals("A", output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharacterNull() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharacterNull", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertNull(output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharacterArray() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharacterArray", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertEquals("AB", output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharacterArrayNull() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharacterArrayNull", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertNull(output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyChar() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getChar", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertEquals("A", output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharNull() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharNull", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertNull(output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharArray() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharArray", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertEquals("AB", output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharArrayNull() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharArrayNull", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertNull(output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetPropertyCharArrayValueNull() { - try { - Method method = JPAEntityParserTestForStaticMethods.class.getMethod("getCharArrayValueNull", (Class<?>[]) null); - String output = (String) JPAEntityParser.getPropertyValue(method, this); - assertEquals("A\u0000", output); - - } catch (NoSuchMethodException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SecurityException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetString() { - char[] expectedChar = new char[] { 'a', 'b', 'c' }; - try { - Clob clob = new SerialClob(expectedChar); - String actualString = JPAEntityParser.getString(clob); - - assertEquals(new String(expectedChar), actualString); - - } catch (SerialException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SQLException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - @Test - public void testGetBytes() { - final String fileName = "SalesOrderProcessingMappingModels.xml"; - - try { - FileInputStream fis = getFileStream(fileName); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int content = fis.read(); - while (content != -1) { - baos.write(content); - content = fis.read(); - } - Blob blob = new SerialBlob(baos.toByteArray()); - byte[] actualBytes = (byte[]) JPAEntityParser.getBytes(blob); - byte[] expectedBytes = baos.toByteArray(); - - assertEquals(expectedBytes.length, actualBytes.length); - int size = actualBytes.length; - int index = 0; - while (index < size) { - assertEquals(expectedBytes[index], actualBytes[index]); - index++; - } - - } catch (FileNotFoundException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SerialException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (SQLException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (IOException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } catch (ODataJPARuntimeException e) { - fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2); - } - } - - private FileInputStream getFileStream(final String name) throws SerialException, FileNotFoundException { - final String fileName = "SalesOrderProcessingMappingModels.xml"; - FileInputStream fis; - - URL fileURL = JPAEntityParserTestForStaticMethods.class.getClassLoader().getResource(fileName); - fis = new FileInputStream(fileURL.getPath()); - - return fis; - - } - - public Character getCharacter() { - return new Character('A'); - } - - public Character getCharacterNull() { - return null; - } - - public Character[] getCharacterArray() { - return new Character[] { 'A', 'B' }; - } - - public Character[] getCharacterArrayNull() { - return null; - } - - public char getChar() { - return 'A'; - } - - public char getCharNull() { - return '\u0000'; - } - - public char[] getCharArray() { - return new char[] { 'A', 'B' }; - } - - public char[] getCharArrayNull() { - return null; - } - - public char[] getCharArrayValueNull() { - return new char[] { 'A', '\u0000' }; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAExpandCallBackTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAExpandCallBackTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAExpandCallBackTest.java index 634df6e..5b0147c 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAExpandCallBackTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAExpandCallBackTest.java @@ -41,6 +41,7 @@ import org.apache.olingo.odata2.api.ep.callback.WriteFeedCallbackResult; import org.apache.olingo.odata2.api.exception.ODataApplicationException; import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode; import org.apache.olingo.odata2.api.uri.NavigationPropertySegment; +import org.apache.olingo.odata2.jpa.processor.core.callback.JPAExpandCallBack; import org.apache.olingo.odata2.jpa.processor.core.common.ODataJPATestConstants; import org.apache.olingo.odata2.jpa.processor.core.mock.data.EdmMockUtil; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderDeltaTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderDeltaTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderDeltaTest.java new file mode 100644 index 0000000..947ed54 --- /dev/null +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderDeltaTest.java @@ -0,0 +1,357 @@ +/******************************************************************************* + * 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.olingo.odata2.jpa.processor.core.access.data; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAPage.JPAPageBuilder; +import org.junit.Test; + +public class JPAPageBuilderDeltaTest { + + private static final int PAGE_SIZE = 10; + + @Test + public void testBuildDefaultDelta() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .build(); + + assertEquals(20, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + assertEquals(10, page.getPagedEntities().size()); + + List<Object> pagedEntities = page.getPagedEntities(); + + assertEquals("9", pagedEntities.get(0)); + assertEquals("18", pagedEntities.get(9)); + } + + @Test + public void testBuildWithNoSkipTokenDelta() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("0") + .build(); + + assertEquals(10, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + + List<Object> pagedEntities = page.getPagedEntities(); + assertEquals(10, pagedEntities.size()); + assertEquals("0", pagedEntities.get(0)); + assertEquals("9", pagedEntities.get(9)); + } + + @Test + public void testBuildDefaultZeroPageDelta() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(0) + .skipToken("10") + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(0, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + assertEquals(0, page.getPagedEntities().size()); + + } + + @Test + public void testBuildWithNullSkipToken() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken(null) + .build(); + + assertEquals(10, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + assertEquals("0", pagedEntities.get(0)); + assertEquals("9", pagedEntities.get(9)); + } + + @Test + public void testBuildWithInvalidSkipToken() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + try { + pageBuilder.entities(mockEntities()) + .skipToken("AB"); + } catch (NumberFormatException e) { + return; + } + fail("Exception Expected"); + } + + @Test + public void testBuildWithTop() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("20") + .top(5) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(20, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + assertEquals(5, pagedEntities.size()); + assertEquals("19", pagedEntities.get(0)); + assertEquals("23", pagedEntities.get(4)); + } + + @Test + public void testBuildWithTopZeroPage() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(0) + .skipToken("10") + .top(5) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(0, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + assertEquals(5, page.getPagedEntities().size()); + } + + @Test + public void testBuildWithSkipZeroPage() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(0) + .skipToken("10") + .skip(5) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(0, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + + assertEquals(0, page.getPagedEntities().size()); + } + + @Test + public void testBuildWithTopSkipZeroPage() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(0) + .skipToken("10") + .skip(5) + .top(5) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(0, page.getPageSize()); + assertNotNull(page.getPagedEntities()); + assertEquals(5, page.getPagedEntities().size()); + } + + @Test + public void testBuildWithTopExceeds() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(15) + .build(); + + assertEquals(20, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + assertEquals(10, pagedEntities.size()); + assertEquals("9", pagedEntities.get(0)); + assertEquals("18", pagedEntities.get(9)); + } + + @Test + public void testBuildWithTopSkipExceeds() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(5) + .skip(10) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertEquals(0, pagedEntities.size()); + + } + + @Test + public void testBuildWithTopSkipMore() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(5) + .skip(9) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals("18", pagedEntities.get(0)); + assertEquals(1, pagedEntities.size()); + } + + @Test + public void testBuildWithTopMoreSkip() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(15) + .skip(9) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals("18", pagedEntities.get(0)); + assertEquals(1, pagedEntities.size()); + } + + @Test + public void testBuildWithTopXSkipX() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(15) + .skip(15) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals(0, pagedEntities.size()); + } + + @Test + public void testBuildWithNegativeTop() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(-5) + .build(); + + assertEquals(20, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals("9", pagedEntities.get(0)); + assertEquals(10, pagedEntities.size()); + } + + @Test + public void testBuildWithNegativeTopSkipToken() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("-10") + .top(-5) + .skip(-1) + .build(); + + assertEquals(10, page.getNextPage()); + assertEquals(0, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals(10, pagedEntities.size()); + + } + + @Test + public void testBuildWithNoRecords() { + JPAPageBuilder pageBuilder = new JPAPageBuilder(); + JPAPage page = pageBuilder.entities(mockEntities()) + .pageSize(PAGE_SIZE) + .skipToken("10") + .top(1) + .skip(1) + .build(); + + assertEquals(0, page.getNextPage()); + assertEquals(10, page.getStartPage()); + assertEquals(PAGE_SIZE, page.getPageSize()); + List<Object> pagedEntities = page.getPagedEntities(); + assertNotNull(pagedEntities); + + assertEquals(1, pagedEntities.size()); + assertEquals("10", pagedEntities.get(0)); + } + + private List<Object> mockEntities() { + List<Object> entities = new ArrayList<Object>(); + for (int i = 0; i < 30; i++) { + entities.add(String.valueOf(i)); + } + return entities; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderTest.java index d775be3..0232163 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAPageBuilderTest.java @@ -52,7 +52,7 @@ public class JPAPageBuilderTest { .skipToken("10") .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -92,7 +92,7 @@ public class JPAPageBuilderTest { .skipToken("0") .build(); - assertEquals(10, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(0, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -112,7 +112,7 @@ public class JPAPageBuilderTest { .skipToken(null) .build(); - assertEquals(10, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(0, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -146,7 +146,7 @@ public class JPAPageBuilderTest { .top(5) .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -227,7 +227,7 @@ public class JPAPageBuilderTest { .top(15) .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -269,7 +269,7 @@ public class JPAPageBuilderTest { .skip(9) .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -290,7 +290,7 @@ public class JPAPageBuilderTest { .skip(9) .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -331,7 +331,7 @@ public class JPAPageBuilderTest { .top(-5) .build(); - assertEquals(20, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(10, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -352,7 +352,7 @@ public class JPAPageBuilderTest { .skip(-1) .build(); - assertEquals(10, page.getNextPage()); + assertEquals(0, page.getNextPage()); assertEquals(0, page.getStartPage()); assertEquals(PAGE_SIZE, page.getPageSize()); assertNotNull(page.getPagedEntities()); @@ -556,4 +556,5 @@ public class JPAPageBuilderTest { } }; } + } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java index 23fe91c..e74dc52 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAProcessorImplTest.java @@ -53,6 +53,7 @@ import org.apache.olingo.odata2.api.edm.EdmProperty; import org.apache.olingo.odata2.api.edm.EdmType; import org.apache.olingo.odata2.api.edm.EdmTypeKind; import org.apache.olingo.odata2.api.edm.EdmTyped; +import org.apache.olingo.odata2.api.edm.provider.Mapping; import org.apache.olingo.odata2.api.exception.ODataException; import org.apache.olingo.odata2.api.processor.ODataContext; import org.apache.olingo.odata2.api.uri.KeyPredicate; @@ -69,6 +70,7 @@ import org.apache.olingo.odata2.jpa.processor.api.access.JPAPaging; import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException; import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException; import org.apache.olingo.odata2.jpa.processor.core.common.ODataJPATestConstants; +import org.apache.olingo.odata2.jpa.processor.core.model.JPAEdmMappingImpl; import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; @@ -192,8 +194,8 @@ public class JPAProcessorImplTest { EasyMock.expect(objUriInfo.getSkipToken()).andReturn("5"); EasyMock.expect(objUriInfo.getInlineCount()).andStubReturn(getInlineCount()); EasyMock.expect(objUriInfo.getFilter()).andStubReturn(getFilter()); - // EasyMock.expect(objUriInfo.getFunctionImport()).andStubReturn(getFunctionImport()); EasyMock.expect(objUriInfo.getFunctionImport()).andStubReturn(null); + EasyMock.expect(objUriInfo.getCustomQueryOptions()).andStubReturn(null); EasyMock.replay(objUriInfo); return objUriInfo; } @@ -576,13 +578,13 @@ public class JPAProcessorImplTest { } private EdmMapping getEdmMappingMockedObj(final String propertyName) { - EdmMapping mockedEdmMapping = EasyMock.createMock(EdmMapping.class); + EdmMapping mockedEdmMapping = new JPAEdmMappingImpl(); if (propertyName.equalsIgnoreCase(SALES_ORDER)) { - EasyMock.expect(mockedEdmMapping.getInternalName()).andStubReturn(SALES_ORDER_HEADERS); + ((Mapping) mockedEdmMapping).setInternalName(SALES_ORDER_HEADERS); } else { - EasyMock.expect(mockedEdmMapping.getInternalName()).andStubReturn(propertyName); + ((Mapping) mockedEdmMapping).setInternalName(propertyName); } - EasyMock.replay(mockedEdmMapping); + return mockedEdmMapping; } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/46d83c8e/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilderTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilderTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilderTest.java index aebea64..2c3c194 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilderTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/access/model/JPAEdmNameBuilderTest.java @@ -49,7 +49,7 @@ public class JPAEdmNameBuilderTest { EasyMock.expect(propertyView.getEdmSimpleProperty()).andStubReturn(simpleProperty); EasyMock.replay(propertyView); - JPAEdmNameBuilder.build(propertyView, false, false,false); + JPAEdmNameBuilder.build(propertyView, false, false, false); assertEquals("Id", simpleProperty.getName()); } @@ -69,7 +69,7 @@ public class JPAEdmNameBuilderTest { EasyMock.expect(propertyView.getEdmSimpleProperty()).andStubReturn(simpleProperty); EasyMock.replay(propertyView); - JPAEdmNameBuilder.build(propertyView, false, true,false); + JPAEdmNameBuilder.build(propertyView, false, true, false); assertEquals("id", simpleProperty.getName()); }
