java/sql-java-prihlasovani/test/cz/frantovo/jaas/sql/SQLRealmTest.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 07 Feb 2012 18:03:31 +0100
changeset 7 46bb283a674d
parent 6 aff44e80f418
permissions -rw-r--r--
uživatelé a skupiny v SQL databázi, parametrizace, hellDesk: #11
     1 package cz.frantovo.jaas.sql;
     2 
     3 import com.sun.enterprise.security.auth.realm.BadRealmException;
     4 import com.sun.enterprise.security.auth.realm.NoSuchRealmException;
     5 import java.util.Properties;
     6 import javax.naming.NamingException;
     7 import org.junit.Before;
     8 import org.junit.Test;
     9 import static org.junit.Assert.*;
    10 
    11 /**
    12  *
    13  * @author fiki
    14  */
    15 public class SQLRealmTest {
    16 
    17 	private static final String JAAS_KONTEXT = "sqlRealm_123456";
    18 	private static final String JNDI_DS = "jdbc/test";
    19 	private static final String SQL_HESLO = "SELECT jmeno FROM uzivatel WHERE jmeno = ? AND heslo = ?";
    20 	private static final String SQL_SKUPINY_UŽIVATELE = "SELECT …";
    21 	private static final String SQL_SKUPINY_VŠECHNY = "SELECT …";
    22 	private static final String PARAMETR_NEPOVINNÝ = "nepovinný_parametr";
    23 	private static final String PARAMETR_NEPOVINNÝ_HODNOTA = "hodnota nepovinného parametru";
    24 	private SQLRealm realm;
    25 
    26 	@Before
    27 	public void setUp() throws BadRealmException, NoSuchRealmException, NamingException {
    28 		Properties parametry = new Properties();
    29 		
    30 		parametry.setProperty(SQLRealm.JAAS_CONTEXT_PARAM, JAAS_KONTEXT);
    31 		parametry.setProperty(SQLRealm.PARAM_JNDI, JNDI_DS);
    32 		parametry.setProperty(SQLRealm.PARAM_SQL_HESLO, SQL_HESLO);
    33 		parametry.setProperty(SQLRealm.PARAM_SQL_SKUPINY_UŽIVATELE, SQL_SKUPINY_UŽIVATELE);
    34 		parametry.setProperty(SQLRealm.PARAM_SQL_SKUPINY_VŠECHNY, SQL_SKUPINY_VŠECHNY);
    35 
    36 		parametry.setProperty(SQLRealm.PARAM_NETESTOVAT_SPOJENÍ, Boolean.TRUE.toString());
    37 
    38 		realm = new SQLRealm();
    39 		realm.init(parametry);
    40 
    41 		realm.setProperty(PARAMETR_NEPOVINNÝ, PARAMETR_NEPOVINNÝ_HODNOTA);
    42 	}
    43 
    44 	@Test
    45 	public void testJaasKontext() {
    46 		String kontext = realm.getJAASContext();
    47 		System.out.println("jaasContext = " + kontext);
    48 		assertEquals("Kontext je jiný, než jsme nastavili v init parametrech.", JAAS_KONTEXT, kontext);
    49 	}
    50 
    51 	@Test
    52 	public void testGetAuthType() {
    53 		String authType = realm.getAuthType();
    54 		System.out.println("authType = " + authType);
    55 		assertTrue("authType musí být nenulový", authType != null);
    56 		assertTrue("authType musí být neprázdný", authType.trim().length() > 0);
    57 	}
    58 
    59 	@Test
    60 	public void testGetProperty() {
    61 		assertEquals("Neplatná hodnota parametru JAAS_KONTEXT", JAAS_KONTEXT, realm.getProperty(SQLRealm.JAAS_CONTEXT_PARAM));
    62 		assertEquals("Neplatná hodnota parametru SQL_HESLO", SQL_HESLO, realm.getProperty(SQLRealm.PARAM_SQL_HESLO));
    63 		assertEquals("Neplatná hodnota parametru PARAMETR_NEPOVINNÝ", PARAMETR_NEPOVINNÝ_HODNOTA, realm.getProperty(PARAMETR_NEPOVINNÝ));
    64 
    65 	}
    66 }