Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package com.javamuseum.dbunit;
  2.  
  3. import java.io.FileInputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.util.List;
  7. import java.util.Properties;
  8.  
  9. import org.dbunit.DBTestCase;
  10. import org.dbunit.database.DatabaseConnection;
  11. import org.dbunit.database.IDatabaseConnection;
  12. import org.dbunit.dataset.IDataSet;
  13. import org.dbunit.dataset.xml.FlatXmlDataSet;
  14. import org.dbunit.operation.DatabaseOperation;
  15.  
  16.  
  17. public class ExhibitsDAOTest extends DBTestCase {
  18.  
  19.     private ExhibitsDAO dao;
  20.     IDataSet loadedDataSet;
  21.    
  22.     IDatabaseConnection conn;
  23.  
  24.  
  25.     @Override
  26.     public void setUp(){
  27.         try {
  28.             //set connection info for DAO
  29.             super.setUp();
  30.             dao = new ExhibitsDAO();
  31.        
  32.         }
  33.         catch (Exception e) {}
  34.        
  35.     }
  36.  
  37.     @Override
  38.     public void tearDown(){
  39.         //dao.close();
  40.     }
  41.    
  42.     @Override
  43.     protected IDatabaseConnection getConnection() throws Exception {
  44.         System.out.println("getConnection");
  45.  
  46.         Properties properties = new Properties();
  47.         properties.load(this.getClass().getClassLoader().getResourceAsStream("db.properties"));
  48.         String driver = properties.getProperty("DRIVER");
  49.         String dburl = properties.getProperty("DBURL");
  50.         String user = properties.getProperty("USER");
  51.         String password = properties.getProperty("PASSWORD");
  52.        
  53.         Class.forName(driver);
  54.        
  55.         Connection jdbcConnection = DriverManager.getConnection(dburl, user, password);
  56.         return new DatabaseConnection(jdbcConnection);
  57.  
  58.        
  59.     }
  60.  
  61.     @Override
  62.     protected IDataSet getDataSet() throws Exception {
  63.         System.out.println("getDataSet");
  64.        
  65.         try {
  66.             System.out.println("before return");
  67.            
  68.             loadedDataSet = new FlatXmlDataSet(new FileInputStream(
  69.             "src/com/javamuseum/dbunit/exhibits.xml"));
  70.            
  71.             System.out.println("dataset" + loadedDataSet);
  72.                
  73.    
  74.         }
  75.         catch(Exception e) {
  76.             System.out.println("error in getDataset" + e.getMessage());
  77.         }
  78.        
  79.         System.out.println("after loading xml");
  80.        
  81.         return loadedDataSet;
  82.        
  83.     }
  84.  
  85.         @Override
  86.     protected DatabaseOperation getSetUpOperation() throws Exception {
  87.         //return DatabaseOperation.REFRESH;
  88.         return DatabaseOperation.CLEAN_INSERT;
  89.     }
  90.  
  91.         @Override
  92.     protected DatabaseOperation getTearDownOperation() throws Exception {
  93.         return DatabaseOperation.NONE;
  94.     }
  95.  
  96. //  public void testAllExhibitsQuery(){
  97. //      List<Exhibit> exhibits = dao.getExhibits();
  98. //      System.out.println("num..." + dao.getExhibits().size());
  99. //     
  100. //      assertTrue( exhibits.contains(new Exhibit("OKeefes", 0, true)) );
  101. //      assertTrue( exhibits.contains(new Exhibit("ElginMarbles", 825, false)) );
  102. //      System.out.println("num..." + dao.getExhibits().size());
  103. //  }
  104.    
  105.     public void testCheckLoginDataLoaded() throws Exception{
  106.         assertNotNull(loadedDataSet);
  107.         int rowCount = loadedDataSet.getTable("TABLE_EXHIBITS").getRowCount();
  108.        
  109.         System.out.println("rowCount="+rowCount);
  110.         assertEquals(2, rowCount);
  111.     }
  112.  
  113.    
  114. //  public void testPermanentExhibitsQuery(){
  115. //      List<Exhibit> exhibits = dao.getPermanentExhibits();
  116. //      assertTrue( "should contain permanent", exhibits.contains(new Exhibit("OKeefes", 0, true)) );
  117. //      assertFalse( "should not contain visiting", exhibits.contains(new Exhibit("ElginMarbles", 825, false)) );
  118. //  }
  119. // 
  120. //  public void testPermanentExhibitsQueryPerformance(){
  121. //      long start = System.nanoTime();
  122. //      testPermanentExhibitsQuery();
  123. //      long end = System.nanoTime();
  124. //      assertTrue( (end-start) < 1000*1000 ); // 1 million nanoseconds = 1ms
  125. //  }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement