|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2014 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.test15.ormlite; |
| 17 | + |
| 18 | +import static org.fest.assertions.api.Assertions.assertThat; |
| 19 | +import static org.fest.reflect.core.Reflection.field; |
| 20 | +import static org.fest.reflect.core.Reflection.staticField; |
| 21 | + |
| 22 | +import org.fest.reflect.field.Invoker; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.robolectric.Robolectric; |
| 27 | +import org.robolectric.RobolectricTestRunner; |
| 28 | +import org.robolectric.util.ActivityController; |
| 29 | + |
| 30 | +import com.j256.ormlite.android.apptools.OpenHelperManager; |
| 31 | +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; |
| 32 | + |
| 33 | +@RunWith(RobolectricTestRunner.class) |
| 34 | +public class OrmLiteOnDestroyTest { |
| 35 | + |
| 36 | + private ActivityController<OrmLiteOnDestroyActivity_> controller; |
| 37 | + private Invoker<Integer> instanceCountInvoker; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setup() throws NoSuchFieldException, IllegalAccessException { |
| 41 | + staticField("helper").ofType(OrmLiteSqliteOpenHelper.class).in(OpenHelperManager.class).set(null); |
| 42 | + staticField("helperClass").ofType(Class.class).in(OpenHelperManager.class).set(null); |
| 43 | + staticField("wasClosed").ofType(boolean.class).in(OpenHelperManager.class).set(false); |
| 44 | + |
| 45 | + instanceCountInvoker = staticField("instanceCount").ofType(int.class).in(OpenHelperManager.class); |
| 46 | + instanceCountInvoker.set(0); |
| 47 | + |
| 48 | + controller = Robolectric.buildActivity(OrmLiteOnDestroyActivity_.class).create(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void helper_is_destroyed() throws Exception { |
| 53 | + controller.destroy(); |
| 54 | + |
| 55 | + Object databaseHelper_ = field("databaseHelper_").ofType(OrmLiteSqliteOpenHelper.class).in(controller.get()).get(); |
| 56 | + assertThat(databaseHelper_).isNull(); |
| 57 | + assertThat(instanceCountInvoker.get()).isEqualTo(0); |
| 58 | + } |
| 59 | +} |
0 commit comments