<commit_before>/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfpush.adm;

import android.content.Context;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onepf.opfutils.OPFPreferences;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.lang.reflect.Field;

/**
 * Created by antonpp on 02.03.15.
 */
@Config(emulateSdk = 18, manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class PreferencesProviderTest {

    private static final String ADM_POSTFIX = "adm";
    private static final String KEY_APP_VERSION = "app_version";

    private Context ctx;
    private PreferencesProvider preferencesProvider;
    private OPFPreferences preferences;

    @Before
    public void setup() {
        ctx = Robolectric.application.getApplicationContext();
        preferencesProvider = PreferencesProvider.getInstance(ctx);
        preferences = new OPFPreferences(ctx, ADM_POSTFIX);
    }

    @After
    public void erasePreferencesProviderInstance() {
        Field instanceField;
        synchronized (PreferencesProvider.class) {
            try {
                instanceField = PreferencesProvider.class.getDeclaredField("instance");
                instanceField.setAccessible(true);
                instanceField.set(null, null);

            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testChangeApplicationVersion() {
        final int appVersion1 = 1;
        final int appVersion2 = 2;
        final String registrationIdBefore = "id1";

        // save registration Id with one appVersion
        preferences.put(KEY_APP_VERSION, appVersion1);
        preferencesProvider.saveRegistrationId(registrationIdBefore);

        // get registration Id with another appVersion
        preferences.put(KEY_APP_VERSION, appVersion2);
        final String registrarionIdAfter = preferencesProvider.getRegistrationId();

        Assert.assertNotSame(registrationIdBefore, registrarionIdAfter);
    }

}<commit_msg>Updated PreferencesProviderTest for adm<commit_after>/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfpush.adm;

import android.content.Context;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onepf.opfutils.OPFPreferences;
import org.onepf.opfutils.OPFUtils;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.lang.reflect.Field;
import java.util.Random;

/**
 * Created by antonpp on 02.03.15.
 */
@Config(emulateSdk = 18, manifest = Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class PreferencesProviderTest extends Assert {

    private static final String KEY_REGISTRATION_ID = "registration_id";
    private static final String KEY_APP_VERSION = "app_version";
    private static final String KEY_AUTHENTICATION_FAILED_FLAG = "authentication_failed_flag";
    private static final String ADM_POSTFIX = "adm";

    private static final Random RND = new Random();
    
    private static final int NUM_TESTS = 100;

    private Context ctx;
    private PreferencesProvider preferencesProvider;
    private OPFPreferences preferences;

    @Before
    public void setup() {
        ctx = Robolectric.application.getApplicationContext();
        preferencesProvider = PreferencesProvider.getInstance(ctx);
        preferences = new OPFPreferences(ctx, ADM_POSTFIX);
    }

    private void postSetupUpdateAppVersion() {
        preferences.put(KEY_APP_VERSION, OPFUtils.getAppVersion(ctx));
    }
    
    @After
    public void erasePreferencesProviderInstance() {
        Field instanceField;
        synchronized (PreferencesProvider.class) {
            try {
                instanceField = PreferencesProvider.class.getDeclaredField("instance");
                instanceField.setAccessible(true);
                instanceField.set(null, null);

            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testChangeApplicationVersion() {
        final int appVersion1 = 1;
        final int appVersion2 = 2;
        final String registrationIdBefore = "id1";

        // save registration Id with one appVersion
        preferences.put(KEY_APP_VERSION, appVersion1);
        preferencesProvider.saveRegistrationId(registrationIdBefore);

        // get registration Id with another appVersion
        preferences.put(KEY_APP_VERSION, appVersion2);
        final String registrarionIdAfter = preferencesProvider.getRegistrationId();

        assertNotSame(registrationIdBefore, registrarionIdAfter);
    }

    @Test
    public void testGetRegistrationId() {
        postSetupUpdateAppVersion();
        
        String expected;
        for (int i = 0; i < NUM_TESTS; ++i) {
            expected = String.format("id%d", i);
            preferences.put(KEY_REGISTRATION_ID, expected);
            assertEquals(expected, preferencesProvider.getRegistrationId());
        }
    }
    
    @Test
    public void testGetRegistrationIdNullCase() {
        postSetupUpdateAppVersion();
        
        // registration id must be null if it is not set
        assertNull(preferencesProvider.getRegistrationId());
        
        // registration Id after reset must be null
        preferencesProvider.saveRegistrationId("notNullRegistrationId");
        preferencesProvider.reset();
        assertNull(preferencesProvider.getRegistrationId());
    }
    
    @Test
    public void testSaveRegistrationId() {
        
        // not null registration id should be saved
        String expected;
        for (int i = 0; i < NUM_TESTS; ++i) {
            expected = String.format("id%d", i);
            preferencesProvider.saveRegistrationId(expected);
            assertEquals(expected, preferences.getString(KEY_REGISTRATION_ID));
        }
        
        // null registration id must erase previous value
        preferencesProvider.saveRegistrationId(null);
        assertFalse(preferences.contains(KEY_REGISTRATION_ID));
    }
    
    @Test
    public void testReset() {
        postSetupUpdateAppVersion();
        
        for (int i = 0; i < NUM_TESTS; ++i) {
            preferences.put(KEY_APP_VERSION, RND.nextInt());
            preferences.put(KEY_REGISTRATION_ID, String.format("regId%d", RND.nextInt()));
            preferences.put(KEY_AUTHENTICATION_FAILED_FLAG, RND.nextBoolean());
            preferencesProvider.reset();
            assertNull(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));
            assertNull(preferences.getString(KEY_REGISTRATION_ID));
            assertNull(preferences.getInt(KEY_APP_VERSION));
        }
    }
    
    @Test
    public void testRemoveAuthenticationFailedFlag() {
        postSetupUpdateAppVersion();
        
        preferences.put(KEY_AUTHENTICATION_FAILED_FLAG, true);
        preferencesProvider.removeAuthenticationFailedFlag();
        assertNull(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));

        preferences.put(KEY_AUTHENTICATION_FAILED_FLAG, false);
        preferencesProvider.removeAuthenticationFailedFlag();
        assertNull(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));
    }
    
    @Test
    public void testSaveAuthenticationFailedFlag() {
        postSetupUpdateAppVersion();

        preferencesProvider.saveAuthenticationFailedFlag();
        assertTrue(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));
        
        preferences.put(KEY_AUTHENTICATION_FAILED_FLAG, false);
        preferencesProvider.saveAuthenticationFailedFlag();
        assertTrue(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));

        preferences.remove(KEY_AUTHENTICATION_FAILED_FLAG);
        preferencesProvider.saveAuthenticationFailedFlag();
        assertTrue(preferences.getBoolean(KEY_AUTHENTICATION_FAILED_FLAG));
    }
    
    @Test
    public void testIsAuthenticationFailed() {
        postSetupUpdateAppVersion();
        
        assertFalse(preferencesProvider.isAuthenticationFailed());
        
        preferencesProvider.saveAuthenticationFailedFlag();
        assertTrue(preferencesProvider.isAuthenticationFailed());
        
        preferencesProvider.removeAuthenticationFailedFlag();
        assertFalse(preferencesProvider.isAuthenticationFailed());
    }
}
{'fraction_non_alphanumeric': 0.0801631820499745, 'fraction_numerical': 0.006119326874043855, 'mean_word_length': 3.214009454232918, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501735', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2514, 'n_words': 605}