001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.conf;
020:
021: import org.apache.openjpa.lib.conf.Value;
022: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
023: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
024: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
025:
026: /**
027: * Tests dynamic modification of configuration property.
028: *
029: * @author Pinaki Poddar
030: *
031: */
032: public class TestDynamicConfiguration extends SingleEMFTestCase {
033:
034: public void testConfigurationIsEqualByValueAndHashCode() {
035: OpenJPAEntityManagerFactorySPI emf1 = createEMF();
036: assertNotNull(emf1);
037: OpenJPAConfiguration conf1 = emf1.getConfiguration();
038:
039: OpenJPAEntityManagerFactorySPI emf2 = createEMF();
040: assertNotNull(emf2);
041: OpenJPAConfiguration conf2 = emf2.getConfiguration();
042:
043: assertFalse(emf1 == emf2);
044: assertFalse(emf1.equals(emf2));
045: assertFalse(conf1 == conf2);
046: assertEquals(conf1, conf2);
047: assertEquals(conf1.hashCode(), conf2.hashCode());
048: assertEquals(conf1.toProperties(false), conf2
049: .toProperties(false));
050: }
051:
052: public void testConfigurationIsReadOnlyAfterFirstConstruction() {
053: OpenJPAConfiguration conf = emf.getConfiguration();
054: assertFalse(conf.isReadOnly());
055: emf.createEntityManager();
056: assertTrue(conf.isReadOnly());
057: }
058:
059: public void testNonDynamicValuesCanNotBeChanged() {
060: emf.createEntityManager();
061: OpenJPAConfiguration conf = emf.getConfiguration();
062:
063: String oldValue = conf.getConnectionURL();
064: String newValue = "jdbc://mydb:8087/DBDoesNotExist";
065: try {
066: conf.setConnectionURL(newValue);
067: fail("Expected exception to modify configuration");
068: } catch (Exception ex) { // good
069: assertEquals(oldValue, conf.getConnectionURL());
070: }
071: }
072:
073: public void testDynamicValuesCanBeChanged() {
074: OpenJPAConfiguration conf = emf.getConfiguration();
075:
076: int oldValue = conf.getLockTimeout();
077: int newValue = oldValue + 10;
078:
079: conf.setLockTimeout(newValue);
080: assertEquals(newValue, conf.getLockTimeout());
081: }
082:
083: public void testDynamicValuesAreCorrectlySet() {
084: OpenJPAConfiguration conf = emf.getConfiguration();
085:
086: Value lockTimeoutValue = conf.getValue("LockTimeout");
087: assertNotNull(lockTimeoutValue);
088: assertTrue(lockTimeoutValue.isDynamic());
089:
090: Value connectionURLValue = conf.getValue("ConnectionURL");
091: assertNotNull(connectionURLValue);
092: assertFalse(connectionURLValue.isDynamic());
093: }
094:
095: public void testDynamicChangeDoesNotChangeHashCode() {
096: OpenJPAConfiguration conf1 = emf.getConfiguration();
097:
098: int oldValue = conf1.getLockTimeout();
099: int newValue = oldValue + 10;
100: int oldHash = conf1.hashCode();
101: conf1.setLockTimeout(newValue);
102: int newHash = conf1.hashCode();
103:
104: assertEquals(oldHash, newHash);
105: }
106: }
|