diff --git a/src/main/java/com/example/cosmos_serversb/models/Groups.java b/src/main/java/com/example/cosmos_serversb/models/Groups.java index 4912efd..f779544 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Groups.java +++ b/src/main/java/com/example/cosmos_serversb/models/Groups.java @@ -4,13 +4,44 @@ import javax.inject.Singleton; import com.example.cosmos_serversb.entities.*; - +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @Singleton public class Groups { private static Groups theInstance = null; private static ArrayList groups = new ArrayList<>(); + private static SessionFactory sessionFactory; + + public static void setUp() { + // A SessionFactory is set up once for an application! + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .configure() // configures settings from hibernate.cfg.xml + .build(); + try { + sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); + } + catch (Exception e) { + // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory + // so destroy it manually. + StandardServiceRegistryBuilder.destroy( registry ); + } + } + + public static void tearDown() { + try { + if ( sessionFactory != null ) { + sessionFactory.close(); + } + } catch (Exception e) { + System.out.println("Exception!"); + } + } + private Groups(){ } @@ -29,18 +60,29 @@ public static Group createGroup(String name, String uId){ //先にGidとuriの発行を行う Group testGroup = new Group("123456789", "http://test.com", name, uId); - groups.add(testGroup); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(testGroup); + session.getTransaction().commit(); + session.close(); return testGroup; } public static Group getGroupByGid(String gId){ Group editGroup; - for (int i = 0; i < groups.size(); i++) { - editGroup = groups.get(i); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List groupResult = session.createQuery( "from Group" ).list(); + for ( Group group : (List) groupResult ) { + editGroup = group; if (editGroup.getgId().equals(gId)) { + session.getTransaction().commit(); + session.close(); return editGroup; } } + session.getTransaction().commit(); + session.close(); return null; } @@ -104,7 +146,8 @@ for (String key : TestMap.keySet()) { System.out.println(key + " " + TestMap.get(key)); } - */ + */ + return testMap; } diff --git a/src/test/java/hibernateTest/models/Groups.java b/src/test/java/hibernateTest/models/Groups.java new file mode 100644 index 0000000..3852f0c --- /dev/null +++ b/src/test/java/hibernateTest/models/Groups.java @@ -0,0 +1,169 @@ +package hibernateTest.models; + +import hibernateTest.entities.Group; +import hibernateTest.entities.Request; +import hibernateTest.entities.Event; +import com.example.cosmos_serversb.models.DeleteResult; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; + +import javax.inject.Singleton; +import java.util.HashMap; +import java.util.List; + + +@Singleton +public class Groups { + private static Groups theInstance = null; + + private static SessionFactory sessionFactory; + + public static void setUp() { + // A SessionFactory is set up once for an application! + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .configure() // configures settings from hibernate.cfg.xml + .build(); + try { + sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); + } + catch (Exception e) { + // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory + // so destroy it manually. + StandardServiceRegistryBuilder.destroy( registry ); + } + } + + public static void tearDown() { + try { + if ( sessionFactory != null ) { + sessionFactory.close(); + } + } catch (Exception e) { + System.out.println("Exception!"); + } + } + + private Groups(){ + + } + + public static Groups getInstance(){ + if(theInstance == null){ + theInstance = new Groups(); + } + return theInstance; + } + + public static HashMap getGroupsListByUid(String uId){ + return createTestHashMap(); + } + + public static Group createGroup(String name, String uId){ + //先にGidとuriの発行を行う + Group testGroup = new Group("123456789", "http://test.com", name, uId); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(testGroup); + session.getTransaction().commit(); + session.close(); + + Session output = sessionFactory.openSession(); + output.beginTransaction(); + //hibernateテスト用出力 + List groupResult = output.createQuery( "from Group" ).list(); + for ( hibernateTest.entities.Group group : (List) groupResult ) { + System.out.println( "Group (" + group.getgId() + ") : " + group.getUri() + ": " + group.getName()); + } + output.getTransaction().commit(); + output.close(); + return testGroup; + } + + public static Group getGroupByGid(String gId){ + Group editGroup; + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List groupResult = session.createQuery( "from Group" ).list(); + for ( hibernateTest.entities.Group group : (List) groupResult ) { + editGroup = group; + if (editGroup.getgId().equals(gId)) { + session.getTransaction().commit(); + session.close(); + return editGroup; + } + } + session.getTransaction().commit(); + session.close(); + return null; + } + + public static DeleteResult deleteGroup(String gId){ + Group editGroup = Groups.getInstance().getGroupByGid(gId); + if(editGroup == null){ + return DeleteResult.FAILURE; + } + //以下に削除処理を行う。 + return DeleteResult.SUCCESS; + } + + public static HashMap getMembersListByGid(String gId){ + return createTestHashMap(); + } + + public static HashMap addMember(String gId, String uId){ + return createTestHashMap(); + } + + public static HashMap deleteMember(String gId, String uId){ + return createTestHashMap(); + } + + public static HashMap getRequestsListByGid(String gId){ + return createTestHashMap(); + } + + public static Request addRequests(String gId, String uId, String product, String deadline, int location){ + //テスト用 + Request testRequest = new Request("123456789", "http://test.com", "1999/01/01/15:00:00", + uId, product, "1999/01/01/15:00:00", 999); + return testRequest; + } + + public static Request getRequestDetailByGidAndRid(String gId, String rId){ + return null; + } + + public static Request updateRequest(String gId, String rId, String uId, String product, String deadline, int location, boolean done){ + + //テスト用 + Request testRequest = new Request("123456789", "http://test.com", "1999/01/01/15:00:00", + uId, product, "1999/01/01/15:00:00", 999); + + return testRequest; + } + + public static DeleteResult deleteRequest(String gId, String rId){ + return DeleteResult.SUCCESS; + } + + //テスト用にHashMapの作成 + public static HashMap createTestHashMap(){ + HashMap testMap = new HashMap<>(); + testMap.put("123456780", "http://test0.com"); + testMap.put("123456781", "http://test1.com"); + testMap.put("123456782", "http://test2.com"); + /* + //Key, valueの取り出し方例(拡張for文) + for (String key : TestMap.keySet()) { + System.out.println(key + " " + TestMap.get(key)); + } + */ + return testMap; + } + + + +} diff --git a/src/test/java/hibernateTest/models/GroupsMain.java b/src/test/java/hibernateTest/models/GroupsMain.java new file mode 100644 index 0000000..6e3f5b7 --- /dev/null +++ b/src/test/java/hibernateTest/models/GroupsMain.java @@ -0,0 +1,13 @@ +package hibernateTest.models; + +import hibernateTest.models.*; + +//このメインクラスを実行すると、Hibernateのテストが行えます。 +public class GroupsMain { + public static void main(String args[]){ + Groups.setUp(); + Groups.createGroup("test1", "1234567890"); + Groups.getGroupByGid("123456789"); + Groups.tearDown(); + } +} diff --git a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java index 14e4bf6..f392f06 100644 --- a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java +++ b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java @@ -48,7 +48,7 @@ * @author Steve Ebersole */ public class NativeApiIllustrationTest { - private SessionFactory sessionFactory; + private SessionFactory sessionFactory; public void setUp() { // A SessionFactory is set up once for an application!