diff options
author | Jim Hahn <jrh3@att.com> | 2018-11-29 09:33:02 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-11-29 09:35:21 -0500 |
commit | 2808374bda04dbe709253ca079dfb89d5dbea7dd (patch) | |
tree | 87eabdbd07e0d3f0cfcc1b81e230026aa713ba73 /feature-session-persistence | |
parent | dbb701d2aae428e27c0590a42e2d371bb9bca59d (diff) |
Remove Factory from various drools-pdp classes
Also merged changes from 'Rename test classes in drools-pdp'.
Change-Id: I4d0a3a1b0b5523eadf38d1edb5aa86c404b69d19
Issue-ID: POLICY-1288
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-session-persistence')
2 files changed, 366 insertions, 392 deletions
diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java index 13264d60..ce038d25 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java @@ -65,13 +65,7 @@ import org.slf4j.LoggerFactory; public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngineFeatureAPI { private static final Logger logger = LoggerFactory.getLogger(PersistenceFeature.class); - - /** Standard factory used to get various items. */ - private static Factory stdFactory = new Factory(); - - /** Factory used to get various items. */ - private Factory fact = stdFactory; - + /** KieService factory. */ private KieServices kieSvcFact; @@ -89,15 +83,6 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine private Object cleanupLock = new Object(); /** - * Sets the factory to be used during junit testing. - * - * @param fact factory to be used - */ - protected void setFactory(Factory fact) { - this.fact = fact; - } - - /** * Lookup the adjunct for this feature that is associated with the specified PolicyContainer. If * not found, create one. * @@ -132,10 +117,10 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine @Override public void globalInit(String[] args, String configDir) { - kieSvcFact = fact.getKieServices(); + kieSvcFact = getKieServices(); try { - persistProps = fact.loadProperties(configDir + "/feature-session-persistence.properties"); + persistProps = loadProperties(configDir + "/feature-session-persistence.properties"); } catch (IOException e1) { logger.error("initializePersistence: ", e1); @@ -305,12 +290,12 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine configureSysProps(); - BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); + BasicDataSource ds = makeDataSource(getDataSourceProperties()); DsEmf dsemf = new DsEmf(ds); try { EntityManagerFactory emf = dsemf.emf; - DroolsSessionConnector conn = fact.makeJpaConnector(emf); + DroolsSessionConnector conn = makeJpaConnector(emf); long desiredSessionId = getSessionId(conn, name); @@ -450,9 +435,9 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine */ private void configureKieEnv(Environment env, EntityManagerFactory emf) { env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); - env.set(EnvironmentName.TRANSACTION, fact.getUserTrans()); - env.set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, fact.getTransSyncReg()); - env.set(EnvironmentName.TRANSACTION_MANAGER, fact.getTransMgr()); + env.set(EnvironmentName.TRANSACTION, getUserTrans()); + env.set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, getTransSyncReg()); + env.set(EnvironmentName.TRANSACTION_MANAGER, getTransMgr()); } /** @@ -526,7 +511,7 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine } // now do the record deletion - try (BasicDataSource ds = fact.makeDataSource(getDataSourceProperties()); + try (BasicDataSource ds = makeDataSource(getDataSourceProperties()); Connection connection = ds.getConnection(); PreparedStatement statement = connection.prepareStatement( @@ -577,7 +562,7 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine */ private Properties getProperties(PolicyContainer container) { try { - return fact.getPolicyController(container).getProperties(); + return getPolicyController(container).getProperties(); } catch (IllegalArgumentException e) { logger.error("getProperties exception: ", e); return null; @@ -808,7 +793,7 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine props.put(org.hibernate.cfg.Environment.JPA_JTA_DATASOURCE, bds); this.bds = bds; - this.emf = fact.makeEntMgrFact(props); + this.emf = makeEntMgrFact(props); } catch (RuntimeException e) { closeDataSource(); @@ -851,99 +836,97 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine } /** Factory for various items. Methods can be overridden for junit testing. */ - protected static class Factory { - /** - * Gets the transaction manager. - * - * @return the transaction manager - */ - public TransactionManager getTransMgr() { - return com.arjuna.ats.jta.TransactionManager.transactionManager(); - } + /** + * Gets the transaction manager. + * + * @return the transaction manager + */ + protected TransactionManager getTransMgr() { + return com.arjuna.ats.jta.TransactionManager.transactionManager(); + } - /** - * Gets the user transaction. - * - * @return the user transaction - */ - public UserTransaction getUserTrans() { - return com.arjuna.ats.jta.UserTransaction.userTransaction(); - } + /** + * Gets the user transaction. + * + * @return the user transaction + */ + protected UserTransaction getUserTrans() { + return com.arjuna.ats.jta.UserTransaction.userTransaction(); + } - /** - * Gets the transaction synchronization registry. - * - * @return the transaction synchronization registry - */ - public TransactionSynchronizationRegistry getTransSyncReg() { - return SingletonRegistry.transreg; - } + /** + * Gets the transaction synchronization registry. + * + * @return the transaction synchronization registry + */ + protected TransactionSynchronizationRegistry getTransSyncReg() { + return SingletonRegistry.transreg; + } - /** - * Gets the KIE services. - * - * @return the KIE services - */ - public KieServices getKieServices() { - return KieServices.Factory.get(); - } + /** + * Gets the KIE services. + * + * @return the KIE services + */ + protected KieServices getKieServices() { + return KieServices.Factory.get(); + } - /** - * Loads properties from a file. - * - * @param filenm name of the file to load - * @return properties, as loaded from the file - * @throws IOException if an error occurs reading from the file - */ - public Properties loadProperties(String filenm) throws IOException { - return PropertyUtil.getProperties(filenm); - } + /** + * Loads properties from a file. + * + * @param filenm name of the file to load + * @return properties, as loaded from the file + * @throws IOException if an error occurs reading from the file + */ + protected Properties loadProperties(String filenm) throws IOException { + return PropertyUtil.getProperties(filenm); + } - /** - * Makes a Data Source. - * - * @param dsProps data source properties - * @return a new data source - */ - public BasicDataSource makeDataSource(Properties dsProps) { - try { - return BasicDataSourceFactory.createDataSource(dsProps); + /** + * Makes a Data Source. + * + * @param dsProps data source properties + * @return a new data source + */ + protected BasicDataSource makeDataSource(Properties dsProps) { + try { + return BasicDataSourceFactory.createDataSource(dsProps); - } catch (Exception e) { - throw new PersistenceFeatureException(e); - } + } catch (Exception e) { + throw new PersistenceFeatureException(e); } + } - /** - * Makes a new JPA connector for drools sessions. - * - * @param emf entity manager factory - * @return a new JPA connector for drools sessions - */ - public DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { - return new JpaDroolsSessionConnector(emf); - } + /** + * Makes a new JPA connector for drools sessions. + * + * @param emf entity manager factory + * @return a new JPA connector for drools sessions + */ + protected DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { + return new JpaDroolsSessionConnector(emf); + } - /** - * Makes a new entity manager factory. - * - * @param props properties with which the factory should be configured - * @return a new entity manager factory - */ - public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { - return Persistence.createEntityManagerFactory("onapsessionsPU", props); - } + /** + * Makes a new entity manager factory. + * + * @param props properties with which the factory should be configured + * @return a new entity manager factory + */ + protected EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + return Persistence.createEntityManagerFactory("onapsessionsPU", props); + } - /** - * Gets the policy controller associated with a given policy container. - * - * @param container container whose controller is to be retrieved - * @return the container's controller - */ - public PolicyController getPolicyController(PolicyContainer container) { - return PolicyController.factory.get(container.getGroupId(), container.getArtifactId()); - } + /** + * Gets the policy controller associated with a given policy container. + * + * @param container container whose controller is to be retrieved + * @return the container's controller + */ + protected PolicyController getPolicyController(PolicyContainer container) { + return PolicyController.factory.get(container.getGroupId(), container.getArtifactId()); } /** diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java index 27ac2cc1..1bab1977 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java @@ -83,6 +83,14 @@ import org.slf4j.LoggerFactory; public class PersistenceFeatureTest { + private static final String MY_KIE_BASE = "mybase"; + + private static final String MY_SESS_NAME = "myname"; + + private static final String MISSING_EXCEPTION = "missing exception"; + + private static final String EXPECTED = "expected exception"; + private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class); private static final String JDBC_DRIVER = "fake.driver"; @@ -113,7 +121,9 @@ public class PersistenceFeatureTest { private PolicyController polctlr; private PolicyContainer polcont; private PolicySession polsess; - private PersistenceFeature.Factory fact; + private int emfCount; + private int jpaCount; + private String propName; private PersistenceFeature feat; @@ -163,22 +173,17 @@ public class PersistenceFeatureTest { polcont = mock(PolicyContainer.class); polctlr = mock(PolicyController.class); polsess = mock(PolicySession.class); - fact = mock(PersistenceFeature.Factory.class); - - feat = new PersistenceFeature(); - feat.setFactory(fact); + emfCount = 0; + jpaCount = 0; + propName = null; + + feat = new PersistenceFeatureImpl(); props.putAll(stdprops); System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm"); System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm"); - when(fact.getKieServices()).thenReturn(kiesvc); - when(fact.getTransMgr()).thenReturn(transmgr); - when(fact.getUserTrans()).thenReturn(usertrans); - when(fact.getTransSyncReg()).thenReturn(transreg); - when(fact.loadProperties(anyString())).thenReturn(props); - when(kiesvc.newEnvironment()).thenReturn(kieenv); when(kiesvc.getStoreServices()).thenReturn(kiestore); when(kiesvc.newKieSessionConfiguration()).thenReturn(kiecfg); @@ -215,14 +220,11 @@ public class PersistenceFeatureTest { @Test public void testGetContainerAdjunct_New() throws Exception { - - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); // force getContainerAdjunct() to be invoked - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -234,14 +236,11 @@ public class PersistenceFeatureTest { @Test public void testGetContainerAdjunct_Existing() throws Exception { - - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); // force getContainerAdjunct() to be invoked - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -253,7 +252,8 @@ public class PersistenceFeatureTest { // force getContainerAdjunct() to be invoked again setUpKie("myname2", 999L, true); - feat.activatePolicySession(polcont, "myname2", "mybase"); + mockDbConn(5); + feat.activatePolicySession(polcont, "myname2", MY_KIE_BASE); // ensure it isn't invoked again verify(polcont, times(1)).setAdjunct(any(), any()); @@ -261,18 +261,16 @@ public class PersistenceFeatureTest { @Test public void testGetContainerAdjunct_WrongType() throws Exception { - - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); // return false adjunct on next call when(polcont.getAdjunct(any())).thenReturn("not-a-real-adjunct"); // force getContainerAdjunct() to be invoked setUpKie("myname2", 999L, true); - feat.activatePolicySession(polcont, "myname2", "mybase"); + mockDbConn(5); + feat.activatePolicySession(polcont, "myname2", MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -293,27 +291,30 @@ public class PersistenceFeatureTest { feat.globalInit(null, SRC_TEST_RESOURCES); // verify that various factory methods were invoked - verify(fact).getKieServices(); - verify(fact).loadProperties("src/test/resources/feature-session-persistence.properties"); + assertEquals("src/test/resources/feature-session-persistence.properties", propName); } @Test(expected = NullPointerException.class) public void testGlobalInitIoEx() throws Exception { - when(fact.loadProperties(anyString())).thenThrow(new IOException("expected exception")); + feat = new PersistenceFeatureImpl() { + @Override + protected Properties loadProperties(String filenm) throws IOException { + throw new IOException(EXPECTED); + } + }; feat.globalInit(null, SRC_TEST_RESOURCES); } @Test public void testActivatePolicySession() throws Exception { + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement ps = mockDbConn(5); - setUpKie("myname", 999L, true); - - feat.globalInit(null, SRC_TEST_RESOURCES); + feat.beforeActivate(null); - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); verify(kiestore, never()).newKieSession(any(), any(), any()); @@ -324,22 +325,20 @@ public class PersistenceFeatureTest { verify(kieenv, times(4)).set(anyString(), any()); - verify(jpa).get("myname"); + verify(jpa).get(MY_SESS_NAME); verify(jpa).replace(any()); } @Test public void testActivatePolicySession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement ps = mockDbConn(5); - setUpKie("myname", 999L, true); props.remove("persistence.type"); feat.beforeStart(null); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); verify(ps, never()).executeUpdate(); verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); @@ -349,12 +348,10 @@ public class PersistenceFeatureTest { /** Verifies that a new KIE session is created when there is no existing session entity. */ @Test public void testActivatePolicySession_New() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - mockDbConn(5); setUpKie("noName", 999L, true); + mockDbConn(5); - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any()); verify(kiestore).newKieSession(any(), any(), any()); @@ -363,7 +360,7 @@ public class PersistenceFeatureTest { verify(kieenv, times(4)).set(anyString(), any()); - verify(jpa).get("myname"); + verify(jpa).get(MY_SESS_NAME); verify(jpa).replace(any()); } @@ -372,12 +369,10 @@ public class PersistenceFeatureTest { */ @Test public void testActivatePolicySession_LoadFailed() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); verify(kiestore).newKieSession(any(), any(), any()); @@ -386,35 +381,31 @@ public class PersistenceFeatureTest { verify(kieenv, times(4)).set(anyString(), any()); - verify(jpa).get("myname"); + verify(jpa).get(MY_SESS_NAME); ArgumentCaptor<DroolsSession> drools = ArgumentCaptor.forClass(DroolsSession.class); verify(jpa).replace(drools.capture()); - assertEquals("myname", drools.getValue().getSessionName()); + assertEquals(MY_SESS_NAME, drools.getValue().getSessionName()); assertEquals(100L, drools.getValue().getSessionId()); } @Test public void testLoadDataSource() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); - verify(fact).makeEntMgrFact(any()); + assertEquals(1, emfCount); } @Test public void testConfigureSysProps() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); assertEquals("60", System.getProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout")); assertEquals(JTA_OSDIR, System.getProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir")); @@ -423,12 +414,10 @@ public class PersistenceFeatureTest { @Test public void testConfigureKieEnv() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kieenv, times(4)).set(any(), any()); @@ -442,19 +431,24 @@ public class PersistenceFeatureTest { @Test public void testConfigureKieEnv_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - - when(fact.getUserTrans()).thenThrow(new IllegalArgumentException("expected exception")); + + feat = new PersistenceFeatureMockDb() { + @Override + protected UserTransaction getUserTrans() { + throw new IllegalArgumentException(EXPECTED); + } + }; + + feat.globalInit(null, SRC_TEST_RESOURCES); try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + fail(MISSING_EXCEPTION); } catch (IllegalArgumentException ex) { - logger.trace("expected exception", ex); + logger.trace(EXPECTED, ex); } verify(bds, times(2)).close(); @@ -462,12 +456,10 @@ public class PersistenceFeatureTest { @Test public void testLoadKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); - - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv); verify(kiestore, never()).newKieSession(any(), any(), any()); @@ -481,15 +473,13 @@ public class PersistenceFeatureTest { */ @Test public void testLoadKieSession_Ex() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); when(kiestore.loadKieSession(anyLong(), any(), any(), any())) - .thenThrow(new IllegalArgumentException("expected exception")); + .thenThrow(new IllegalArgumentException(EXPECTED)); - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).loadKieSession(anyLong(), any(), any(), any()); verify(kiestore).newKieSession(any(), any(), any()); @@ -499,12 +489,10 @@ public class PersistenceFeatureTest { @Test public void testNewKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - KieSession session = feat.activatePolicySession(polcont, "myname", "mybase"); + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).newKieSession(kiebase, null, kieenv); @@ -513,11 +501,10 @@ public class PersistenceFeatureTest { @Test public void testLoadDataSource_DiffSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -527,16 +514,17 @@ public class PersistenceFeatureTest { when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); setUpKie("myname2", 999L, false); + mockDbConn(5); // invoke it again - feat.activatePolicySession(polcont, "myname2", "mybase"); + feat.activatePolicySession(polcont, "myname2", MY_KIE_BASE); - verify(fact, times(2)).makeEntMgrFact(any()); + assertEquals(2, emfCount); } @Test public void testSelectThreadModel_Persistent() throws Exception { - setUpKie("myname", 999L, true); + setUpKie(MY_SESS_NAME, 999L, true); ThreadModel model = feat.selectThreadModel(polsess); assertNotNull(model); @@ -545,13 +533,12 @@ public class PersistenceFeatureTest { @Test public void testSelectThreadModel_NotPersistent() throws Exception { - when(fact.getPolicyController(any())).thenReturn(polctlr); assertNull(feat.selectThreadModel(polsess)); } @Test public void testSelectThreadModel_Start__Run_Update_Stop() throws Exception { - setUpKie("myname", 999L, true); + setUpKie(MY_SESS_NAME, 999L, true); ThreadModel model = feat.selectThreadModel(polsess); assertNotNull(model); @@ -567,17 +554,16 @@ public class PersistenceFeatureTest { @Test public void testDisposeKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = - ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(emf, never()).close(); + + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -599,17 +585,16 @@ public class PersistenceFeatureTest { @Test public void testDisposeKieSession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = - ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(emf, never()).close(); + + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -624,17 +609,16 @@ public class PersistenceFeatureTest { @Test public void testDestroyKieSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = - ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(emf, never()).close(); + + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -656,17 +640,16 @@ public class PersistenceFeatureTest { @Test public void testDestroyKieSession_NoPersistence() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = - ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(emf, never()).close(); + + final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = + ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -761,13 +744,10 @@ public class PersistenceFeatureTest { @Test public void testGetPersistenceTimeout_Valid() throws Exception { + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(5); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement).executeUpdate(); } @@ -777,13 +757,10 @@ public class PersistenceFeatureTest { props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, never()).executeUpdate(); } @@ -791,24 +768,21 @@ public class PersistenceFeatureTest { @Test public void testGetPersistenceTimeout_Invalid() throws Exception { props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc"); + + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement s = mockDbConn(0); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(s, never()).executeUpdate(); } @Test public void testCleanUpSessionInfo() throws Exception { - setUpKie("myname", 999L, true); + setUpKie(MY_SESS_NAME, 999L, true); // use a real DB so we can verify that the "delete" works correctly - fact = new PartialFactory(); - feat.setFactory(fact); + feat = new PartialFeature(); makeSessionInfoTbl(20000); @@ -819,70 +793,64 @@ public class PersistenceFeatureTest { feat.globalInit(null, SRC_TEST_RESOURCES); feat.beforeStart(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); assertEquals("[1, 4, 5]", getSessions().toString()); } @Test public void testCleanUpSessionInfo_WithBeforeStart() throws Exception { + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - // reset feat.beforeStart(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(1)).executeUpdate(); // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(1)).executeUpdate(); // reset feat.beforeStart(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(2)).executeUpdate(); // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(2)).executeUpdate(); } @Test public void testCleanUpSessionInfo_WithBeforeActivate() throws Exception { + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - // reset feat.beforeActivate(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(1)).executeUpdate(); // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(1)).executeUpdate(); // reset feat.beforeActivate(null); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(2)).executeUpdate(); // should not clean-up again - feat.activatePolicySession(polcont, "myname", "mybase"); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, times(2)).executeUpdate(); } @@ -891,32 +859,26 @@ public class PersistenceFeatureTest { props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT); + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement, never()).executeUpdate(); } @Test public void testCleanUpSessionInfo_NoUrl() throws Exception { - final PreparedStatement statement = mockDbConn(0); - props.remove(DroolsPersistenceProperties.DB_URL); - - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); + + setUpKie(MY_SESS_NAME, 999L, true); + final PreparedStatement statement = mockDbConn(0); try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + fail(MISSING_EXCEPTION); } catch (RuntimeException e) { - logger.trace("expected exception", e); + logger.trace(EXPECTED, e); } verify(statement, never()).executeUpdate(); @@ -924,19 +886,16 @@ public class PersistenceFeatureTest { @Test public void testCleanUpSessionInfo_NoUser() throws Exception { - final PreparedStatement statement = mockDbConn(0); - props.remove(DroolsPersistenceProperties.DB_USER); - - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); + + setUpKie(MY_SESS_NAME, 999L, true); + final PreparedStatement statement = mockDbConn(0); try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + fail(MISSING_EXCEPTION); } catch (RuntimeException e) { - logger.trace("expected exception", e); + logger.trace(EXPECTED, e); } verify(statement, never()).executeUpdate(); @@ -944,19 +903,16 @@ public class PersistenceFeatureTest { @Test public void testCleanUpSessionInfo_NoPassword() throws Exception { - final PreparedStatement statement = mockDbConn(0); - props.remove(DroolsPersistenceProperties.DB_PWD); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); + setUpKie(MY_SESS_NAME, 999L, true); + final PreparedStatement statement = mockDbConn(0); try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + fail(MISSING_EXCEPTION); } catch (RuntimeException e) { - logger.trace("expected exception", e); + logger.trace(EXPECTED, e); } verify(statement, never()).executeUpdate(); @@ -964,162 +920,141 @@ public class PersistenceFeatureTest { @Test public void testCleanUpSessionInfo_SqlEx() throws Exception { + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(-1); - feat.globalInit(null, SRC_TEST_RESOURCES); - - setUpKie("myname", 999L, true); - - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(statement).executeUpdate(); } @Test public void testGetDroolsSessionConnector() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); - verify(fact).makeJpaConnector(emf); + assertEquals(1, jpaCount); } @Test public void testReplaceSession() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - - final ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + final ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class); + verify(jpa).replace(sesscap.capture()); - assertEquals("myname", sesscap.getValue().getSessionName()); + assertEquals(MY_SESS_NAME, sesscap.getValue().getSessionName()); assertEquals(999L, sesscap.getValue().getSessionId()); } @Test public void testIsPersistenceEnabled_Auto() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.setProperty("persistence.type", "auto"); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNotNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testIsPersistenceEnabled_Native() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.setProperty("persistence.type", "native"); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNotNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testIsPersistenceEnabled_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.remove("persistence.type"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperties_Ex() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); - when(fact.getPolicyController(polcont)) - .thenThrow(new IllegalArgumentException("expected exception")); + feat = new PersistenceFeatureMockDb() { + @Override + protected PolicyController getPolicyController(PolicyContainer container) { + throw new IllegalArgumentException(EXPECTED); + } + }; - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperty_Specific() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.remove("persistence.type"); props.setProperty("persistence.myname.type", "auto"); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNotNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperty_Specific_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.remove("persistence.type"); props.setProperty("persistence.xxx.type", "auto"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperty_Both_SpecificOn() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.setProperty("persistence.type", "other"); props.setProperty("persistence.myname.type", "auto"); - assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNotNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperty_Both_SpecificDisabledOff() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.setProperty("persistence.type", "auto"); props.setProperty("persistence.myname.type", "other"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testGetProperty_None() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - setUpKie("myname", 999L, true); props.remove("persistence.type"); - assertNull(feat.activatePolicySession(polcont, "myname", "mybase")); + assertNull(feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE)); } @Test public void testPersistenceFeatureException() { - SecurityException secex = new SecurityException("expected exception"); + SecurityException secex = new SecurityException(EXPECTED); PersistenceFeatureException ex = new PersistenceFeatureException(secex); assertEquals(secex, ex.getCause()); @@ -1127,19 +1062,24 @@ public class PersistenceFeatureTest { @Test public void testDsEmf_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - when(fact.makeEntMgrFact(any())).thenThrow(new IllegalArgumentException("expected exception")); + feat = new PersistenceFeatureMockDb() { + @Override + protected EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + throw new IllegalArgumentException(EXPECTED); + } + }; + + feat.globalInit(null, SRC_TEST_RESOURCES); try { - feat.activatePolicySession(polcont, "myname", "mybase"); - fail("missing exception"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); + fail(MISSING_EXCEPTION); } catch (IllegalArgumentException ex) { - logger.trace("expected exception", ex); + logger.trace(EXPECTED, ex); } verify(bds, times(2)).close(); @@ -1147,12 +1087,10 @@ public class PersistenceFeatureTest { @Test public void testDsEmf_Close_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -1163,13 +1101,13 @@ public class PersistenceFeatureTest { when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); try { - doThrow(new IllegalArgumentException("expected exception")).when(emf).close(); + doThrow(new IllegalArgumentException(EXPECTED)).when(emf).close(); feat.destroyKieSession(polsess); - fail("missing exception"); + fail(MISSING_EXCEPTION); } catch (IllegalArgumentException ex) { - logger.trace("expected exception", ex); + logger.trace(EXPECTED, ex); } verify(bds, times(2)).close(); @@ -1177,12 +1115,10 @@ public class PersistenceFeatureTest { @Test public void testDsEmf_CloseDataSource_RtEx() throws Exception { - feat.globalInit(null, SRC_TEST_RESOURCES); - + setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - setUpKie("myname", 999L, false); - feat.activatePolicySession(polcont, "myname", "mybase"); + feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); @@ -1193,13 +1129,13 @@ public class PersistenceFeatureTest { when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); try { - doThrow(new SQLException("expected exception")).when(bds).close(); + doThrow(new SQLException(EXPECTED)).when(bds).close(); feat.destroyKieSession(polsess); - fail("missing exception"); + fail(MISSING_EXCEPTION); } catch (PersistenceFeatureException ex) { - logger.trace("expected exception", ex); + logger.trace(EXPECTED, ex); } } @@ -1236,11 +1172,6 @@ public class PersistenceFeatureTest { * @throws Exception exception */ private void setUpKie(String sessnm, long sessid, boolean loadOk) throws Exception { - - when(fact.makeJpaConnector(emf)).thenReturn(jpa); - when(fact.makeEntMgrFact(any())).thenReturn(emf); - when(fact.getPolicyController(polcont)).thenReturn(polctlr); - props.setProperty("persistence.type", "auto"); when(polctlr.getProperties()).thenReturn(props); @@ -1263,6 +1194,9 @@ public class PersistenceFeatureTest { } when(kiestore.newKieSession(any(), any(), any())).thenReturn(kiesess); + + feat = new PersistenceFeatureKie(); + feat.globalInit(null, SRC_TEST_RESOURCES); } /** @@ -1363,51 +1297,108 @@ public class PersistenceFeatureTest { PreparedStatement statement = mock(PreparedStatement.class); when(bds.getConnection()).thenReturn(connection); - when(fact.makeDataSource(any())).thenReturn(bds); when(connection.prepareStatement(anyString())).thenReturn(statement); if (retval < 0) { // should throw an exception - when(statement.executeUpdate()).thenThrow(new SQLException("expected exception")); + when(statement.executeUpdate()).thenThrow(new SQLException(EXPECTED)); } else { // should return the value when(statement.executeUpdate()).thenReturn(retval); } + feat = new PersistenceFeatureMockDb(); + feat.globalInit(null, SRC_TEST_RESOURCES); + return statement; } + + /** + * Feature with a mock DB. + */ + private class PersistenceFeatureMockDb extends PersistenceFeatureKie { - /** A partial factory, which exports a few of the real methods, but overrides the rest. */ - private class PartialFactory extends PersistenceFeature.Factory { + @Override + protected BasicDataSource makeDataSource(Properties dsProps) { + return bds; + } + } + + /** + * Feature supporting newKieSession. + */ + private class PersistenceFeatureKie extends PersistenceFeatureImpl { + + @Override + protected EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + ++emfCount; + return emf; + } + + @Override + protected DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { + ++jpaCount; + return jpa; + } + } + + /** + * Feature with overrides. + */ + private class PersistenceFeatureImpl extends PartialFeature { + + @Override + protected Properties loadProperties(String filenm) throws IOException { + propName = filenm; + return props; + } + + @Override + protected BasicDataSource makeDataSource(Properties dsProps) { + return null; + } + + @Override + protected DroolsSessionConnector makeJpaConnector(EntityManagerFactory emf) { + ++jpaCount; + return null; + } + } + + /** + * Feature with <i>some</i> overrides. + */ + private class PartialFeature extends PersistenceFeature { @Override - public TransactionManager getTransMgr() { + protected TransactionManager getTransMgr() { return transmgr; } @Override - public UserTransaction getUserTrans() { + protected UserTransaction getUserTrans() { return usertrans; } @Override - public TransactionSynchronizationRegistry getTransSyncReg() { + protected TransactionSynchronizationRegistry getTransSyncReg() { return transreg; } @Override - public KieServices getKieServices() { + protected KieServices getKieServices() { return kiesvc; } @Override - public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + protected EntityManagerFactory makeEntMgrFact(Map<String, Object> props) { + ++emfCount; return emf; } @Override - public PolicyController getPolicyController(PolicyContainer container) { + protected PolicyController getPolicyController(PolicyContainer container) { return polctlr; } } |