<commit_before>/*
 * Copyright (C) 2017 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 */
package org.openecard.gui.android.eac;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import mockit.Mocked;
import mockit.Tested;
import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

/**
 *
 * @author <NAME>
 */
public class EacGuiServiceTest {
    
    @Mocked
    EacGui.Stub stub;
    
    @Mocked
    Service service;
    
    @BeforeTest
    public void setUpSuite() {
	EacGuiService.prepare();
    }
    
    @AfterMethod
    public void tearDown() {
	EacGuiService.prepare();
    }
    
    @Test
    public void canDeliverCorrectGui(@Mocked EacGuiImpl input) {
	EacGuiService.setGuiImpl(input);
    }
    
    @Test(expectedExceptions = {IllegalStateException.class})
    public void canDeliverGuiMultipleTimes(@Mocked EacGuiImpl input1, @Mocked EacGuiImpl input2) throws IllegalStateException {
	EacGuiService.setGuiImpl(input1);
	EacGuiService.setGuiImpl(input2);
    }
    
    @Test
    public void GivenNoGuiImplSetThenBindingWaitsForever(@Tested final EacGuiService sut, @Mocked final Intent input)  {
	Future<IBinder> future = callBindAsync(sut, input);
	try {
	    //wait 1 seconds for the task to complete.
	    future.get(1000, TimeUnit.MILLISECONDS);
	    
	    Assert.fail("The call to onBind(...) is supposed to wait forever and not terminate.");
	}
	catch (InterruptedException | ExecutionException | TimeoutException e) {
	    // Pass
	}
    }

    @Test
    public void BindingWaitsForGuiAssignment(@Tested final EacGuiService sut, @Mocked final Intent inputIntent, @Mocked EacGuiImpl inputGui) throws InterruptedException, ExecutionException  {
	
	Future<IBinder> future = callBindAsync(sut, inputIntent);
	TimeUnit.MILLISECONDS.sleep(2);
	EacGuiService.setGuiImpl(inputGui);
	IBinder result = future.get();
	
	assertEquals(result, inputGui);
    }

    private Future<IBinder> callBindAsync(final EacGuiService sut, final Intent input) {
	ExecutorService exec = Executors.newSingleThreadExecutor();
	Future<IBinder> future = exec.submit(new Callable<IBinder>(){
	    @Override
	    public IBinder call() throws Exception {
		return sut.onBind(input);
	    }
	});
	exec.shutdown();
	return future;
    }
}<commit_msg>Rename tests for clarity<commit_after>/*
 * Copyright (C) 2017 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 */
package org.openecard.gui.android.eac;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import mockit.Mocked;
import mockit.Tested;
import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

/**
 *
 * @author <NAME>
 */
public class EacGuiServiceTest {

    @Mocked
    EacGui.Stub stub;

    @Mocked
    Service service;

    @BeforeTest
    public void setUpSuite() {
	EacGuiService.prepare();
    }

    @AfterMethod
    public void tearDown() {
	EacGuiService.prepare();
    }

    @Test
    public void canAssignGuiSuccessfully(@Mocked EacGuiImpl input) {
	EacGuiService.setGuiImpl(input);
    }

    @Test(expectedExceptions = {IllegalStateException.class})
    public void cannotDeliverGuiMultipleTimes(@Mocked EacGuiImpl input1, @Mocked EacGuiImpl input2) throws IllegalStateException {
	EacGuiService.setGuiImpl(input1);
	EacGuiService.setGuiImpl(input2);
    }

    @Test
    public void givenGuiIsNotAssignedThenBindingWaitsForever(@Tested final EacGuiService sut, @Mocked final Intent input) {
	Future<IBinder> future = callBindAsync(sut, input);
	try {
	    //wait 1 seconds for the task to complete.
	    future.get(1000, TimeUnit.MILLISECONDS);

	    Assert.fail("The call to onBind(...) is supposed to wait forever and not terminate.");
	} catch (InterruptedException | ExecutionException | TimeoutException e) {
	    // Pass
	}
    }

    @Test
    public void givenBindingStartsBeforeGuiAssignmentThenBindingWaitsForGuiAssignment(@Tested final EacGuiService sut,
	    @Mocked final Intent inputIntent,
	    @Mocked EacGuiImpl inputGui) throws InterruptedException, ExecutionException {

	Future<IBinder> future = callBindAsync(sut, inputIntent);
	TimeUnit.MILLISECONDS.sleep(2);
	EacGuiService.setGuiImpl(inputGui);
	IBinder result = future.get();

	assertEquals(result, inputGui);
    }

    private Future<IBinder> callBindAsync(final EacGuiService sut, final Intent input) {
	ExecutorService exec = Executors.newSingleThreadExecutor();
	Future<IBinder> future = exec.submit(new Callable<IBinder>() {
	    @Override
	    public IBinder call() throws Exception {
		return sut.onBind(input);
	    }
	});
	exec.shutdown();
	return future;
    }
}
{'fraction_non_alphanumeric': 0.08994396933058095, 'fraction_numerical': 0.005308168681804777, 'mean_word_length': 4.221709006928407, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3319092', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1985, 'n_words': 573}