orderMngmntWebapp\src\orderMngr\service\Database.java
 
/* * Copyright (c) 2006-2014 Rogério Liesenfeld * This file is subject to the terms of the MIT license (see LICENSE.txt). */
/*...*/
5 
package orderMngr.service;
 
 
import java.sql.*;
import ...
 
 
/** * A static facade for database access through JDBC. * It assumes the application can use a single global DB connection. * <p/> * This class is just for the sake of demonstrating the capabilities of the available mocking APIs. * In the real world, such direct use of JDBC is not a practical approach, leading to long and complicated methods, * with excessive amounts of mocking in the corresponding unit tests. * A better approach is to use a higher-level abstraction for access to persistent state, which allows both production * and test code to be significantly smaller. */
/*...*/
19 
public final class Database
20 
{
21 
   private static Connection connection;
 
230
   private Database() {}
     
    25 
       public static synchronized Connection connection()
    26 
       {
    0 Path coverage: 0/2 A: 0 B: 0
    270
          if (connection == null) {
      28 
               try {
      290
                  connection = DriverManager.getConnection("jdbc:test:ordersDB");
        30 
                 }
        310
                 catch (SQLException e) {
          320
                      throw new RuntimeException(e);
            330
                     }
              34 
                    }
               
              360
                    return connection;
                37 
                   }
                 
                39 
                   public static void executeInsertUpdateOrDelete(String sql, Object... args)
                40 
                   {
                0 Path coverage: 0/3 A: 0 B: 0 C: 0
                410
                      try (PreparedStatement stmt = createStatement(sql, args)) {
                  420
                           stmt.executeUpdate();
                    430
                          }
                      440
                            catch (SQLException e) {
                        450
                                 throw new RuntimeException(e);
                          460
                                }
                            470
                               }
                               
                              49 
                                 private static PreparedStatement createStatement(String sql, Object... args) throws SQLException
                              50 
                                 {
                              2 Path coverage: 1/2 A: 0 B: 2
                              512
                                    PreparedStatement stmt = connection().prepareStatement(sql);
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              522
                                    int i = 1;
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                               
                              544
                                    for (Object arg : args) {
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x4
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              552
                                       stmt.setObject(i, arg);
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              562
                                       i++;
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              57 
                                    }
                               
                              592
                                    return stmt;
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              60 
                                 }
                               
                              62 
                                 public static void closeStatement(ResultSet result)
                              63 
                                 {
                              2 Path coverage: 1/2 A: 0 B: 2
                              642
                                    if (result != null) {
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              65 
                                       try {
                              662
                                          result.getStatement().close();
                              1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                              67 
                                       }
                              680
                                       catch (SQLException e) {
                                690
                                            throw new RuntimeException(e);
                                  702
                                           }
                                  1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                                  71 
                                        }
                                  722
                                     }
                                  1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                                   
                                  74 
                                     public static ResultSet executeQuery(String sql, Object... args)
                                  75 
                                     {
                                  76 
                                        try {
                                  772
                                           PreparedStatement stmt = createStatement(sql, args);
                                  1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                                  782
                                           return stmt.executeQuery();
                                  1. orderMngr.domain.order.OrderRepository_MockupsAPI_Test#findOrderByNumber: 169x2
                                  79 
                                        }
                                  800
                                        catch (SQLException e) {
                                    810
                                             throw new RuntimeException(e);
                                      82 
                                            }
                                      83 
                                         }
                                      84 
                                      }