PageRenderTime 46ms CodeModel.GetById 29ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/clust/src/test/java/org/jboss/as/test/clustering/single/jdbcstore/TransactionalInfinispanManagedBean.java

#
Java | 174 lines | 135 code | 15 blank | 24 comment | 25 complexity | 3048ccd96cd18a8003d373d6389fa361 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2012, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.test.clustering.single.jdbcstore;
 24
 25import java.sql.Connection;
 26import java.sql.ResultSet;
 27import java.sql.Statement;
 28import javax.annotation.ManagedBean;
 29import javax.annotation.PostConstruct;
 30import javax.annotation.Resource;
 31import javax.naming.Context;
 32import javax.naming.InitialContext;
 33import javax.sql.DataSource;
 34import javax.transaction.UserTransaction;
 35import org.infinispan.Cache;
 36
 37/**
 38 * @author Martin Gencur
 39 */
 40@ManagedBean("infinispan")
 41public class TransactionalInfinispanManagedBean {
 42
 43    private static final String CACHE_JNDI_NAME = "java:jboss/infinispan/cache/jdbccontainer/jdbccache";
 44    private static final String DATASOURCE_JNDI_NAME = "java:jboss/datasources/ExampleDS";
 45
 46    @Resource(name = CACHE_JNDI_NAME)
 47    private Cache<Integer, Object> cache;
 48
 49    @Resource
 50    private UserTransaction tx;
 51
 52    @PostConstruct
 53    public void start() {
 54        assert cache != null;
 55        assert tx != null;
 56    }
 57
 58    public void testTxPutCommit() throws Exception {
 59        tx.begin();
 60        cache.put(1, "v1");
 61        cache.put(2, "v2");
 62        assert "v1".equals(cache.get(1));
 63        assert "v2".equals(cache.get(2));
 64        assert rowCount() == 0;
 65        tx.commit();
 66        assert "v1".equals(cache.get(1));
 67        assert "v2".equals(cache.get(2));
 68        assert rowCount() == 2;
 69    }
 70
 71    public void testTxPutRollback() throws Exception {
 72        tx.begin();
 73        cache.put(1, "v1");
 74        cache.put(2, "v2");
 75        assert "v1".equals(cache.get(1));
 76        assert "v2".equals(cache.get(2));
 77        assert rowCount() == 0;
 78        tx.rollback();
 79        assert cache.get(3) == null;
 80        assert cache.get(4) == null;
 81        assert rowCount() == 0; // no change in DB
 82    }
 83
 84    public void testTxRemoveCommit() throws Exception {
 85        initializeCache();
 86        tx.begin();
 87        assert "v1".equals(cache.get(1));
 88        assert "v2".equals(cache.get(2));
 89        assert rowCount() == 2;
 90        cache.remove(1);
 91        assert cache.get(1) == null;
 92        assert "v2".equals(cache.get(2));
 93        assert rowCount() == 2;
 94        tx.commit();
 95        assert cache.get(1) == null;
 96        assert "v2".equals(cache.get(2));
 97        assert rowCount() == 1;
 98    }
 99
100    public void testTxRemoveRollback() throws Exception {
101        initializeCache();
102        tx.begin();
103        assert "v1".equals(cache.get(1));
104        assert "v2".equals(cache.get(2));
105        assert rowCount() == 2;
106        cache.remove(1);
107        assert cache.get(1) == null;
108        assert "v2".equals(cache.get(2));
109        assert rowCount() == 2;
110        tx.rollback();
111        assert "v1".equals(cache.get(1));
112        assert "v2".equals(cache.get(2));
113        assert rowCount() == 2; // no change in DB
114    }
115
116    public void testTxAlterCommit() throws Exception {
117        initializeCache();
118        tx.begin();
119        assert "v1".equals(cache.get(1));
120        assert "v2".equals(cache.get(2));
121        assert rowCount() == 2;
122        cache.put(1, "v1_new");
123        assert "v1_new".equals(cache.get(1));
124        assert "v2".equals(cache.get(2));
125        assert rowCount() == 2;
126        tx.commit();
127        assert "v1_new".equals(cache.get(1));
128        assert "v2".equals(cache.get(2));
129        assert rowCount() == 2;
130    }
131
132    public void testTxAlterRollback() throws Exception {
133        initializeCache();
134        tx.begin();
135        assert "v1".equals(cache.get(1));
136        assert "v2".equals(cache.get(2));
137        assert rowCount() == 2;
138        cache.put(1, "v1_new");
139        assert "v1_new".equals(cache.get(1));
140        assert "v2".equals(cache.get(2));
141        assert rowCount() == 2;
142        tx.rollback();
143        assert "v1".equals(cache.get(1));
144        assert "v2".equals(cache.get(2));
145        assert rowCount() == 2;
146    }
147
148    private int rowCount() throws Exception {
149        Context ctx = new InitialContext();
150        DataSource ds = (DataSource) ctx.lookup(DATASOURCE_JNDI_NAME);
151        Connection conn = ds.getConnection();
152        Statement s = conn.createStatement();
153        ResultSet rs = s.executeQuery("SELECT id FROM stringbased_jdbccache");
154        int rowCount = 0;
155        while (rs.next()) {
156            rowCount++;
157        }
158        return rowCount;
159    }
160    
161    private void initializeCache() throws Exception {
162        try {
163            tx.begin();
164            cache.clear();
165            cache.put(1, "v1");
166            cache.put(2, "v2");
167            assert cache.keySet().size() == 2;
168            tx.commit();
169        } catch (Exception e) {
170            tx.rollback();
171            throw new RuntimeException(e);
172        }
173    }
174}