/** * Parses the given properties into a name/attribute map. * Expects method names as keys and String attributes definitions as values, * parsable into TransactionAttribute instances via TransactionAttributeEditor. * @see #setNameMap * @see TransactionAttributeEditor */ public void setProperties(Properties transactionAttributes) { TransactionAttributeEditor tae = new TransactionAttributeEditor(); Enumeration<?> propNames = transactionAttributes.propertyNames(); while (propNames.hasMoreElements()) { String methodName = (String) propNames.nextElement(); String value = transactionAttributes.getProperty(methodName); tae.setAsText(value); TransactionAttribute attr = (TransactionAttribute) tae.getValue(); addTransactionalMethod(methodName, attr); } }
@Test(expected = IllegalArgumentException.class) public void testValidPropagationAndIsolationCodesAndInvalidRollbackRule() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); // should fail with bogus rollback rule pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidPropagationCodeOnly() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); // should have failed with bogus propagation code pe.setAsText("XXPROPAGATION_REQUIRED"); }
/** * Parses the given properties into a name/attribute map. * Expects method names as keys and String attributes definitions as values, * parsable into TransactionAttribute instances via TransactionAttributeEditor. * @see #setNameMap * @see TransactionAttributeEditor */ public void setProperties(Properties transactionAttributes) { TransactionAttributeEditor tae = new TransactionAttributeEditor(); Enumeration<?> propNames = transactionAttributes.propertyNames(); while (propNames.hasMoreElements()) { String methodName = (String) propNames.nextElement(); String value = transactionAttributes.getProperty(methodName); tae.setAsText(value); TransactionAttribute attr = (TransactionAttribute) tae.getValue(); addTransactionalMethod(methodName, attr); } }
@Test public void testEmptyString() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(""); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertTrue(ta == null); }
@Test public void testNull() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(null); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertTrue(ta == null); }
@Override public void setAsText(String text) throws IllegalArgumentException { MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource(); if (StringUtils.hasLength(text)) { // Use properties editor to tokenize the hold string. PropertiesEditor propertiesEditor = new PropertiesEditor(); propertiesEditor.setAsText(text); Properties props = (Properties) propertiesEditor.getValue(); // Now we have properties, process each one individually. TransactionAttributeEditor tae = new TransactionAttributeEditor(); Enumeration<?> propNames = props.propertyNames(); while (propNames.hasMoreElements()) { String name = (String) propNames.nextElement(); String value = props.getProperty(name); // Convert value to a transaction attribute. tae.setAsText(value); TransactionAttribute attr = (TransactionAttribute) tae.getValue(); // Register name and attribute. source.addTransactionalMethod(name, attr); } } setValue(source); }
@Override public void setAsText(String text) throws IllegalArgumentException { MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource(); if (StringUtils.hasLength(text)) { // Use properties editor to tokenize the hold string. PropertiesEditor propertiesEditor = new PropertiesEditor(); propertiesEditor.setAsText(text); Properties props = (Properties) propertiesEditor.getValue(); // Now we have properties, process each one individually. TransactionAttributeEditor tae = new TransactionAttributeEditor(); Enumeration<?> propNames = props.propertyNames(); while (propNames.hasMoreElements()) { String name = (String) propNames.nextElement(); String value = props.getProperty(name); // Convert value to a transaction attribute. tae.setAsText(value); TransactionAttribute attr = (TransactionAttribute) tae.getValue(); // Register name and attribute. source.addTransactionalMethod(name, attr); } } setValue(source); }
@Test public void testValidPropagationCodeAndIsolationCode() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText("PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertTrue(ta != null); assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED); assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED); }
@Test public void testValidPropagationCodeOnly() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText("PROPAGATION_REQUIRED"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertTrue(ta != null); assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED); assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT); assertTrue(!ta.isReadOnly()); }
@Test public void testChangeRollbackRules() throws Exception { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText("+RuntimeException"); TransactionAttribute attr = (TransactionAttribute) editor.getValue(); assertFalse(attr.rollbackOn(new RuntimeException(""))); assertFalse(attr.rollbackOn(new Exception(""))); }
@Test public void testOverrideWithoutChangingRollbackRules() throws Exception { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText("-RuntimeException"); TransactionAttribute attr = (TransactionAttribute) editor.getValue(); assertTrue(attr.rollbackOn(new RuntimeException(""))); assertFalse(attr.rollbackOn(new Exception(""))); }
@Test public void testToStringMatchesEditor() { List<RollbackRuleAttribute> list = new LinkedList<>(); list.add(new NoRollbackRuleAttribute("Throwable")); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); TransactionAttributeEditor tae = new TransactionAttributeEditor(); tae.setAsText(rta.toString()); rta = (RuleBasedTransactionAttribute) tae.getValue(); assertFalse(rta.rollbackOn(new Throwable())); assertFalse(rta.rollbackOn(new RuntimeException())); assertFalse(rta.rollbackOn(new MyRuntimeException(""))); assertFalse(rta.rollbackOn(new Exception())); assertFalse(rta.rollbackOn(new IOException())); }
@Test public void testNonDefaultRollbackRules() throws Exception { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText("+RuntimeException,+SkippableException"); RuleBasedTransactionAttribute attr = (RuleBasedTransactionAttribute) editor.getValue(); attr.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); assertTrue(attr.rollbackOn(new Exception(""))); assertFalse(attr.rollbackOn(new RuntimeException(""))); assertFalse(attr.rollbackOn(new SkippableException(""))); }
@Test public void testValidPropagationCodeAndIsolationCodeAndRollbackRules1() { TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText("PROPAGATION_MANDATORY,ISOLATION_REPEATABLE_READ,timeout_10,-IOException,+MyRuntimeException"); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertNotNull(ta); assertEquals(TransactionDefinition.PROPAGATION_MANDATORY, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.ISOLATION_REPEATABLE_READ, ta.getIsolationLevel()); assertEquals(10, ta.getTimeout()); assertFalse(ta.isReadOnly()); assertTrue(ta.rollbackOn(new RuntimeException())); assertFalse(ta.rollbackOn(new Exception())); // Check for our bizarre customized rollback rules assertTrue(ta.rollbackOn(new IOException())); assertTrue(!ta.rollbackOn(new MyRuntimeException(""))); }