Skip to content

Commit

Permalink
refactor(java): ♻️ more refactoring done
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB committed Oct 18, 2023
1 parent 14d0446 commit 84c5175
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static java.lang.ThreadLocal.withInitial;
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.logging.log4j.LogManager.getLogger;

import java.util.HashMap;
Expand Down Expand Up @@ -60,10 +59,8 @@ public static synchronized void clearAllSessions () {
.stream ()
.toList ();
for (final var persona : sessionList) {
if (isNotEmpty (persona)) {
switchPersona (persona);
clearSession ();
}
switchPersona (persona);
clearSession ();
}
SESSION.remove ();
}
Expand All @@ -74,7 +71,7 @@ public static synchronized void clearAllSessions () {
public static void clearSession () {
LOGGER.info ("Clearing session for persona [{}]...", getCurrentPersona ());
final var session = SESSION.get ();
if (session.isEmpty ()) {
if (!isSessionCreated ()) {
throwError (SESSION_ALREADY_CLEARED);
}
getSession ().clearListeners ();
Expand All @@ -83,7 +80,7 @@ public static void clearSession () {
if (getSession ().getPlatformType () != WEB) {
ofNullable (getSession ().getServiceManager ()).ifPresent (ServiceManager::stopServer);
}
if (!session.isEmpty ()) {
if (isSessionCreated ()) {
session.remove (getCurrentPersona ());
}
CURRENT_PERSONA.remove ();
Expand Down Expand Up @@ -139,15 +136,23 @@ public static String getCurrentPersona () {
public static synchronized <D extends WebDriver> DriverSession<D> getSession () {
LOGGER.traceEntry ();
final var currentPersona = getCurrentPersona ();
final var session = SESSION.get ()
.containsKey (currentPersona);
if (!session) {
if (!isSessionCreated ()) {
throwError (SESSION_NOT_CREATED);
}
return LOGGER.traceExit ((DriverSession<D>) SESSION.get ()
.get (currentPersona));
}

/**
* Checks if the session has been started.
*
* @return true, if session has been created, else, false.
*/
public static boolean isSessionCreated () {
return SESSION.get ()
.containsKey (getCurrentPersona ());
}

/**
* Switch current session persona.
*
Expand Down Expand Up @@ -179,16 +184,10 @@ static <D extends WebDriver> void setDriver (final D driver) {
private static synchronized <D extends WebDriver> void setSession (final DriverSession<D> session) {
final var persona = getCurrentPersona ();
final var currentSession = SESSION.get ();
if (currentSession.isEmpty ()) {
final var sessionMap = new HashMap<String, DriverSession<? extends WebDriver>> ();
sessionMap.put (persona, session);
SESSION.set (sessionMap);
if (!isSessionCreated ()) {
currentSession.put (persona, session);
} else {
if (!currentSession.containsKey (persona)) {
currentSession.put (persona, session);
} else {
throwError (SESSION_ALREADY_CREATED, persona);
}
throwError (SESSION_ALREADY_CREATED, persona);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.github.wasiqb.boyka.manager.ParallelSession.getSession;

import com.github.wasiqb.boyka.exception.FrameworkError;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

/**
Expand All @@ -34,30 +35,32 @@
public class SessionTest {
private static final String PERSONA = "SessionTest";

/**
* Clear any open sessions.
*/
@AfterMethod
public void teardownMethod () {
clearAllSessions ();
}

/**
* Test duplicate clear session.
*/
@Test (description = "Test duplicate clear session", expectedExceptions = FrameworkError.class)
public void testDuplicateClearSession () {
try {
createSession (PERSONA, WEB, "test_local_chrome");
} finally {
clearSession ();
clearSession ();
}
createSession (PERSONA, WEB, "test_local_chrome");
clearSession ();
clearSession ();
}

/**
* Test Duplicate Session creation.
*/
@Test (description = "Test Duplicate Session creation", expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Session is already created for .SessionTest. persona...")
public void testDuplicateSessionCreation () {
try {
createSession (PERSONA, WEB, "test_local_chrome");
createSession (PERSONA, WEB, "test_local_chrome");
} finally {
clearAllSessions ();
}
createSession (PERSONA, WEB, "test_local_chrome");
createSession (PERSONA, WEB, "test_local_chrome");
clearSession ();
}

/**
Expand Down

0 comments on commit 84c5175

Please sign in to comment.