Browse Source

Try catch for database access.

reyez 11 years ago
parent
commit
6f81b1e4bc

+ 46 - 8
ninja-standalone/src/test/java/hello/controllers/SetupDao.java

@@ -1,4 +1,4 @@
-package hello.dao;
+package hello.controllers;
 
 
 import hello.model.Fortune;
 import hello.model.Fortune;
 
 
@@ -9,21 +9,59 @@ import javax.persistence.Query;
 
 
 import com.google.inject.Inject;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Provider;
+import com.google.inject.Singleton;
 import com.google.inject.persist.Transactional;
 import com.google.inject.persist.Transactional;
+import hello.model.World;
 
 
-public class FortuneDao {
+/**
+ * This class is just for testing.
+ * Has nothing to do with the TechEmpower test itself...
+ * @author ra
+ */
+@Singleton
+public class SetupDao {
 
 
     @Inject
     @Inject
     Provider<EntityManager> entitiyManagerProvider;
     Provider<EntityManager> entitiyManagerProvider;
 
 
     @Transactional
     @Transactional
-    public List<Fortune> getAll() {
-	EntityManager entityManager = entitiyManagerProvider.get();
-
-	Query q = entityManager.createQuery("SELECT x FROM Fortune x");
-	List<Fortune> fortunes = q.getResultList();
+    public void generateWorldsForTest() {
+	
+        for (int i = 0; i < 10000; i++) {
+            
+            World world = new World();
+            world.randomNumber = i; // not really a random number. But we can test with that...
+            entitiyManagerProvider.get().persist(world);
+            
+         }
+        
+        
+    }
+    
+    @Transactional
+    public void generateFortunesForTest() {
+	
 
 
-	return fortunes;
+        {
+            
+            Fortune fortune = new Fortune();
+            // dummy message => just to make sure utf-8 works.
+            fortune.message = "レームワークのベンチマーク";
+            entitiyManagerProvider.get().persist(fortune);
+            
+        }
+        
+        {
+ 
+            Fortune fortune = new Fortune();
+            // dummy message => just to make sure utf-8 works.
+            fortune.message = "<script>I want to be escaped</script>";
+            entitiyManagerProvider.get().persist(fortune);
+            
+        }   
+   
+        
+        
     }
     }
 
 
 }
 }

+ 13 - 3
ninja/src/main/java/hello/controllers/DatabaseAccess.java

@@ -19,9 +19,19 @@ public class DatabaseAccess implements Filter {
     @Override
     @Override
     public Result filter(FilterChain filterChain, Context context) {
     public Result filter(FilterChain filterChain, Context context) {
         
         
-        unitOfWork.begin();
-        Result result = filterChain.next(context);
-        unitOfWork.end();
+        Result result;
+                
+        try {
+            
+            unitOfWork.begin();
+          
+            result = filterChain.next(context);
+            
+        } finally {
+            
+            unitOfWork.end();
+            
+        }
         
         
         return result;
         return result;
      }
      }

+ 46 - 8
ninja/src/test/java/hello/controllers/SetupDao.java

@@ -1,4 +1,4 @@
-package hello.dao;
+package hello.controllers;
 
 
 import hello.model.Fortune;
 import hello.model.Fortune;
 
 
@@ -9,21 +9,59 @@ import javax.persistence.Query;
 
 
 import com.google.inject.Inject;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Provider;
+import com.google.inject.Singleton;
 import com.google.inject.persist.Transactional;
 import com.google.inject.persist.Transactional;
+import hello.model.World;
 
 
-public class FortuneDao {
+/**
+ * This class is just for testing.
+ * Has nothing to do with the TechEmpower test itself...
+ * @author ra
+ */
+@Singleton
+public class SetupDao {
 
 
     @Inject
     @Inject
     Provider<EntityManager> entitiyManagerProvider;
     Provider<EntityManager> entitiyManagerProvider;
 
 
     @Transactional
     @Transactional
-    public List<Fortune> getAll() {
-	EntityManager entityManager = entitiyManagerProvider.get();
-
-	Query q = entityManager.createQuery("SELECT x FROM Fortune x");
-	List<Fortune> fortunes = q.getResultList();
+    public void generateWorldsForTest() {
+	
+        for (int i = 0; i < 10000; i++) {
+            
+            World world = new World();
+            world.randomNumber = i; // not really a random number. But we can test with that...
+            entitiyManagerProvider.get().persist(world);
+            
+         }
+        
+        
+    }
+    
+    @Transactional
+    public void generateFortunesForTest() {
+	
 
 
-	return fortunes;
+        {
+            
+            Fortune fortune = new Fortune();
+            // dummy message => just to make sure utf-8 works.
+            fortune.message = "レームワークのベンチマーク";
+            entitiyManagerProvider.get().persist(fortune);
+            
+        }
+        
+        {
+ 
+            Fortune fortune = new Fortune();
+            // dummy message => just to make sure utf-8 works.
+            fortune.message = "<script>I want to be escaped</script>";
+            entitiyManagerProvider.get().persist(fortune);
+            
+        }   
+   
+        
+        
     }
     }
 
 
 }
 }