1 package com.xtremelabs.robolectric.util;
2 
3 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
4 import com.xtremelabs.robolectric.util.DatabaseConfig.CannotLoadDatabaseMapDriverException;
5 import com.xtremelabs.robolectric.util.DatabaseConfig.UsingDatabaseMap;
6 import org.junit.Assert;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 
10 import java.sql.Connection;
11 
12 /**
13  * the @UsingDatabaseMap integration test
14  * @author cvanvranken
15  *
16  */
17 @UsingDatabaseMap(NullDatabaseMap.class)
18 @RunWith(WithTestDefaultsRunner.class)
19 public class NullDatabaseMapTest {
20 
21 	@Test
CanChangeDatabaseMapUsingAnnotation()22 	public void CanChangeDatabaseMapUsingAnnotation() {
23 		Assert.assertTrue(DatabaseConfig.getDatabaseMap().getClass().getName()
24 				.equals(NullDatabaseMap.class.getName()));
25 		Assert.assertTrue(DatabaseConfig.getDatabaseMap().getConnectionString() == null);
26 		Assert.assertTrue(DatabaseConfig.getDatabaseMap()
27 				.getSelectLastInsertIdentity() == null);
28 		Assert.assertTrue(DatabaseConfig.getDatabaseMap().getDriverClassName() == "com.xtremelabs.robolectric.util.NullDatabaseMap");
29 	}
30 
31 	@Test
MapIsSetButIsNotLoaded()32 	public void MapIsSetButIsNotLoaded() {
33 		Assert.assertTrue(DatabaseConfig.getDatabaseMap().getClass().getName()
34 				.equals(NullDatabaseMap.class.getName()));
35 		// check that the map has been set but not loaded.
36 		Assert.assertFalse(DatabaseConfig.isMapLoaded());
37 	}
38 
39 	@Test
MapLoadsButConnectionFails()40 	public void MapLoadsButConnectionFails() {
41 		Assert.assertFalse(DatabaseConfig.isMapLoaded());
42 		Connection connection = null;
43 
44 		boolean expectedError = false;
45 
46 		try {
47 			connection = DatabaseConfig.getMemoryConnection();
48 			// we should never reach this,
49 			//since the connection should not actually be made
50 			Assert.assertTrue(false);
51 		} catch (CannotLoadDatabaseMapDriverException e) {
52 			//This error is expected.
53 			expectedError = true;
54 		}
55 		Assert.assertTrue(expectedError);
56 		Assert.assertTrue(connection == null);
57 		// driver should have loaded because the class name was valid,
58 		// even if the memoryConnectionString was invalid
59 		Assert.assertTrue(DatabaseConfig.isMapLoaded());
60 
61 	}
62 }
63