Author: jrbauer
Date: Wed Aug 4 17:33:13 2010
New Revision: 982342
URL: http://svn.apache.org/viewvc?rev=982342&view=rev
Log:
OPENJPA-1739 Fixed minor bug and added more test variations.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java
(with props)
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/AbstractDataCacheInstrument.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/AbstractDataCacheInstrument.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/AbstractDataCacheInstrument.java?rev=982342&r1=982341&r2=982342&view=diff
==============================================================================
---
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/AbstractDataCacheInstrument.java
(original)
+++
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/AbstractDataCacheInstrument.java
Wed Aug 4 17:33:13 2010
@@ -220,7 +220,7 @@ public abstract class AbstractDataCacheI
private long getReadCount(Class<?> c) {
CacheStatistics stats = getStatistics();
if (stats != null)
- stats.getReadCount(c);
+ return stats.getReadCount(c);
return NO_STATS;
}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java?rev=982342&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java
Wed Aug 4 17:33:13 2010
@@ -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;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/CacheableEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java?rev=982342&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java
Wed Aug 4 17:33:13 2010
@@ -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;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SecondProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java?rev=982342&r1=982341&r2=982342&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
Wed Aug 4 17:33:13 2010
@@ -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.te
*/
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
// 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
}
/**
- * 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);
+ }
}