[New] Added Unit tests to test the bulk operations
This commit is contained in:
parent
86dddde196
commit
38d1cf61dd
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
*
|
||||||
|
* 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 li.strolch.persistence.impl.dao.test;
|
||||||
|
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
*/
|
||||||
|
public class XmlContainerTest extends AbstractDaoImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldStartContainer() {
|
||||||
|
try (StrolchTransaction tx = getPersistenceHandler().openTx()) {
|
||||||
|
tx.getOrderDao().queryKeySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,12 +18,22 @@ package li.strolch.persistence.impl.dao.test;
|
||||||
import static li.strolch.model.ModelGenerator.BAG_ID;
|
import static li.strolch.model.ModelGenerator.BAG_ID;
|
||||||
import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
|
import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
|
||||||
import static li.strolch.model.ModelGenerator.createOrder;
|
import static li.strolch.model.ModelGenerator.createOrder;
|
||||||
|
import static li.strolch.model.ModelGenerator.createOrders;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import li.strolch.model.Order;
|
import li.strolch.model.Order;
|
||||||
import li.strolch.model.parameter.Parameter;
|
import li.strolch.model.parameter.Parameter;
|
||||||
|
import li.strolch.persistence.api.OrderDao;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -89,4 +99,68 @@ public class XmlOrderDaoTest extends AbstractDaoImplTest {
|
||||||
assertNull("Should no read Order with id " + ID, order); //$NON-NLS-1$
|
assertNull("Should no read Order with id " + ID, order); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
@Test
|
||||||
|
public void shouldPerformBulkOperations() {
|
||||||
|
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
orders.addAll(createOrders(orders.size(), 5, "@", "My Order ", "MyType1"));
|
||||||
|
orders.addAll(createOrders(orders.size(), 5, "@", "Other Order ", "MyType2"));
|
||||||
|
orders.addAll(createOrders(orders.size(), 5, "@", "Further Order ", "MyType3"));
|
||||||
|
|
||||||
|
Comparator<Order> comparator = new Comparator<Order>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Order o1, Order o2) {
|
||||||
|
return o1.getId().compareTo(o2.getId());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Collections.sort(orders, comparator);
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
tx.getOrderDao().removeAll(tx.getOrderDao().queryAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
tx.getOrderDao().saveAll(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> expectedTypes = new HashSet<>();
|
||||||
|
expectedTypes.add("MyType1");
|
||||||
|
expectedTypes.add("MyType2");
|
||||||
|
expectedTypes.add("MyType3");
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
List<Order> allOrders = tx.getOrderDao().queryAll();
|
||||||
|
Collections.sort(allOrders, comparator);
|
||||||
|
assertEquals(orders, allOrders);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
OrderDao orderDao = tx.getOrderDao();
|
||||||
|
|
||||||
|
Set<String> types = orderDao.queryTypes();
|
||||||
|
assertEquals(expectedTypes, types);
|
||||||
|
|
||||||
|
Set<String> keySet = orderDao.queryKeySet();
|
||||||
|
assertEquals(15, keySet.size());
|
||||||
|
|
||||||
|
for (String type : types) {
|
||||||
|
Set<String> idsByType = orderDao.queryKeySet(type);
|
||||||
|
assertEquals(5, idsByType.size());
|
||||||
|
|
||||||
|
List<Order> ordersByType = orderDao.queryAll(type);
|
||||||
|
assertEquals(5, ordersByType.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
Order order = tx.getOrderDao().queryBy("MyType1", "@_1");
|
||||||
|
assertNotNull(order);
|
||||||
|
order = tx.getOrderDao().queryBy("MyType2", "@_6");
|
||||||
|
assertNotNull(order);
|
||||||
|
order = tx.getOrderDao().queryBy("MyType3", "@_11");
|
||||||
|
assertNotNull(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,22 @@ package li.strolch.persistence.impl.dao.test;
|
||||||
import static li.strolch.model.ModelGenerator.BAG_ID;
|
import static li.strolch.model.ModelGenerator.BAG_ID;
|
||||||
import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
|
import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
|
||||||
import static li.strolch.model.ModelGenerator.createResource;
|
import static li.strolch.model.ModelGenerator.createResource;
|
||||||
|
import static li.strolch.model.ModelGenerator.createResources;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import li.strolch.model.Resource;
|
import li.strolch.model.Resource;
|
||||||
import li.strolch.model.parameter.Parameter;
|
import li.strolch.model.parameter.Parameter;
|
||||||
|
import li.strolch.persistence.api.ResourceDao;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -89,4 +99,68 @@ public class XmlResourceDaoTest extends AbstractDaoImplTest {
|
||||||
assertNull("Should no read Resource with id " + ID, resource); //$NON-NLS-1$
|
assertNull("Should no read Resource with id " + ID, resource); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
@Test
|
||||||
|
public void shouldPerformBulkOperations() {
|
||||||
|
|
||||||
|
List<Resource> resources = new ArrayList<>();
|
||||||
|
resources.addAll(createResources(resources.size(), 5, "@", "My Resource ", "MyType1"));
|
||||||
|
resources.addAll(createResources(resources.size(), 5, "@", "Other Resource ", "MyType2"));
|
||||||
|
resources.addAll(createResources(resources.size(), 5, "@", "Further Resource ", "MyType3"));
|
||||||
|
|
||||||
|
Comparator<Resource> comparator = new Comparator<Resource>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Resource o1, Resource o2) {
|
||||||
|
return o1.getId().compareTo(o2.getId());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Collections.sort(resources, comparator);
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
tx.getResourceDao().removeAll(tx.getResourceDao().queryAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
tx.getResourceDao().saveAll(resources);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> expectedTypes = new HashSet<>();
|
||||||
|
expectedTypes.add("MyType1");
|
||||||
|
expectedTypes.add("MyType2");
|
||||||
|
expectedTypes.add("MyType3");
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
List<Resource> allResources = tx.getResourceDao().queryAll();
|
||||||
|
Collections.sort(allResources, comparator);
|
||||||
|
assertEquals(resources, allResources);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
ResourceDao resourceDao = tx.getResourceDao();
|
||||||
|
|
||||||
|
Set<String> types = resourceDao.queryTypes();
|
||||||
|
assertEquals(expectedTypes, types);
|
||||||
|
|
||||||
|
Set<String> keySet = resourceDao.queryKeySet();
|
||||||
|
assertEquals(15, keySet.size());
|
||||||
|
|
||||||
|
for (String type : types) {
|
||||||
|
Set<String> idsByType = resourceDao.queryKeySet(type);
|
||||||
|
assertEquals(5, idsByType.size());
|
||||||
|
|
||||||
|
List<Resource> resourcesByType = resourceDao.queryAll(type);
|
||||||
|
assertEquals(5, resourcesByType.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx()) {
|
||||||
|
Resource resource = tx.getResourceDao().queryBy("MyType1", "@_1");
|
||||||
|
assertNotNull(resource);
|
||||||
|
resource = tx.getResourceDao().queryBy("MyType2", "@_6");
|
||||||
|
assertNotNull(resource);
|
||||||
|
resource = tx.getResourceDao().queryBy("MyType3", "@_11");
|
||||||
|
assertNotNull(resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue