Last active
August 29, 2015 14:01
-
-
Save aVolpe/8c3bf74cbdaab11ea38e to your computer and use it in GitHub Desktop.
23910328 Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package py.una.volpe.test; | |
import static org.hamcrest.CoreMatchers.hasItem; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertThat; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Properties; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.FetchType; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.JoinTable; | |
import javax.persistence.ManyToMany; | |
import javax.sql.DataSource; | |
import org.hibernate.Session; | |
import org.junit.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | |
import org.springframework.orm.hibernate4.HibernateTransactionManager; | |
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.TestExecutionListeners; | |
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; | |
import org.springframework.test.context.support.AnnotationConfigContextLoader; | |
import org.springframework.test.context.transaction.TransactionConfiguration; | |
import org.springframework.test.context.transaction.TransactionalTestExecutionListener; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import org.springframework.transaction.annotation.Transactional; | |
/** | |
* | |
* @author Arturo Volpe | |
* @since 1.0 | |
* @version 1.0 May 28, 2014 | |
* | |
*/ | |
@Transactional | |
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) | |
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) | |
@TestExecutionListeners({ TransactionalTestExecutionListener.class }) | |
public class TestTest extends AbstractJUnit4SpringContextTests { | |
@Configuration | |
@EnableTransactionManagement | |
static class ContextConfiguration { | |
@Bean | |
public DataSource dataSource() { | |
DataSource ds; | |
EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder() | |
.setType(EmbeddedDatabaseType.H2); | |
ds = edb.build(); | |
return ds; | |
} | |
@Bean | |
public LocalSessionFactoryBean sessionFactory() { | |
LocalSessionFactoryBean bean = new LocalSessionFactoryBean(); | |
Class<?>[] annonClasses = getEntityClasses(); | |
bean.setAnnotatedClasses(annonClasses); | |
bean.setDataSource(this.dataSource()); | |
Properties props = new Properties(); | |
props.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | |
props.put("hibernate.hbm2ddl.auto", "create-drop"); | |
props.put("hibernate.show_sql", "true"); | |
props.put("hibernate.format_sql", "false"); | |
bean.setHibernateProperties(props); | |
return bean; | |
} | |
@Bean | |
@Autowired | |
HibernateTransactionManager transactionManager( | |
LocalSessionFactoryBean sf) { | |
return new HibernateTransactionManager(sf.getObject()); | |
} | |
public Class<?>[] getEntityClasses() { | |
return new Class<?>[] { Projet.class, Utilisateur.class }; | |
} | |
} | |
@Autowired | |
private LocalSessionFactoryBean sf; | |
@Test | |
public void test() throws Exception { | |
Session s = sf.getObject().getCurrentSession(); | |
Projet p1 = new Projet(); | |
p1.statut = "Completed"; | |
s.persist(p1); | |
s.flush(); | |
Projet p3 = new Projet(); | |
p3.statut = "aa"; | |
s.persist(p3); | |
s.flush(); | |
Utilisateur u1 = new Utilisateur(); | |
u1.name = "test"; | |
u1.projets = new ArrayList<TestTest.Projet>( | |
Collections.singletonList(p1)); | |
Utilisateur u2 = new Utilisateur(); | |
u2.name = "test2"; | |
Utilisateur u3 = new Utilisateur(); | |
u3.name = "test3"; | |
u3.projets = new ArrayList<TestTest.Projet>( | |
Collections.singletonList(p3)); | |
s.persist(u1); | |
s.persist(u2); | |
s.persist(u3); | |
s.flush(); | |
p1.utilisateurs = new ArrayList<TestTest.Utilisateur>( | |
Collections.singletonList(u1)); | |
p3.utilisateurs = new ArrayList<TestTest.Utilisateur>( | |
Collections.singletonList(u3)); | |
s.merge(p1); | |
s.merge(p3); | |
s.flush(); | |
//@formatter:off | |
String sql = "select u " + | |
"from " + Utilisateur.class.getName() + " u " | |
+ "left join u.projets p " | |
+ "where " | |
+ " 1 = 1 and " | |
+ " p.statut like 'Completed' or " | |
+ " p is null"; | |
//@formatter:on | |
List<Utilisateur> us = s.createQuery(sql).list(); | |
for (Utilisateur u : us) { | |
System.out.println(u.projets == null ? u.toString() | |
+ " dont have projects" : u.toString() + " have " | |
+ u.projets.size() + " projects"); | |
} | |
assertThat(us, hasItem(u1)); | |
assertThat(us, hasItem(u2)); | |
assertEquals(2, us.size()); | |
} | |
@Entity | |
public static class Utilisateur implements Serializable { | |
@Id | |
@Column(name = "iduser", nullable = false) | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
Integer iduser; | |
String name; | |
@ManyToMany(fetch = FetchType.LAZY) | |
@JoinTable(name = "join_membre_projet", joinColumns = { @JoinColumn(name = "iduser", referencedColumnName = "iduser", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "idprojet", referencedColumnName = "idprojet", nullable = false, updatable = false) }) | |
List<Projet> projets; | |
@Override | |
public String toString() { | |
return name; | |
} | |
} | |
@Entity | |
public static class Projet implements Serializable { | |
@Id | |
@Column(name = "idprojet", nullable = false) | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
Integer idprojet; | |
String statut; | |
@ManyToMany(mappedBy = "projets") | |
List<Utilisateur> utilisateurs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment