1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| //ma method Getuser
public Utilisateur getUser(String login,String pswd,String host){
Utilisateur e=null;
getEntityManager();
try{
query=ges.createQuery("SELECT u FROM Utilisateur u WHERE u.login=:login AND u.pswd=:pswd AND u.profil=:profil");
query.setParameter("login", login);
query.setParameter("pswd", pswd);
query.setParameter("profil",profile.ADMIN);
e=(Utilisateur)query.getSingleResult();
if(e==null)
throw new IllegalArgumentException("cet utilisateur n'est pas présent dans le système");
return e;
}
catch(Exception ex){
throw new RuntimeException(ex);
}
finally{
return e;
}
}
// ma methode Gestadmim
public Utilisateur getAdmin(String login,String pswd){
Utilisateur e=null;
getEntityManager();
try{
query=ges.createQuery("SELECT u FROM Utilisateur u WHERE u.login=:login AND e.pwd=:pwd AND u.profil=:profil");
query.setParameter("login", login);
query.setParameter("pwd", pswd);
query.setParameter("profil",profile.ADMIN);
e=(Utilisateur)query.getSingleResult();
if(e==null)
throw new IllegalArgumentException("cet Administrateur n'est pas présent dans le système");
return e;
}
catch(Exception ex){
throw new RuntimeException(ex);
}
finally{
return e;
}
}
//ma methode Getalluser
public List<Utilisateur> getAllUser(){
List<Utilisateur> alluser=null;
getEntityManager();
try{
query=ges.createQuery("SELECT u FROM Utilisateur u WHERE u.profil=:profil");
query.setParameter("profil",profile.USER);
alluser=(List<Utilisateur>)query.getResultList();
return alluser;
}
catch(Exception ex){
throw new RuntimeException(ex);
}
finally{
return alluser;
}
//ma methode dropcontact
public void dropcontact(Integer id){
getEntityManager();
try{
Contacts con=ges.find(Contacts.class,id);
if(con==null){
throw new IllegalArgumentException("ce contact n'existe pas");
}
else{
ges.getTransaction().begin();
ges.remove(con);
ges.getTransaction().commit();
}
}catch(Exception ex){
if(ges.getTransaction().isActive())
ges.getTransaction().rollback();
throw new RuntimeException(ex);
}
}
//method de test
@Before
public void setUp() {
instance = DB.getDB();
}
@After
public void tearDown() {
instance=null;
}
//vérifie l'existence de cet utilisateur
@Test
public void testGetUser() {
System.out.println("getUser");
String login = "toto";
String pwd = "tata";
String host = "127.0.0.1";
Utilisateur expResult = new Utilisateur();
Utilisateur result = instance.getUser(login, pwd, host);
assertEquals(expResult, result);
}
//vérifie la présence de cet administrateur
@Test
public void testGetAdmin() {
System.out.println("getAdmin");
String login = "admin";
String pwd = "admin";
Utilisateur expResult =new Utilisateur();
Utilisateur result = instance.getAdmin(login, pwd);
assertEquals(expResult, result);
}
//retourne tous les utilisateurs du système
@Test
public void testGetAllUser() {
System.out.println("getAllUser");
List expResult = null;
List result = instance.getAllUser();
System.out.println(result);
assertEquals(expResult, result);
}
//supprime le contact dont l'id est 1
@Test
public void testDropcontact() {
System.out.println("Dropcontact");
Integer IDs = 1;
instance.dropcontact(IDs);
}
//mom fichier persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="NETWORKSERVERPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>database.Contacts</class>
<class>database.InnerJoin</class>
<class>database.Messages</class>
<class>database.Service</class>
<class>database.Utilisateur</class>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/socialnetwork"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence> |
Partager