List of usage examples for org.apache.ibatis.session SqlSession selectOne
<T> T selectOne(String statement, Object parameter);
From source file:io.starter.datamodel.ContentData.java
License:Open Source License
/** * Get a Content object from this request * /*from ww w. ja v a 2 s .c o m*/ * @param servletRequest * @param id * @return * @throws IOException * @throws ServletException */ static Content getContentObject(HttpServletRequest servletRequest, int id) throws IOException, ServletException { // get cached Content content = getCachedContent(id, servletRequest); if (content == null) { Logger.debug("Content Object Cache Miss for content id: " + id); // apply some filters SqlSession session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION); content = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id); // add to cache addContentToCaches(id, content, servletRequest); } else { Logger.debug("Content Object Cache Hit for content id: " + id); } return content; }
From source file:io.starter.TestContent.java
License:Open Source License
@Test public void testGetContentRatingCount() throws Exception { sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); Content content = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", 379); assertEquals((Integer) 1, content.getRatingCount()); assertEquals((Integer) 4, content.getCommentCount()); session.close();/*from w w w. j a v a2 s . c o m*/ }
From source file:io.starter.TestContent.java
License:Open Source License
@Test public void testGetContent() throws Exception { sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); Content content = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", 2); assertEquals("Who's There?", content.getDescription()); session.close();/*from w w w.j ava 2s. c o m*/ }
From source file:io.starter.TestContent.java
License:Open Source License
/** * test inserting some data/*from ww w. j a v a 2s . c o m*/ * */ @Test public void testInsertAndMarkContentTrashed() { Content content = new Content(); content.setUserId(42); content.setAuthor(42); content.setAuthorDevice(1); content.setCopyright("Copyright 2013. All rights reserved"); content.setDescription("CheezBurger Cat"); content.setLicense("CC"); content.setMimeType("text/plain"); content.setUrl("http://www.hazcheezburger.com"); content.setFlag(FLAG_STAR); content.setPostDate(new java.util.Date(System.currentTimeMillis())); // ContentMapper.insert(content); SqlSession session = sqlSessionFactory.openSession(true); session.insert("io.starter.dao.ContentMapper.insert", content); Integer id = content.getId(); if (id == null) fail("Content insertion returns null ID"); try { Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id); User u = ccx.getUser(); if (u == null) fail("User is null for content object inserted with user_id = 1. Content ID: " + id); String s = ccx.getDescription(); if (s == null) fail("Failed to insert Content ID: " + id); } catch (Exception x) { fail("Exception fetching content from new Content ID: " + id); } // test deletion of that new content // content.setFlag(FLAG_DELETED); session = sqlSessionFactory.openSession(true); session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", content); try { Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id); if (ccd.getFlag() != FLAG_DELETED) fail("Failed to mark deleted newly inserted Content ID: " + id); } catch (Exception x) { // good! } session.close(); }
From source file:io.starter.TestContent.java
License:Open Source License
/** * test inserting some data//from w w w . j a va2 s . co m * */ @Test public void testInsertAndDeleteContent() { Content content = new Content(); content.setAuthor(1); content.setAuthorDevice(1); content.setCopyright("Copyright 2013. All rights reserved"); content.setDescription("CheezBurger Cat"); content.setLicense("CC"); content.setFlag(FLAG_STAR); content.setMimeType("text/plain"); content.setUrl("http://www.hazcheezburger.com"); content.setPostDate(new java.util.Date(System.currentTimeMillis())); sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); session.insert("io.starter.dao.ContentMapper.insert", content); Integer id = content.getId(); if (id == null) fail("Content insertion returns null ID"); try { Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id); String s = ccx.getDescription(); if (s == null) fail("Failed to insert Content ID: " + id); } catch (Exception x) { fail("Exception fetching content from new Content ID: " + id); } // test deletion of that new content Content cxdel = new Content(); cxdel.setId(id); session = sqlSessionFactory.openSession(true); session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", cxdel); session.close(); try { Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id); String s = ccd.getDescription(); if (s != null) fail("Failed to delete inserted Content ID: " + id); } catch (Exception x) { // good! } }
From source file:io.starter.TestContent.java
License:Open Source License
/** * Ok, ibatis gets more cool here, note the ratings, we are dynamically * setting up objects that subquery on their own. * /* w w w . jav a2 s. c o m*/ * @throws Exception */ @Test @Ignore // we don't select ratings objects anymore... revisit someday public void testGetContentObjectWithRatings() throws Exception { sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); Content content = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", 1); assertEquals("Knock Knock", content.getDescription()); ArrayList<ContentRating> ratings = content.getRatings(); ContentRating rating = ratings.get(1); assertEquals("Not Very Good!", rating.getReview()); User user = content.getUser(); assertEquals("spaceghost", user.getUsername()); session.close(); }
From source file:io.starter.TestContent.java
License:Open Source License
/** * test cpntent ownership ratings/* ww w .ja v a2 s . co m*/ * * @throws Exception */ @Test public void testOwnershipPermissions() throws Exception { Content content = new Content(); content.setAuthor(42); content.setUserId(42); content.setAuthorDevice(1); content.setCopyright("Copyright 2014. All rights reserved"); content.setDescription("TEST Starter Content"); content.setLicense("CC"); content.setFlag(FLAG_STAR); content.setMimeType("text/html"); content.setUrl("http://$$PROJECT_DOMAIN$$"); content.setPostDate(new java.util.Date(System.currentTimeMillis())); sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); session.insert("io.starter.dao.ContentMapper.insert", content); session.commit(); Integer id = content.getId(); assertTrue(id != null); Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id); User user = ccx.getUser(); assertTrue(ccx.getUserId() == 42); // assert that contact info is stripped from this assertEquals(null, user.getEmail()); // and that we got the right user assertEquals("test", user.getUsername()); // 1. Build the Subject instance for the test to run: Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject(); // 2. Bind the subject to the current thread: setSubject(subjectUnderTest); // see /login.jsp for these form fields String username = "test"; String password = API_CRYPT_KEY; // create a UsernamePasswordToken using the // username and password provided by the user UsernamePasswordToken token = new UsernamePasswordToken(username, password); // get the user (aka subject) associated with this request. Subject subject = SecurityUtils.getSubject(); try { subject.checkPermission("thing:action"); fail("User INCORRECTLY has permission thing:action"); } catch (AuthorizationException x) { // GOOD! } subject.login(token); user.setSubject(subject); ContentData.setContentOwnerPermissions(content, user, session); WildcardPermission owner = new WildcardPermission(SystemConstants.SECURITY_TARGET_TYPE_CONTENT + ":" + SystemConstants.SECURITY_ACL_OWNER + ":" + content.getId()); assert (user.checkAccess(owner)); // test deletion of that new content session = sqlSessionFactory.openSession(true); session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", ccx); try { Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id); String s1 = ccd.getDescription(); if (s1 != null) fail("Failed to delete inserted Content ID: " + id); } catch (Exception x) { // good! } session.close(); }
From source file:io.starter.TestContent.java
License:Open Source License
/** * test cpntent ratings!//from w ww .j a v a 2 s .co m * * @throws Exception */ @Test @Ignore // we don't select ratings objects anymore... revisit someday public void testRatings() throws Exception { Content content = new Content(); content.setAuthor(42); content.setUserId(42); content.setAuthorDevice(1); content.setCopyright("Copyright 2013. All rights reserved"); content.setDescription("TEST Ostrich Pillow"); content.setLicense("CC"); content.setFlag(FLAG_STAR); content.setMimeType("text/html"); content.setUrl("http://www.ostrichpillow.com"); content.setPostDate(new java.util.Date(System.currentTimeMillis())); sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); session.insert("io.starter.dao.ContentMapper.insert", content); Integer id = content.getId(); assertTrue(id != null); // rate the bastard ContentRating rx = new ContentRating(); rx.setContentId(id); rx.setFlag(0); rx.setRaterId(1); rx.setReview("that kinda sucked"); rx.setType(1); rx.setValue((long) 3); session.insert("io.starter.dao.ContentRatingMapper.insert", rx); session.commit(); Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id); String s = ccx.getDescription(); if (s == null) fail("Failed to insert Content ID: " + id); ArrayList<ContentRating> ratings = ccx.getRatings(); ContentRating rating = ratings.get(0); assertEquals("that kinda sucked", rating.getReview()); User user = ccx.getUser(); assertTrue(ccx.getUserId() == 1); assertEquals("john@$$PROJECT_DOMAIN$$", user.getEmail()); // test deletion of that new content session = sqlSessionFactory.openSession(true); session.delete("io.starter.dao.ContentRatingMapper.deleteByPrimaryKey", rx); session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", ccx); session.close(); try { Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id); String s1 = ccd.getDescription(); if (s1 != null) fail("Failed to delete inserted Content ID: " + id); } catch (Exception x) { // good! } session.close(); }
From source file:io.starter.TestContent.java
License:Open Source License
/** * test cpntent ratings!/*from ww w . j a v a 2 s. co m*/ * * @throws Exception */ @Test @Ignore // we don't select ratings objects anymore... revisit someday public void testInsertRatingThroughContent() throws Exception { sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory(); SqlSession session = sqlSessionFactory.openSession(true); Content content = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", 1); assertEquals("Knock Knock", content.getDescription()); ArrayList<ContentRating> ratings = content.getRatings(); ContentRating rating = ratings.get(1); assertEquals("Not Very Good!", rating.getReview()); int ratingCount = ratings.size(); // rate the bastard ContentRating rx = new ContentRating(); rx.setContentId(content.getId()); rx.setFlag(0); rx.setRaterId(1); rx.setReview("that kinda sucked"); rx.setType(1); rx.setValue((long) 3); // here is the new call content.insertContentRating(rx); session = sqlSessionFactory.openSession(true); Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", 1); assertTrue(ccx.getRatings().size() == (ratingCount + 1)); assertTrue(ccx.getUserId() == 1); User user = ccx.getUser(); assertEquals("spaceghost", user.getUsername()); }
From source file:mbg.test.mb3.miscellaneous.MiscellaneousTest.java
License:Apache License
@Test public void testAnotherAwfulTableInsert() { SqlSession sqlSession = sqlSessionFactory.openSession(); try {//from w ww.ja v a2 s. c o m Anotherawfultable record = new Anotherawfultable(); record.setId(5); record.setSelect("select"); record.setInsert("insert"); sqlSession.insert("mbg.test.mb3.generated.miscellaneous.xml.AnotherawfultableMapper.insert", record); Anotherawfultable key = new Anotherawfultable(); key.setId(5); Anotherawfultable returnedRecord = (Anotherawfultable) sqlSession.selectOne( "mbg.test.mb3.generated.miscellaneous.xml.AnotherawfultableMapper.selectByPrimaryKey", key); assertEquals(record.getId(), returnedRecord.getId()); assertEquals(record.getSelect(), returnedRecord.getSelect()); assertEquals(record.getInsert(), returnedRecord.getInsert()); assertEquals(record.getUpdate(), returnedRecord.getUpdate()); assertEquals(record.getDelete(), returnedRecord.getDelete()); } finally { sqlSession.close(); } }