Skip to content

Commit

Permalink
=Blazebit#414 - Improved and significatly extended testsuite for upda…
Browse files Browse the repository at this point in the history
…table entity views
  • Loading branch information
beikov committed Jul 5, 2017
1 parent 529fdf1 commit 05ce9ff
Show file tree
Hide file tree
Showing 148 changed files with 5,373 additions and 1,979 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class Document extends Ownable implements Serializable {
private DocumentType documentType;
private Boolean archived = false;
private Document parent;
private Person responsiblePerson;

public Document() {
}
Expand Down Expand Up @@ -341,4 +342,13 @@ public void setParent(Document parent) {
this.parent = parent;
}

@ManyToOne(fetch = FetchType.LAZY)
public Person getResponsiblePerson() {
return responsiblePerson;
}

public void setResponsiblePerson(Person responsiblePerson) {
this.responsiblePerson = responsiblePerson;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2014 - 2017 Blazebit.
*
* 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 com.blazebit.persistence.view.impl.collection;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
*
* @author Christian Beikov
* @since 1.2.0
*/
public final class ActionUtils {
private ActionUtils() {
}

public static List<Object> replaceElements(Collection<?> elements, Object oldElem, Object elem) {
List<Object> newElements = null;

int i = 0;
for (Object element : elements) {
if (element == oldElem) {
if (newElements == null) {
newElements = new ArrayList<>(elements);
}
newElements.set(i, elem);
}
i++;
}

return newElements;
}

public static Map<Object, Object> replaceElements(Map<?, ?> elements, Object oldKey, Object oldValue, Object newKey, Object newValue) {
Map<Object, Object> newElements = null;
Iterator<? extends Map.Entry<?, ?>> iter = elements.entrySet().iterator();

while (iter.hasNext()) {
Map.Entry<?, ?> entry = iter.next();
if (entry.getKey() == oldKey || entry.getValue() == oldValue) {
if (newElements == null) {
newElements = new LinkedHashMap<>(elements);
}
newElements.remove(oldKey);
newElements.put(newKey, newValue);
}
}

return newElements;
}

public static List<Map.Entry<Object, Object>> replaceEntries(Collection<? extends Map.Entry<?, ?>> elements, Object oldKey, Object oldValue, Object newKey, Object newValue) {
List<Map.Entry<Object, Object>> newElements = null;

int i = 0;
for (Map.Entry<?, ?> element : elements) {
if (element.getKey() == oldKey || element.getValue() == oldValue) {
if (newElements == null) {
newElements = new ArrayList<>((Collection<? extends Map.Entry<Object, Object>>) elements);
}
newElements.set(i, new AbstractMap.SimpleEntry<>(newKey, newValue));
}
i++;
}

return newElements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public interface CollectionAction<T extends Collection<?>> {
public void doAction(T collection, UpdateContext context, ViewToEntityMapper mapper);

public boolean containsObject(T collection, Object o);


public CollectionAction<T> replaceObject(Object oldElem, Object elem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ public void doAction(C collection, UpdateContext context, ViewToEntityMapper map
public boolean containsObject(C collection, Object o) {
return element == o;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
if (element != oldElem) {
return null;
}
return new CollectionAddAction(elem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*
Expand All @@ -29,7 +30,7 @@
*/
public class CollectionAddAllAction<C extends Collection<E>, E> implements CollectionAction<C> {

private final Collection<? extends E> elements;
private final List<? extends E> elements;

public CollectionAddAllAction(Collection<? extends E> collection) {
this.elements = new ArrayList<E>(collection);
Expand Down Expand Up @@ -60,4 +61,15 @@ public boolean containsObject(C collection, Object o) {
return false;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
List<Object> newElements = ActionUtils.replaceElements(elements, oldElem, elem);

if (newElements == null) {
return null;
}
return new CollectionAddAllAction(newElements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public boolean containsObject(C collection, Object o) {
// Trivially, a clear action contains all objects
return true;
}

@Override
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ public void doAction(C collection, UpdateContext context, ViewToEntityMapper map
public boolean containsObject(C collection, Object o) {
return o == element;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
if (element != oldElem) {
return null;
}
return new CollectionRemoveAction(elem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*
Expand All @@ -29,10 +30,10 @@
*/
public class CollectionRemoveAllAction<C extends Collection<E>, E> implements CollectionAction<C> {

private final Collection<?> elements;
private final List<Object> elements;

public CollectionRemoveAllAction(Collection<?> collection) {
this.elements = new ArrayList<Object>(collection);
this.elements = new ArrayList<>(collection);
}

@Override
Expand All @@ -57,4 +58,15 @@ public boolean containsObject(C collection, Object o) {
return false;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
List<Object> newElements = ActionUtils.replaceElements(elements, oldElem, elem);

if (newElements == null) {
return null;
}
return new CollectionRemoveAllAction(newElements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class CollectionRetainAllAction<C extends Collection<E>, E> implements CollectionAction<C> {

private final Collection<?> elements;
private final List<?> elements;

public CollectionRetainAllAction(Collection<?> collection) {
this.elements = new ArrayList<Object>(collection);
Expand Down Expand Up @@ -68,4 +68,15 @@ public boolean containsObject(C collection, Object o) {
return false;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
List<Object> newElements = ActionUtils.replaceElements(elements, oldElem, elem);

if (newElements == null) {
return null;
}
return new CollectionRetainAllAction(newElements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public boolean containsObject(C collection, Object o) {
return o == element;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
if (element != oldElem) {
return null;
}
return new ListAddAction(index, elem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class ListAddAllAction<C extends List<E>, E> implements ListAction<C> {

private final int index;
private final Collection<? extends E> elements;
private final List<? extends E> elements;

public ListAddAllAction(int index, Collection<? extends E> collection) {
this.index = index;
Expand Down Expand Up @@ -64,4 +64,15 @@ public boolean containsObject(C collection, Object o) {
return false;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
List<Object> newElements = ActionUtils.replaceElements(elements, oldElem, elem);

if (newElements == null) {
return null;
}
return new ListAddAllAction(index, newElements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public boolean containsObject(C collection, Object o) {
return collection.size() > index && collection.get(index) == o;
}

@Override
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public boolean containsObject(C collection, Object o) {
return element == o;
}

@Override
@SuppressWarnings("unchecked")
public CollectionAction<C> replaceObject(Object oldElem, Object elem) {
if (element != oldElem) {
return null;
}
return new ListSetAction(index, elem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
public interface MapAction<T extends Map<?, ?>> {

public void doAction(T map, UpdateContext context, MapViewToEntityMapper mapper);


public MapAction<T> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper)
map.clear();
}

@Override
public MapAction<C> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper)
map.put(key, value);
}

@Override
@SuppressWarnings("unchecked")
public MapAction<C> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue) {
if (oldKey == key || oldValue == value) {
return new MapPutAction(newKey, newValue);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -61,4 +62,15 @@ public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper)
}
}

@Override
@SuppressWarnings("unchecked")
public MapAction<C> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue) {
Map<Object, Object> newElements = ActionUtils.replaceElements(elements, oldKey, oldValue, newKey, newValue);

if (newElements == null) {
return null;
}
return new MapPutAllAction(newElements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper)
}
}

@Override
@SuppressWarnings("unchecked")
public MapAction<C> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue) {
if (oldKey == key) {
return new MapRemoveAction(newKey);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ public void doAction(C map, UpdateContext context, MapViewToEntityMapper mapper)
}
}

@Override
@SuppressWarnings("unchecked")
public MapAction<C> replaceObject(Object oldKey, Object oldValue, Object newKey, Object newValue) {
Collection<Map.Entry<Object, Object>> newElements = ActionUtils.replaceEntries(elements, oldKey, oldValue, newKey, newValue);

if (newElements == null) {
return null;
}
return new MapRemoveAllEntriesAction(newElements);
}

}
Loading

0 comments on commit 05ce9ff

Please sign in to comment.