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 java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.PrintStream;
024: import java.util.ArrayList;
025: import java.util.List;
026: import javax.persistence.EntityManager;
027: import javax.persistence.Query;
028:
029: import org.apache.openjpa.persistence.JPAFacadeHelper;
030: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
031: import org.apache.openjpa.persistence.query.NamedQueryEntity;
032: import org.apache.openjpa.persistence.simple.AllFieldTypes;
033: import org.apache.openjpa.persistence.test.PersistenceTestCase;
034: import org.apache.openjpa.lib.log.Log;
035:
036: public class TestCacheMarshallerEndToEnd extends PersistenceTestCase {
037:
038: private static final Object[] STORE_PROPS = new Object[] {
039: "openjpa.CacheMarshallers",
040: "default(Id="
041: + MetaDataCacheMaintenance.class.getName()
042: + ", OutputFile=target/"
043: + MetaDataCacheMaintenance.class.getName()
044: + ".ser"
045: + ", ConsumeSerializationErrors=false"
046: + ", ValidationPolicy="
047: + OpenJPAVersionAndConfigurationTypeValidationPolicy.class
048: .getName() + ")",
049: "openjpa.QueryCompilationCache",
050: "java.util.concurrent.ConcurrentHashMap",
051: AllFieldTypes.class, NamedQueryEntity.class, CLEAR_TABLES };
052:
053: private static final Object[] LOAD_PROPS = new Object[] {
054: "openjpa.CacheMarshallers",
055: "default(Id="
056: + MetaDataCacheMaintenance.class.getName()
057: + ", InputURL=file:target/"
058: + MetaDataCacheMaintenance.class.getName()
059: + ".ser"
060: + ", ConsumeSerializationErrors=false"
061: + ", ValidationPolicy="
062: + OpenJPAVersionAndConfigurationTypeValidationPolicy.class
063: .getName() + ")",
064: "openjpa.QueryCompilationCache",
065: "java.util.concurrent.ConcurrentHashMap",
066: AllFieldTypes.class, NamedQueryEntity.class };
067:
068: public void testCacheMarshallerEndToEnd() throws IOException {
069: OpenJPAEntityManagerFactorySPI emf = createEMF(STORE_PROPS);
070: CacheMarshallerImpl cm = (CacheMarshallerImpl) CacheMarshallersValue
071: .getMarshallerById(emf.getConfiguration(),
072: MetaDataCacheMaintenance.class.getName());
073: cm.getOutputFile().delete();
074: MetaDataCacheMaintenance maint = new MetaDataCacheMaintenance(
075: JPAFacadeHelper.toBrokerFactory(emf), false);
076: LogImpl log = new LogImpl();
077: maint.setLog(log);
078: maint.store();
079: assertContains(log.lines, " "
080: + AllFieldTypes.class.getName());
081: assertContains(log.lines, " "
082: + NamedQueryEntity.class.getName());
083: assertContains(log.lines, " NamedQueryEntity.namedQuery");
084: emf.close();
085:
086: emf = createEMF(LOAD_PROPS);
087: EntityManager em = emf.createEntityManager();
088: em.getTransaction().begin();
089: em.persist(new NamedQueryEntity("foo"));
090: em.flush();
091: Query q = em.createNamedQuery("NamedQueryEntity.namedQuery");
092: assertEquals(1, q.getResultList().size());
093: em.getTransaction().rollback();
094: em.close();
095: emf.close();
096: }
097:
098: private void assertContains(List<String> lines, String prefix) {
099: for (String line : lines)
100: if (line.startsWith(prefix))
101: return;
102: fail("should contain a line starting with " + prefix + ": "
103: + lines);
104: }
105:
106: private class LogImpl implements Log {
107: private List<String> lines = new ArrayList<String>();
108:
109: public boolean isTraceEnabled() {
110: return true;
111: }
112:
113: public boolean isInfoEnabled() {
114: return true;
115: }
116:
117: public boolean isWarnEnabled() {
118: throw new UnsupportedOperationException();
119: }
120:
121: public boolean isErrorEnabled() {
122: throw new UnsupportedOperationException();
123: }
124:
125: public boolean isFatalEnabled() {
126: throw new UnsupportedOperationException();
127: }
128:
129: public void trace(Object o) {
130: lines.add(o.toString());
131: }
132:
133: public void trace(Object o, Throwable t) {
134: throw new UnsupportedOperationException();
135: }
136:
137: public void info(Object o) {
138: lines.add(o.toString());
139: }
140:
141: public void info(Object o, Throwable t) {
142: throw new UnsupportedOperationException();
143: }
144:
145: public void warn(Object o) {
146: throw new UnsupportedOperationException();
147: }
148:
149: public void warn(Object o, Throwable t) {
150: throw new UnsupportedOperationException();
151: }
152:
153: public void error(Object o) {
154: throw new UnsupportedOperationException();
155: }
156:
157: public void error(Object o, Throwable t) {
158: throw new UnsupportedOperationException();
159: }
160:
161: public void fatal(Object o) {
162: throw new UnsupportedOperationException();
163: }
164:
165: public void fatal(Object o, Throwable t) {
166: throw new UnsupportedOperationException();
167: }
168: }
169: }
|