1   /**
2    * Copyright (C) 2009 Erik Putrycz <erik.putrycz@gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.ep.db4o.activator;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import net.ep.db4o.javassist.ActivateOnAllMethods;
23  import net.ep.db4o.javassist.AnnotationPersistencePolicy;
24  
25  import org.testng.annotations.AfterClass;
26  
27  import com.db4o.Db4o;
28  import com.db4o.ObjectContainer;
29  import com.db4o.config.Configuration;
30  import com.db4o.config.QueryEvaluationMode;
31  
32  public abstract class GenericProxyTest {
33  	
34  	protected Configuration configureTA() {
35  		Configuration configuration = Db4o.newConfiguration();
36  		configuration.queries().evaluationMode(QueryEvaluationMode.LAZY);
37  		configuration.add(ProxyActivationSupport.create().withActivationPolicy(new ActivateOnAllMethods()).withPersistPolicy(new AnnotationPersistencePolicy()));
38  		return configuration;
39  	}
40  
41  	protected Configuration configurePA() {
42  		Configuration configuration = Db4o.newConfiguration();
43  		//configuration.activationDepth(0);
44  		configuration.queries().evaluationMode(QueryEvaluationMode.LAZY);
45  		configuration.add(ProxyActivationSupport.create().withActivationPolicy(new ActivateOnAllMethods()).withPersistPolicy(new AnnotationPersistencePolicy()));
46  //		configuration.diagnostic().addListener(new DiagnosticToConsole());
47  //		configuration.messageLevel(2);
48  		return configuration;
49  	}	
50  	
51  	protected File tmpFile;
52  	
53  	protected ObjectContainer db;
54  	
55  	protected ObjectContainer openDB(Configuration conf) throws IOException {
56  		if (tmpFile == null)
57  			tmpFile = File.createTempFile("db4o", ".db");
58  		return Db4o.openFile(conf, tmpFile.getAbsolutePath());
59  	}
60  	
61  	protected void openDBNoTA() throws IOException {
62  		if (db != null)
63  			closeDB();
64  		db = openDB(Db4o.newConfiguration());
65  	}
66  	
67  	protected void openDBwTA() throws IOException {
68  		if (db != null)
69  			closeDB();
70  		db = openDB(configurePA());
71  	}
72  
73  	protected void openDBwPA() throws IOException {
74  		if (db != null)
75  			closeDB();
76  		db = openDB(configurePA());
77  	}	
78  	
79  	public void createDBIfNecessary(int instances) {
80  		if (tmpFile != null)
81  			return;
82  	
83  		Configuration conf = Db4o.newConfiguration();
84  		configure(conf);
85  		try {
86  			openDBNoTA();
87  			System.out.println("creating db " + tmpFile.getAbsolutePath());
88  			saveData(db, instances);			
89  			db.commit();
90  			db.close();
91  		} catch (IOException e) {
92  			throw new RuntimeException(e);
93  		}
94  	
95  	}
96  
97  	protected abstract void saveData(ObjectContainer db, int instances);
98  
99  	protected void configure(Configuration conf) {
100 	}
101 
102 	public void closeDB() {		
103 		System.out.println("closing database");
104 		db.close();
105 		db = null;
106 	}
107 	
108 	@AfterClass(alwaysRun=true)
109 	public void cleanup() {
110 		if (tmpFile != null && !tmpFile.delete()) {
111 			tmpFile.deleteOnExit();
112 		}
113 		tmpFile = null;
114 	}
115 
116 }