OPENJPA-1739 Fixed minor bug and added more test variations.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@982342 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2010-08-04 17:33:13 +00:00
parent 46026f012c
commit 536e3a284b
4 changed files with 217 additions and 13 deletions

View File

@ -220,7 +220,7 @@ public abstract class AbstractDataCacheInstrument extends AbstractInstrument
private long getReadCount(Class<?> c) {
CacheStatistics stats = getStatistics();
if (stats != null)
stats.getReadCount(c);
return stats.getReadCount(c);
return NO_STATS;
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.instrumentation;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="INST_CACHE_ENT")
public class CacheableEntity {
@Id
private int id;
@Basic
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.instrumentation;
import org.apache.openjpa.lib.instrumentation.AbstractInstrumentationProvider;
import org.apache.openjpa.lib.instrumentation.Instrument;
public class SecondProvider extends AbstractInstrumentationProvider {
public static final String[] INSTRUMENT_ALIASES = {
"DataCache", "org.apache.openjpa.instrumentation.DCInstrument",
"QueryCache","org.apache.openjpa.instrumentation.QCInstrument",
"QuerySQLCache","org.apache.openjpa.instrumentation.QSCInstrument"
};
public void start() {
setStarted(true);
}
public void stop() {
setStarted(false);
for (Instrument inst : getInstruments()) {
stopInstrument(inst);
}
}
public void startInstrument(Instrument instrument) {
instrument.start();
}
public void stopInstrument(Instrument instrument, boolean force) {
instrument.stop();
}
@Override
public String[] getInstrumentAliases() {
return INSTRUMENT_ALIASES;
}
}

View File

@ -18,12 +18,15 @@
*/
package org.apache.openjpa.instrumentation;
import java.util.Random;
import java.util.Set;
import javax.persistence.EntityManager;
import org.apache.openjpa.lib.instrumentation.Instrument;
import org.apache.openjpa.lib.instrumentation.InstrumentationProvider;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
@ -34,12 +37,19 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
*/
public class TestInstrumentationProvider extends SingleEMFTestCase {
public static final String INSTRUMENTATION =
public static final String SINGLE_PROVIDER =
"org.apache.openjpa.instrumentation.SimpleProvider(Instrument='DataCache,QueryCache,QuerySQLCache')";
public static final String MULTI_PROVIDER =
"org.apache.openjpa.instrumentation.SimpleProvider(Instrument='DataCache,QueryCache,QuerySQLCache'), " +
"org.apache.openjpa.instrumentation.SecondProvider(Instrument='DataCache,QuerySQLCache')";
public static final String DC_PROVIDER =
"org.apache.openjpa.instrumentation.SimpleProvider(Instrument='DataCache')";
public void setUp() throws Exception {
super.setUp("openjpa.Instrumentation",
INSTRUMENTATION,
super.setUp("openjpa.Instrumentation",
SINGLE_PROVIDER,
"openjpa.DataCache",
"true(EnableStatistics=true)",
"openjpa.QueryCache",
@ -58,7 +68,7 @@ public class TestInstrumentationProvider extends SingleEMFTestCase {
// Verify the instrumentation value was stored in the config
String instrValue = emf.getConfiguration().getInstrumentation();
assertEquals(instrValue, INSTRUMENTATION);
assertEquals(instrValue, SINGLE_PROVIDER);
// Verify an instrumentation manager is available
InstrumentationManager mgr = emf.getConfiguration().getInstrumentationManagerInstance();
@ -144,16 +154,104 @@ public class TestInstrumentationProvider extends SingleEMFTestCase {
}
/**
* Verifies the cache metrics are available through simple instrumentation.
* Verifies the data cache metrics are available through simple instrumentation.
*/
// public void testCacheInstruments() {
//
// }
public void testDataCacheInstrument() {
OpenJPAEntityManagerFactorySPI oemf = createEMF(
"openjpa.Instrumentation", DC_PROVIDER,
"openjpa.DataCache", "true(EnableStatistics=true)",
"openjpa.RemoteCommitProvider", "sjvm",
"openjpa.jdbc.SynchronizeMappings", "buildSchema",
CacheableEntity.class);
// Verify an EMF was created with the supplied instrumentation
assertNotNull(oemf);
// Verify the instrumentation value was stored in the config
String instrValue = oemf.getConfiguration().getInstrumentation();
assertEquals(DC_PROVIDER, instrValue);
// Verify an instrumentation manager is available
InstrumentationManager mgr = oemf.getConfiguration().getInstrumentationManagerInstance();
assertNotNull(mgr);
// Get the data cache instrument
Set<InstrumentationProvider> providers = mgr.getProviders();
assertNotNull(providers);
assertEquals(1, providers.size());
InstrumentationProvider provider = providers.iterator().next();
assertEquals(provider.getClass(), SimpleProvider.class);
Instrument inst = provider.getInstrumentByName(DCInstrument.NAME);
assertNotNull(inst);
assertTrue(inst instanceof DataCacheInstrument);
DataCacheInstrument dci = (DataCacheInstrument)inst;
assertEquals(dci.getCacheName(), "default");
OpenJPAEntityManagerSPI oem = oemf.createEntityManager();
CacheableEntity ce = new CacheableEntity();
int id = new Random().nextInt();
ce.setId(id);
oem.getTransaction().begin();
oem.persist(ce);
oem.getTransaction().commit();
oem.clear();
assertTrue(oemf.getCache().contains(CacheableEntity.class, id));
ce = oem.find(CacheableEntity.class, id);
assertTrue(dci.getHitCount() > 0);
assertTrue(dci.getReadCount() > 0);
assertTrue(dci.getWriteCount() > 0);
try {
assertTrue(dci.getHitCount(CacheableEntity.class.getName()) > 0);
assertTrue(dci.getReadCount(CacheableEntity.class.getName()) > 0);
assertTrue(dci.getWriteCount(CacheableEntity.class.getName()) > 0);
} catch (ClassNotFoundException e) {
fail("Class name based assertion failed");
}
closeEMF(oemf);
}
/**
* Verifies multiple instrumentation providers can be specified.
*/
// public void testMultipleProviderConfig() {
//
// }
public void testMultipleProviderConfig() {
OpenJPAEntityManagerFactorySPI oemf = createEMF(
"openjpa.Instrumentation",
MULTI_PROVIDER,
"openjpa.DataCache",
"true(EnableStatistics=true)",
"openjpa.QueryCache",
"true",
"openjpa.RemoteCommitProvider",
"sjvm");
// Verify an EMF was created with the supplied instrumentation
assertNotNull(oemf);
// Verify the instrumentation value was stored in the config
String instrValue = oemf.getConfiguration().getInstrumentation();
assertEquals(MULTI_PROVIDER, instrValue);
// Verify an instrumentation manager is available
InstrumentationManager mgr = oemf.getConfiguration().getInstrumentationManagerInstance();
assertNotNull(mgr);
// Verify the manager is managing the correct provider
Set<InstrumentationProvider> providers = mgr.getProviders();
assertNotNull(providers);
assertEquals(2, providers.size());
for (InstrumentationProvider provider : providers) {
assertTrue( provider instanceof SimpleProvider ||
provider instanceof SecondProvider);
if (provider instanceof SimpleProvider) {
assertEquals(3, provider.getInstruments().size());
} else {
assertEquals(2, provider.getInstruments().size());
}
}
closeEMF(oemf);
}
}