use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.
the class MaterialRepositoryIntegrationTest method findModificationsSince_shouldCacheResults.
@Test
public void findModificationsSince_shouldCacheResults() {
SvnMaterial material = MaterialsMother.svnMaterial();
MaterialRevision zero = saveOneScmModification(material, "user1", "file1");
MaterialRevision first = saveOneScmModification(material, "user1", "file1");
MaterialRevision second = saveOneScmModification(material, "user2", "file2");
MaterialRevision third = saveOneScmModification(material, "user2", "file2");
repo.findModificationsSince(material, first);
HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
repo.setHibernateTemplate(mockTemplate);
List<Modification> modifications = repo.findModificationsSince(material, first);
assertThat(modifications.size(), is(2));
assertEquals(third.getLatestModification(), modifications.get(0));
assertEquals(second.getLatestModification(), modifications.get(1));
verifyNoMoreInteractions(mockTemplate);
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project gocd by gocd.
the class MaterialRepositoryIntegrationTest method findOrCreateFrom_shouldEnsureOnlyOneThreadCanCreateAtATime.
@Test
public void findOrCreateFrom_shouldEnsureOnlyOneThreadCanCreateAtATime() throws Exception {
final Material svn = MaterialsMother.svnMaterial("url", null, "username", "password", false, null);
HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
repo = new MaterialRepository(repo.getSessionFactory(), goCache, 200, transactionSynchronizationManager, materialConfigConverter, materialExpansionService, databaseStrategy) {
@Override
public MaterialInstance findMaterialInstance(Material material) {
MaterialInstance result = super.findMaterialInstance(material);
// force multiple threads to try to create the material
TestUtils.sleepQuietly(20);
return result;
}
@Override
public void saveOrUpdate(MaterialInstance material) {
material.setId(10);
super.saveOrUpdate(material);
}
};
repo.setHibernateTemplate(mockTemplate);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
public void run() {
repo.findOrCreateFrom(svn);
}
}, "thread-" + i);
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
verify(mockTemplate, times(1)).saveOrUpdate(Mockito.<MaterialInstance>any());
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project opennms by OpenNMS.
the class AbstractDaoRestServiceWithDTO method getPropertyValues.
@GET
@Path("properties/{propertyId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getPropertyValues(@PathParam("propertyId") final String propertyId, @QueryParam("q") final String query, @QueryParam("limit") final Integer limit) {
Set<SearchProperty> props = getQueryProperties();
// Find the property with the matching ID
Optional<SearchProperty> prop = props.stream().filter(p -> p.getId().equals(propertyId)).findAny();
if (prop.isPresent()) {
SearchProperty property = prop.get();
if (property.values != null && property.values.size() > 0) {
final Set<String> validValues;
if (query != null && query.length() > 0) {
validValues = property.values.keySet().stream().filter(v -> v.contains(query)).collect(Collectors.toSet());
} else {
validValues = property.values.keySet();
}
switch(property.type) {
case FLOAT:
return Response.ok(new FloatCollection(validValues.stream().map(Float::parseFloat).collect(Collectors.toList()))).build();
case INTEGER:
return Response.ok(new IntegerCollection(validValues.stream().map(Integer::parseInt).collect(Collectors.toList()))).build();
case LONG:
return Response.ok(new LongCollection(validValues.stream().map(Long::parseLong).collect(Collectors.toList()))).build();
case IP_ADDRESS:
case STRING:
return Response.ok(new StringCollection(validValues)).build();
case TIMESTAMP:
return Response.ok(new DateCollection(validValues.stream().map(v -> {
try {
return ISO8601DateEditor.stringToDate(v);
} catch (IllegalArgumentException | UnsupportedOperationException e) {
LOG.error("Invalid date in value list: " + v, e);
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList()))).build();
default:
return Response.noContent().build();
}
}
switch(property.type) {
case FLOAT:
List<Float> floats = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Float>(property, query, limit));
return Response.ok(new FloatCollection(floats)).build();
case INTEGER:
List<Integer> ints = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Integer>(property, query, limit));
return Response.ok(new IntegerCollection(ints)).build();
case LONG:
List<Long> longs = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Long>(property, query, limit));
return Response.ok(new LongCollection(longs)).build();
case IP_ADDRESS:
List<InetAddress> addresses = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<InetAddress>(property, query, limit));
return Response.ok(new StringCollection(addresses.stream().map(InetAddressUtils::str).collect(Collectors.toList()))).build();
case STRING:
List<String> strings = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<String>(property, query, limit));
return Response.ok(new StringCollection(strings)).build();
case TIMESTAMP:
List<Date> dates = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Date>(property, query, limit));
return Response.ok(new DateCollection(dates)).build();
default:
return Response.noContent().build();
}
} else {
// 404
return Response.status(Status.NOT_FOUND).build();
}
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project simple-java by angryziber.
the class HibernatePhotoSpotRepositoryIntegrationTest method setUp.
@Before
public void setUp() throws Exception {
dataSource = new DriverManagerDataSource("jdbc:h2:mem:hibernate;DB_CLOSE_DELAY=-1", "sa", "sa");
System.setProperty("hibernate.dialect", H2Dialect.class.getName());
System.setProperty("hibernate.hbm2ddl.auto", "create-drop");
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setAnnotatedClasses(new Class[] { PhotoSpot.class });
sessionFactory.afterPropertiesSet();
repo.hibernate = new HibernateTemplate(sessionFactory.getObject());
}
use of org.springframework.orm.hibernate3.HibernateTemplate in project opennms by OpenNMS.
the class AnnotationIT method assertLoadAll.
private <T> void assertLoadAll(Class<T> annotatedClass, Checker<T> checker) {
HibernateTemplate template = new HibernateTemplate(m_sessionFactory);
Collection<T> results = template.loadAll(annotatedClass);
assertNotNull(results);
checker.checkCollection(results);
for (T t : results) {
checker.check(t);
}
}
Aggregations