/*
 *  Copyright 2011 Rudolf Biczok
 *
 *  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.pamther;

import javax.security.auth.login.LoginException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.pamther.test.TempUser;

/**
 * The class {@link PAMService} should ...
 * 
 * @author <a href="https://github.com/RCBiczok">Rudolf Biczok</a>
 */
public class TransactionTest {

	private static final String NAME = "pamtest";
	private static final String OLD_PASSWORD = "mop";
	private static final String NEW_PASSWORD = "new_mop";
	private static TempUser user;
	
	private static DefaultCallbackHandler handler;

	@BeforeClass
	public static void createUser() throws LoginException {
		TransactionTest.user = new TempUser(TransactionTest.NAME,
				TransactionTest.OLD_PASSWORD);
		TransactionTest.user.create();
		handler = new DefaultCallbackHandler();
		handler.setName(TransactionTest.NAME);
		handler.setOldPassword(TransactionTest.OLD_PASSWORD.toCharArray());
		handler.setNewPassword(TransactionTest.NEW_PASSWORD.toCharArray());
	}

	@AfterClass
	public static void deleteUser() {
		TransactionTest.user.delete();
		TransactionTest.user = null;
	}

	@Test
	public void haveAService() throws LoginException {
		Transaction pam = new Transaction("login", TransactionTest.NAME, handler);
		Assert.assertEquals("login", pam.getService());
		pam.setService("su");
		Assert.assertEquals("su", pam.getService());
		pam.close();
	}

	@Test
	public void login() throws LoginException {
		Transaction pam = new Transaction("login", TransactionTest.NAME, handler);
		pam.authenticate();
		pam.verify();
		pam.close();
		System.gc();
	}

	@Test
	public void setcred() throws LoginException {
		Transaction pam = new Transaction("login", TransactionTest.NAME, handler);
		pam.authenticate();
		pam.verify();
		pam.chauthtok();
		try {
		pam.authenticate();
		}
		catch (LoginException e) {
			return;
		}
		finally {
			pam.close();
		}
		Assert.fail();
	}
	
//	@Test
//	public void beCollectableByGC() throws LoginException {
//		Transaction pam = new Transaction("login", TransactionTest.NAME, handler);
//		pam.authenticate();
//		pam.verify();
//		pam.close();
//		
//		pam = new Transaction("login", TransactionTest.NAME, handler);
//		pam.authenticate();
//		pam.verify();
//		pam.close();
//		pam = null;
//		
//		System.gc();
//	}
}

@@ -1242,21 +1242,16 @@
 id creat
-eUser
 () throw
@@ -1666,13 +1666,8 @@
 elet
-eUser
 () %7B
@@ -1800,35 +1800,43 @@
 %7B%0A%09%09Transaction 
-pam
+transaction
  = new Transacti
@@ -1910,19 +1910,27 @@
 login%22, 
-pam
+transaction
 .getServ
@@ -1939,19 +1939,27 @@
 e());%0A%09%09
-pam
+transaction
 .setServ
@@ -1997,19 +1997,27 @@
 s(%22su%22, 
-pam
+transaction
 .getServ
@@ -2022,27 +2022,35 @@
 rvice());%0A%09%09
-pam
+transaction
 .close();%0A%09%7D
@@ -2109,35 +2109,43 @@
 %7B%0A%09%09Transaction 
-pam
+transaction
  = new Transacti
@@ -2182,35 +2182,43 @@
 ME, handler);%0A%09%09
-pam
+transaction
 .authenticate();
@@ -2212,35 +2212,43 @@
 thenticate();%0A%09%09
-pam
+transaction
 .verify();%0A%09%09pam
@@ -2244,19 +2244,27 @@
 fy();%0A%09%09
-pam
+transaction
 .close()
@@ -2344,35 +2344,43 @@
 %7B%0A%09%09Transaction 
-pam
+transaction
  = new Transacti
@@ -2425,19 +2425,27 @@
 ler);%0A%09%09
-pam
+transaction
 .authent
@@ -2455,19 +2455,27 @@
 te();%0A%09%09
-pam
+transaction
 .verify(
@@ -2479,19 +2479,27 @@
 fy();%0A%09%09
-pam
+transaction
 .chautht
@@ -2514,19 +2514,27 @@
 try %7B%0A%09%09
-pam
+transaction
 .authent
@@ -2605,19 +2605,27 @@
 ly %7B%0A%09%09%09
-pam
+transaction
 .close()

Rename some local fields