PageRenderTime 67ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/spring/spring-jpa/src/test/java/com/acme/spring/jpa/JpaTestHelper.java

https://github.com/ALRubinger/arquillian-showcase
Java | 92 lines | 33 code | 14 blank | 45 comment | 4 complexity | 733091eb9feb014e8e1be5f0cc52bb10 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2012, Red Hat Middleware LLC, and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.acme.spring.jpa;
  18. import com.acme.spring.jpa.domain.Stock;
  19. import org.hibernate.jdbc.Work;
  20. import javax.persistence.EntityManager;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.sql.Connection;
  26. import java.sql.SQLException;
  27. import java.util.List;
  28. /**
  29. * <p>A helper class for performing operations on hibernate session.</p>
  30. *
  31. * @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
  32. */
  33. public final class JpaTestHelper {
  34. /**
  35. * <p>Creates new instance of {@link JpaTestHelper} class.</p>
  36. *
  37. * <p>Private constructor prevents from instantiation outside of this class.</p>
  38. */
  39. private JpaTestHelper() {
  40. // empty constructor
  41. }
  42. /**
  43. * <p>Executes a sql script.</p>
  44. *
  45. * @param entityManager the entity manager
  46. * @param fileName the file name
  47. *
  48. * @throws java.io.IOException if any error occurs
  49. */
  50. public static void runScript(EntityManager entityManager, String fileName) throws IOException {
  51. // retrieves the resource from class path
  52. InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
  53. BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
  54. // loads the entire file
  55. StringBuilder stringBuilder = new StringBuilder();
  56. String line;
  57. while ((line = inputReader.readLine()) != null) {
  58. if (!line.startsWith("--")) {
  59. stringBuilder.append(line);
  60. }
  61. }
  62. // splits the commands by semicolon
  63. String[] commands = stringBuilder.toString().split(";");
  64. for (final String command : commands) {
  65. entityManager.createNativeQuery(command).executeUpdate();
  66. }
  67. }
  68. /**
  69. * <p>Retrieves all Stocks stored in database.</p>
  70. *
  71. * @param entityManager the entity manager to use
  72. *
  73. * @return list of stocks retrieved from database
  74. */
  75. public static List<Stock> retrieveAllStocks(EntityManager entityManager) {
  76. return entityManager.createQuery("from Stock").getResultList();
  77. }
  78. }