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.factories;
18  
19  import static org.testng.Assert.assertEquals;
20  import static org.testng.Assert.assertNotNull;
21  
22  import java.util.List;
23  
24  import net.ep.db4o.activator.GenericProxyActivationTest;
25  import net.ep.db4o.javassist.testclasses.BrownBag;
26  
27  import org.testng.annotations.Test;
28  
29  import com.db4o.ObjectContainer;
30  
31  @Test
32  public class EnhancingFactoryTest extends GenericProxyActivationTest {
33  	
34  	private ObjectContainerProvider provider() {
35  		return new ObjectContainerProvider() {
36  
37  			public ObjectContainer db() {
38  				return db;
39  			}};
40  	}
41  	
42  	public void brownBag() {
43  		BasicFactories facts = new PersistentFactories(provider());
44  		BrownBagFactory fac = facts.createFactory(BrownBagFactory.class);
45  		BrownBag bag = fac.createBrownbag("erik");
46  		assertNotNull(bag);
47  		assertEquals(bag.getOwner(), "erik");
48  	}
49  	
50  	@Test(expectedExceptions=InvalidFactoryException.class)
51  	public void brownBagException() {
52  		BasicFactories facts = new PersistentFactories(provider());
53  		InvalidBrownBagFactory fac = facts.createFactory(InvalidBrownBagFactory.class);
54  		BrownBag bag = fac.createBrownbag();
55  	}	
56  	
57  	public void brownBag2() {
58  		BasicFactories facts = new PersistentFactories(provider());
59  		BrownBagFactory2 fac = facts.createFactory(BrownBagFactory2.class);
60  		BrownBag bag = fac.createBrownbag("erik","hello");
61  		assertNotNull(bag);
62  		assertEquals(bag.getOwner(), "erik");
63  		BrownBag bag2 = fac.createBrownbag("erik");
64  		assertNotNull(bag2);
65  		assertEquals(bag2.getOwner(), "erik");
66  		BrownBag ex = new BrownBag("erik","hello");
67  		List<BrownBag> bags = db.queryByExample(ex);
68  		assertEquals(bags.size(), 2);
69  		BrownBag ex2 = new BrownBag("olina");
70  		List<BrownBag> bags2 = db.queryByExample(ex2);
71  		assertEquals(bags2.size(), 0);
72  	}
73  
74  	@Override
75  	protected void saveData(ObjectContainer db, int instances) {
76  		// TODO Auto-generated method stub
77  		
78  	}
79  
80  }