Guest User

Untitled

a guest
May 4th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.71 KB | None | 0 0
  1. <hibernate-configuration>
  2. <session-factory>
  3. <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  4. <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  5. <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/animal-clinic</property>
  6. <property name="hibernate.connection.username">postgres</property>
  7. <property name="hibernate.connection.password">48iobrhqd5</property>
  8.  
  9. <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
  10. <property name="hibernate.c3p0.min_size">7</property>
  11. <property name="hibernate.c3p0.max_size">53</property>
  12. <property name="hibernate.c3p0.timeout">100</property>
  13. <property name="hibernate.c3p0.max_statements">50</property>
  14. <property name="hibernate.c3p0.idle_test_period">1000</property>
  15. <property name="hibernate.c3p0.validate">true</property>
  16.  
  17. <!-- List of XML mapping files -->
  18. <mapping resource="ru/lesson/models/users/Role.hbm.xml"/>
  19. <mapping resource="ru/lesson/models/users/User.hbm.xml"/>
  20. <mapping resource="ru/lesson/models/users/Message.hbm.xml"/>
  21. </session-factory>
  22. </hibernate-configuration>
  23.  
  24. <hibernate-mapping>
  25. <class name="ru.lesson.models.users.User" table="users">
  26. <meta attribute="class-description">
  27. This class contains the user detail.
  28. </meta>
  29. <id name="id" type="int" column="uid">
  30. <generator class="identity"/>
  31. </id>
  32. <property name="login" column="login" type="string"/>
  33. <property name="email" column="email" type="string"/>
  34. <property name="password" column="password" type="string"/>
  35. <property name="name" column="name" type="string"/>
  36. <property name="surname" column="surname" type="string"/>
  37. <property name="fullname" column="fullname" type="string"/>
  38. <property name="sex" column="sex" type="string"/>
  39. <property name="phone_number" column="phone_number" type="string"/>
  40. <property name="city" column="city" type="string"/>
  41. <many-to-one name="role" lazy="false" column="role_id" class="ru.lesson.models.users.Role" cascade="save-update"/>
  42. <bag name="messages" table="messages" lazy="false" inverse="true" cascade="all">
  43. <key column="user_id"/>
  44. <one-to-many class="ru.lesson.models.users.Message"/>
  45. </bag>
  46. </class>
  47. </hibernate-mapping>
  48.  
  49. <hibernate-mapping>
  50. <class name="ru.lesson.models.users.Message" table="messages">
  51. <meta attribute="class-description">
  52. This class contains the message of user.
  53. </meta>
  54. <id name="id" type="int" column="uid">
  55. <generator class="identity"/>
  56. </id>
  57. <many-to-one name="user" column="user_id" class="ru.lesson.models.users.User" cascade="all" insert="false" update="false"/>
  58. <many-to-one name="sender" column="sender_id" class="ru.lesson.models.users.User" cascade="all" insert="false" update="false"/>
  59. <property name="text" column="text" type="string"/>
  60. </class>
  61. </hibernate-mapping>
  62.  
  63. <hibernate-mapping>
  64. <class name="ru.lesson.models.users.Role" table="roles">
  65. <meta attribute="class-description">
  66. This class contains the role detail.
  67. </meta>
  68. <id name="id" type="int" column="uid">
  69. <generator class="identity"/>
  70. </id>
  71. <property name="name" column="name" type="string"/>
  72. </class>
  73. </hibernate-mapping>
  74.  
  75. package ru.lesson.models.users;
  76.  
  77. import java.util.*;
  78.  
  79. public class User extends Base{
  80. private String login;
  81. private String email;
  82. private String password;
  83. private String name;
  84. private String surname;
  85. private String fullname;
  86. private String sex;
  87. private String phone_number;
  88. private String city;
  89. private Role role;
  90. private List<Message> messages = new ArrayList<Message>();
  91.  
  92. public User(){}
  93.  
  94. public User(int id, String login, String email, String password, String name, String surname,
  95. String sex, String phone_number, String city){
  96. this.id = id;
  97. this.login = login;
  98. this.email = email;
  99. this.password = password;
  100. this.name = name;
  101. this.surname = surname;
  102. this.sex = sex;
  103. this.phone_number = phone_number;
  104. this.city = city;
  105. this.fullname = name + " " + surname;
  106. }
  107.  
  108. public List<Message> getMessages() {
  109. return messages;
  110. }
  111.  
  112. public void setMessages(List<Message> messages) {
  113. this.messages = messages;
  114. }
  115.  
  116. public String getLogin() {
  117. return login;
  118. }
  119.  
  120. public void setLogin(String login) {
  121. this.login = login;
  122. }
  123.  
  124. public String getEmail() {
  125. return email;
  126. }
  127.  
  128. public void setEmail(String email) {
  129. this.email = email;
  130. }
  131.  
  132. public String getPassword() {
  133. return password;
  134. }
  135.  
  136. public void setPassword(String password) {
  137. this.password = password;
  138. }
  139.  
  140. public String getName() {
  141. return name;
  142. }
  143.  
  144. public void setName(String name) {
  145. this.name = name;
  146. }
  147.  
  148. public String getSurname() {
  149. return surname;
  150. }
  151.  
  152. public void setSurname(String surname) {
  153. this.surname = surname;
  154. }
  155.  
  156. public String getFullname() {
  157. return fullname;
  158. }
  159.  
  160. public void setFullname(String fullname) {
  161. this.fullname = fullname;
  162. }
  163.  
  164. public String getSex() {
  165. return sex;
  166. }
  167.  
  168. public void setSex(String sex) {
  169. this.sex = sex;
  170. }
  171.  
  172. public String getPhone_number() {
  173. return phone_number;
  174. }
  175.  
  176. public void setPhone_number(String phone_number) {
  177. this.phone_number = phone_number;
  178. }
  179.  
  180. public String getCity() {
  181. return city;
  182. }
  183.  
  184. public void setCity(String city) {
  185. this.city = city;
  186. }
  187.  
  188. public Role getRole() {
  189. return role;
  190. }
  191.  
  192. public void setRole(Role role) {
  193. this.role = role;
  194. }
  195.  
  196. @Override
  197. public String toString() {
  198. return "User{" +
  199. "login='" + login + ''' +
  200. ", email='" + email + ''' +
  201. ", password='" + password + ''' +
  202. ", name='" + name + ''' +
  203. ", surname='" + surname + ''' +
  204. ", fullname='" + fullname + ''' +
  205. ", sex='" + sex + ''' +
  206. ", phone_number='" + phone_number + ''' +
  207. ", city='" + city + ''' +
  208. ", role=" + role +
  209. ", messages=" + messages +
  210. '}';
  211. }
  212. }
  213.  
  214. package ru.lesson.models.users;
  215.  
  216. public class Message extends Base {
  217. private User user;
  218. private User sender;
  219. private String text;
  220.  
  221. public Message(){}
  222.  
  223. public Message(int id, User userToSend, User sender, String textMessage){
  224. this.user = userToSend;
  225. this.sender = sender;
  226. this.text = textMessage;
  227. }
  228.  
  229. public User getUser() {
  230. return user;
  231. }
  232.  
  233. public void setUser(User user) {
  234. this.user = user;
  235. }
  236.  
  237. public User getSender() {
  238. return sender;
  239. }
  240.  
  241. public void setSender(User sender) { this.sender = sender; }
  242.  
  243. public String getText() {
  244. return text;
  245. }
  246.  
  247. public void setText(String text) {
  248. this.text = text;
  249. }
  250. }
  251.  
  252. package ru.lesson.models.users;
  253.  
  254. public class Role extends Base {
  255. private String name;
  256.  
  257. public Role(){}
  258.  
  259. public Role(int id, String name){
  260. this.id = id;
  261. this.name = name;
  262. }
  263.  
  264. public String getName() {
  265. return name;
  266. }
  267.  
  268. public void setName(String name) {
  269. this.name = name;
  270. }
  271. }
  272.  
  273. package ru.lesson.models.users;
  274.  
  275. public abstract class Base {
  276. protected int id;
  277.  
  278. public int getId() {
  279. return id;
  280. }
  281.  
  282. public void setId(int id) {
  283. this.id = id;
  284. }
  285. }
  286.  
  287. package ru.lesson.store;
  288.  
  289. import static org.junit.Assert.assertEquals;
  290. import static org.junit.Assert.assertNotNull;
  291.  
  292. import java.util.ArrayList;
  293. import java.util.List;
  294.  
  295. import org.junit.After;
  296. import org.junit.Before;
  297. import org.junit.Test;
  298.  
  299. import org.junit.runner.RunWith;
  300. import org.springframework.beans.factory.annotation.Autowired;
  301. import org.springframework.context.ApplicationContext;
  302. import org.springframework.context.support.ClassPathXmlApplicationContext;
  303. import org.springframework.test.context.ContextConfiguration;
  304. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  305. import org.springframework.test.context.web.WebAppConfiguration;
  306. import ru.lesson.models.users.Message;
  307. import ru.lesson.models.users.Role;
  308. import ru.lesson.models.users.User;
  309.  
  310. @RunWith(SpringJUnit4ClassRunner.class)
  311. @ContextConfiguration(locations = {"classpath:spring-context.xml"})
  312. @WebAppConfiguration
  313. public class UserStorageTest {
  314. @Autowired
  315. private Storages storage;
  316. private int id;
  317. private int idSender;
  318. private int size;
  319.  
  320. @Test
  321. public void testStorage(){
  322. try {
  323. Role role = new Role();
  324. role.setName("user");
  325. User user = new User(-1, "sanyaTest", "testYawik@mail.ru", "123456", "Sanya", "Testirovwik", "Man", "095434345", "Kharkov");
  326. user.setRole(role);
  327. User sender = new User(-1, "Sender911", "testSender@mail.ru", "123456", "Sanya", "Sender", "Man", "095434345", "Praga");
  328. sender.setRole(role);
  329. idSender = storage.userStorage.add(sender);
  330. id = storage.userStorage.add(user);
  331. user = storage.userStorage.get(id);
  332. assertEquals(id, user.getId());
  333. assertNotNull(user);
  334. assertEquals("sanyaTest", user.getLogin());
  335. assertEquals("testYawik@mail.ru", user.getEmail());
  336. assertEquals("123456", user.getPassword());
  337. assertEquals("Sanya", user.getName());
  338. assertEquals("Testirovwik", user.getSurname());
  339. assertEquals("Man", user.getSex());
  340. assertEquals("095434345", user.getPhone_number());
  341. assertEquals("Kharkov", user.getCity());
  342. size = storage.userStorage.values().size();
  343.  
  344. user = new User(id, "sanyaTest1", "testYawik@mail.ru1", "1234561", "Sanya1", "Testirovwik1", "Man1", "0954343451", "Kharkov1");
  345. user.setRole(role);
  346. List<Message> messages = new ArrayList<Message>();
  347. Message message = new Message(-1,user,sender,"textTestJUNIt");
  348. messages.add(message);
  349. user.setReceivedMessage(messages);
  350. storage.userStorage.edit(user);
  351. user = storage.userStorage.get(id);
  352. assertEquals(1, storage.userStorage.get(id).getReceivedMessage().size());
  353. assertEquals(id, user.getId());
  354. assertNotNull(user);
  355. assertEquals("sanyaTest1", user.getLogin());
  356. assertEquals("testYawik@mail.ru1", user.getEmail());
  357. assertEquals("1234561", user.getPassword());
  358. assertEquals("Sanya1", user.getName());
  359. assertEquals("Testirovwik1", user.getSurname());
  360. assertEquals("Man1", user.getSex());
  361. assertEquals("0954343451", user.getPhone_number());
  362. assertEquals("Kharkov1", user.getCity());
  363.  
  364. List<User> list = storage.userStorage.searchByID(id);
  365. assertNotNull(list.get(0));
  366. assertEquals(list.get(0).getId(), user.getId());
  367.  
  368. user = storage.userStorage.findByAuth("sanyaTest1", "1234561");
  369. assertNotNull(user);
  370.  
  371. list = storage.userStorage.searchByName("Sanya1 Testirovwik1");
  372. assertNotNull(list.get(0));
  373.  
  374. //list = storage.searchByLogin(user.getLogin());
  375. //assertNotNull(list.get(0));
  376. }finally{
  377. /*storage.userStorage.delete(id);
  378. assertEquals(size-1, storage.userStorage.values().size());*/
  379. }
  380. }
  381. }
Add Comment
Please, Sign In to add comment