JavaFX FileChooser in swing - swing

I think everyone can agree that JFileChooser is really poop. So I was looking for an alternative and found out that JavaFX has got a great FileChooser class. So now the obvious question: how can I embed that neat FileChooser into my Swing application?
Needless to say, I did some research before I posted this, and this is what I found so far: link to a Reddit post.
The code of that JavaFXFileDialog class is very interesting, but it does not close when I exit my application (JavaFX seems to continue running in the background). Also I am missing some fields I can pass to the FileChooser like the path to set default directory. And I don't like how it is static.
I am grateful for any input.

The code of that dialog has multiple problems besides the ones you mention. For example, it doesn't handle the situation when JavaFX platform shuts down right after isJavaFXStillUsable() is called, but before the call to Platform.runLater(), which will still make it hang forever. I don't like that huge synchronized block either, although there don't seem to be any real problems with that. I also don't get why "the stupid synchronization object had to be a field" - each invocation of chooseFileWithJavaFXDialog() is independent of each other, so it could just as well use a local final lock (even that array would do fine).
The right way to make JVM exit correctly is to call Platform.exit() when you are shutting down your application (perhaps in windowClosed() of your main window). You need to do this manually because the chooser class has no idea whether you need JavaFX any more or not, and there is no way to restart it once it has been shut down.
That code inspired me to develop a utility class for calling just about any code in the JavaFX event thread, and get the result back to the calling thread, handling various exceptions and JavaFX states nicely:
/**
* A utility class to execute a Callable synchronously
* on the JavaFX event thread.
*
* #param <T> the return type of the callable
*/
public class SynchronousJFXCaller<T> {
private final Callable<T> callable;
/**
* Constructs a new caller that will execute the provided callable.
*
* The callable is accessed from the JavaFX event thread, so it should either
* be immutable or at least its state shouldn't be changed randomly while
* the call() method is in progress.
*
* #param callable the action to execute on the JFX event thread
*/
public SynchronousJFXCaller(Callable<T> callable) {
this.callable = callable;
}
/**
* Executes the Callable.
* <p>
* A specialized task is run using Platform.runLater(). The calling thread
* then waits first for the task to start, then for it to return a result.
* Any exception thrown by the Callable will be rethrown in the calling
* thread.
* </p>
* #param startTimeout time to wait for Platform.runLater() to <em>start</em>
* the dialog-showing task
* #param startTimeoutUnit the time unit of the startTimeout argument
* #return whatever the Callable returns
* #throws IllegalStateException if Platform.runLater() fails to start
* the task within the given timeout
* #throws InterruptedException if the calling (this) thread is interrupted
* while waiting for the task to start or to get its result (note that the
* task will still run anyway and its result will be ignored)
*/
public T call(long startTimeout, TimeUnit startTimeoutUnit)
throws Exception {
final CountDownLatch taskStarted = new CountDownLatch(1);
// Can't use volatile boolean here because only finals can be accessed
// from closures like the lambda expression below.
final AtomicBoolean taskCancelled = new AtomicBoolean(false);
// a trick to emulate modality:
final JDialog modalBlocker = new JDialog();
modalBlocker.setModal(true);
modalBlocker.setUndecorated(true);
modalBlocker.setOpacity(0.0f);
modalBlocker.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
final CountDownLatch modalityLatch = new CountDownLatch(1);
final FutureTask<T> task = new FutureTask<T>(() -> {
synchronized (taskStarted) {
if (taskCancelled.get()) {
return null;
} else {
taskStarted.countDown();
}
}
try {
return callable.call();
} finally {
// Wait until the Swing thread is blocked in setVisible():
modalityLatch.await();
// and unblock it:
SwingUtilities.invokeLater(() ->
modalBlocker.setVisible(false));
}
});
Platform.runLater(task);
if (!taskStarted.await(startTimeout, startTimeoutUnit)) {
synchronized (taskStarted) {
// the last chance, it could have been started just now
if (!taskStarted.await(0, TimeUnit.MILLISECONDS)) {
// Can't use task.cancel() here because it would
// interrupt the JavaFX thread, which we don't own.
taskCancelled.set(true);
throw new IllegalStateException("JavaFX was shut down"
+ " or is unresponsive");
}
}
}
// a trick to notify the task AFTER we have been blocked
// in setVisible()
SwingUtilities.invokeLater(() -> {
// notify that we are ready to get the result:
modalityLatch.countDown();
});
modalBlocker.setVisible(true); // blocks
modalBlocker.dispose(); // release resources
try {
return task.get();
} catch (ExecutionException ex) {
Throwable ec = ex.getCause();
if (ec instanceof Exception) {
throw (Exception) ec;
} else if (ec instanceof Error) {
throw (Error) ec;
} else {
throw new AssertionError("Unexpected exception type", ec);
}
}
}
}
The only part that worries me is that modality trick. It could very well work
without it (just remove any code that references modalBlocker and modalityHatch), but then the Swing part of the application won't just stop responding to the user input (which is what we need), but also will freeze,
stopping updates, progress bars and so on, which is not so nice. What worries me about this particular trick is that the invisible dialog may be not so invisible in some L&Fs, or cause other unwanted glitches.
I deliberately didn't include any initialization or shutdown code because I believe it doesn't belong there. I would just do new JFXPanel() in main() and Platform.exit() wherever I perform other shutdown tasks.
Using this class, calling for a FileChooser is easy:
/**
* A utility class that summons JavaFX FileChooser from the Swing EDT.
* (Or anywhere else for that matter.) JavaFX should be initialized prior to
* using this class (e. g. by creating a JFXPanel instance). It is also
* recommended to call Platform.setImplicitExit(false) after initialization
* to ensure that JavaFX platform keeps running. Don't forget to call
* Platform.exit() when shutting down the application, to ensure that
* the JavaFX threads don't prevent JVM exit.
*/
public class SynchronousJFXFileChooser {
private final Supplier<FileChooser> fileChooserFactory;
/**
* Constructs a new file chooser that will use the provided factory.
*
* The factory is accessed from the JavaFX event thread, so it should either
* be immutable or at least its state shouldn't be changed randomly while
* one of the dialog-showing method calls is in progress.
*
* The factory should create and set up the chooser, for example,
* by setting extension filters. If there is no need to perform custom
* initialization of the chooser, FileChooser::new could be passed as
* a factory.
*
* Alternatively, the method parameter supplied to the showDialog()
* function can be used to provide custom initialization.
*
* #param fileChooserFactory the function used to construct new choosers
*/
public SynchronousJFXFileChooser(Supplier<FileChooser> fileChooserFactory) {
this.fileChooserFactory = fileChooserFactory;
}
/**
* Shows the FileChooser dialog by calling the provided method.
*
* Waits for one second for the dialog-showing task to start in the JavaFX
* event thread, then throws an IllegalStateException if it didn't start.
*
* #see #showDialog(java.util.function.Function, long, java.util.concurrent.TimeUnit)
* #param <T> the return type of the method, usually File or List<File>
* #param method a function calling one of the dialog-showing methods
* #return whatever the method returns
*/
public <T> T showDialog(Function<FileChooser, T> method) {
return showDialog(method, 1, TimeUnit.SECONDS);
}
/**
* Shows the FileChooser dialog by calling the provided method. The dialog
* is created by the factory supplied to the constructor, then it is shown
* by calling the provided method on it, then the result is returned.
* <p>
* Everything happens in the right threads thanks to
* {#link SynchronousJFXCaller}. The task performed in the JavaFX thread
* consists of two steps: construct a chooser using the provided factory
* and invoke the provided method on it. Any exception thrown during these
* steps will be rethrown in the calling thread, which shouldn't
* normally happen unless the factory throws an unchecked exception.
* </p>
* <p>
* If the calling thread is interrupted during either the wait for
* the task to start or for its result, then null is returned and
* the Thread interrupted status is set.
* </p>
* #param <T> return type (usually File or List<File>)
* #param method a function that calls the desired FileChooser method
* #param timeout time to wait for Platform.runLater() to <em>start</em>
* the dialog-showing task (once started, it is allowed to run as long
* as needed)
* #param unit the time unit of the timeout argument
* #return whatever the method returns
* #throws IllegalStateException if Platform.runLater() fails to start
* the dialog-showing task within the given timeout
*/
public <T> T showDialog(Function<FileChooser, T> method,
long timeout, TimeUnit unit) {
Callable<T> task = () -> {
FileChooser chooser = fileChooserFactory.get();
return method.apply(chooser);
};
SynchronousJFXCaller<T> caller = new SynchronousJFXCaller<>(task);
try {
return caller.call(timeout, unit);
} catch (RuntimeException | Error ex) {
throw ex;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return null;
} catch (Exception ex) {
throw new AssertionError("Got unexpected checked exception from"
+ " SynchronousJFXCaller.call()", ex);
}
}
/**
* Shows a FileChooser using FileChooser.showOpenDialog().
*
* #see #showDialog(java.util.function.Function, long, java.util.concurrent.TimeUnit)
* #return the return value of FileChooser.showOpenDialog()
*/
public File showOpenDialog() {
return showDialog(chooser -> chooser.showOpenDialog(null));
}
/**
* Shows a FileChooser using FileChooser.showSaveDialog().
*
* #see #showDialog(java.util.function.Function, long, java.util.concurrent.TimeUnit)
* #return the return value of FileChooser.showSaveDialog()
*/
public File showSaveDialog() {
return showDialog(chooser -> chooser.showSaveDialog(null));
}
/**
* Shows a FileChooser using FileChooser.showOpenMultipleDialog().
*
* #see #showDialog(java.util.function.Function, long, java.util.concurrent.TimeUnit)
* #return the return value of FileChooser.showOpenMultipleDialog()
*/
public List<File> showOpenMultipleDialog() {
return showDialog(chooser -> chooser.showOpenMultipleDialog(null));
}
public static void main(String[] args) {
javafx.embed.swing.JFXPanel dummy = new javafx.embed.swing.JFXPanel();
Platform.setImplicitExit(false);
try {
SynchronousJFXFileChooser chooser = new SynchronousJFXFileChooser(() -> {
FileChooser ch = new FileChooser();
ch.setTitle("Open any file you wish");
return ch;
});
File file = chooser.showOpenDialog();
System.out.println(file);
// this will throw an exception:
chooser.showDialog(ch -> ch.showOpenDialog(null), 1, TimeUnit.NANOSECONDS);
} finally {
Platform.exit();
}
}
}
Using this class, you may either initialize your chooser in the factory method, or, if you need to perform different initialization for each call, you could pass a custom method to showDialog() instead:
System.out.println(chooser.showDialog(ch -> {
ch.setInitialDirectory(new File(System.getProperty("user.home")));
return ch.showOpenDialog(null);
}));

Related

How to get real-time data on new data insert into MySQL db

I am using Laravel and pusher. Pusher is working good. But I want to know how can I get data when I will insert new data into database?
Process is if someone push/insert data on the table, then those data will automatically show without reloading the page.
Can anyone explain it? or give me any documentation or video link about it?
What you need are Broadcast Events.
Let's assume that you are inserting a Post and you want all users to get notified about a new post, therefore refresh the posts table index.
All your users should be subscribed to a presence channel, but you could use private or public channel. IMO, presence channel works better for this scenario since you are dispatching just 1 event for all users instead of 1 event per user in case of private channel
In your store function in PostController.php you dispatch the event once Post has been created:
use App\Events\PostCreated;
public function store(Request $request)
{
// Insert new post
$post = Post::create($request->all());
// Dispatch broadcast
PostCreated::dispatch($post);
return $result;
}
Then in your PostCreated.php Event, you send the post itself as the event payload:
<?php
namespace App\Events;
use App\Models\Post;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class PostCreated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $afterCommit = true;
public Post $post;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* The event's broadcast name.
*
* #return string
*/
public function broadcastAs()
{
return 'post.created';
}
/**
* Get the data to broadcast.
*
* #return array
*/
public function broadcastWith()
{
return $this->post;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('posts');
}
}
Now you need that all users subscribe to the right channel. Assuming, again, that you are using laravel echo, this is how I do it within a Vue Js app by joining a presence channel posts and listening for post.created event.
this.echoInstance.join('posts')
.listen('.post.created', (post) => {
// Do something like refresh table
// or insert `post` object directly in posts array
})
Since you didn't provide any code, this is a generic sample. Next time, please share what you've done so far.

Symfony, How to use DenyAccess with Doctrine object?

I want to control the same user access on some methods in my controller.
Currently, I use this :
$this->denyAccessUnlessGranted('ACCESS', $this->Player($toolRepository));
However I am forced to use this line and inject the ToolRepository into each method ...
What would be the easiest way to do it?
I saw that there was the IsGranted annotation but my subject needs to be a doctrine object to control access with my Vote.
/**
* #Route("/player")
*/
class PlayerController extends AbstractController
{
/**
* #Route("/", name="player")
* #throws Exception
*/
public function Player(ToolRepository $toolRepository): \App\Entity\Tool
{
$playerTool = 'TestTool2';
$tool = $toolRepository->findOneBy(array('libelle' => $playerTool));
if (!$tool) {
throw new Exception('Tool : ' . $playerTool . 'not found!');
}
return $tool;
}
/**
* #Route("/main", name="player")
* #IsGranted ("ACCESS", subject="tool")
* #throws Exception
*/
public function mainPlayer(PlayerRepository $playerRepository, ToolRepository $toolRepository): Response
{
$this->denyAccessUnlessGranted('ACCESS', $this->Player($toolRepository));
$players = $playerRepository->findAll();
return $this->render('player/player_mainpage.html.twig', ['players'=>$players]);
}
}
I think this ressource should answer you: Symfony voters.
You'll put your security logic in your custom voter which will be called in every function of your controller (or every methods where you want to control access) isGranted() function.
Calling your Player() function is a easier way to do this for beginner, you can keep like that but you shouldn't put it in Controller and use a Service instead .
Edit:
You may store your ToolRepository as Controller private property and inject it in a __construct() method so you don't have to inject ToolRepository in each method

How should "Connection reset by peer" be handled in Netty?

A "side effect" of using Netty is that you need to handle stuff you never thought about, like sockets closing and connection resets. A recurring theme is having your logs stuffed full of java.lang.IOException: Connection reset by peer.
What I am wondering about is how to handle these "correctly" from a web server perspective. AFAIK, this error simply means the other side has closed its socket (for instance, if reloading the web page or similar) while a request was sent to the server.
This is how we currently handle exceptions happening in our pipeline (I think it does not make full sense):
s, not the handler I have attached to the end of the pipeline.
current setup
pipeline.addLast(
new HttpServerCodec(),
new HttpObjectAggregator(MAX_CONTENT_LENGTH),
new HttpChunkContentCompressor(),
new ChunkedWriteHandler()
// lots of handlers
// ...
new InterruptingExceptionHandler()
);
pipeline.addFirst(new OutboundExceptionRouter());
the handler of exceptions
private class InterruptingExceptionHandler extends ChannelInboundHandlerAdapter {
#Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
final var id = ctx.channel().id();
// This needs to ge before the next section as the interrupt handler might shutdown the server before
// we are able to notify the client of the error
ctx.writeAndFlush(serverErrorJSON("A server error happened. Examine the logs for channel id " + id));
if (cause instanceof Error) {
logger.error(format("Error caught at end of pipeline in channel %s, interrupting!", id), cause);
ApplicationPipelineInitializer.this.serverInterruptHook.run();
} else {
logger.error(format("Uncaught user land exception in channel %s for request %s: ", id, requestId(ctx)), cause);
}
}
If some exception, like the IOException, is thrown we try and write a response back. In the case of a closed socket, this will then fail, right? So I guess we should try and detect "connection reset by peer" somehow and just ignore the exception silently to avoid triggering a new issue by writing to a closed socket ... If so, how? Should I try and do err instanceof IOException and err.message.equals("Connection reset by peer") or are there more elegant solutions? To me, it seems like this should be handled by some handler further down in the stack, closer to the HTTP handler
If you wonder about the OutboundExceptionRouter:
/**
* This is the first outbound handler invoked in the pipeline. What it does is add a listener to the
* outbound write promise which will execute future.channel().pipeline().fireExceptionCaught(future.cause())
* when the promise fails.
* The fireExceptionCaught method propagates the exception through the pipeline in the INBOUND direction,
* eventually reaching the ExceptionHandler.
*/
private class OutboundExceptionRouter extends ChannelOutboundHandlerAdapter {
#Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
super.write(ctx, msg, promise);
}
}

PHPUnit Mock Exception

I have a class which handles errors, including exceptions. If an exception is caught, I will pass the exception as an argument to my exception/error handler.
try {
someTrowingFnc();
} catch (\Exception $e) {
this->error->exception($e);
}
Now I want to unit test this error handler and mock the exception.
I am finding it hard to mock the exception so that I can control the exception message, file and line.
$exceptionMock = $this->getMock('Exception', array(
'getFile',
'getLine',
'getMessage',
'getTrace'
)); // Tried all mock arguments like disable callOriginalConstructor
$exceptionMock->expects($this->any())
->method('getFile')
->willReturn('/file/name');
$exceptionMock->expects($this->any())
->method('getLine')
->willReturn('3069');
$exceptionMock->expects($this->any())
->method('getMessage')
->willReturn('Error test');
The results of the code below always returns NULL
$file = $exception->getFile();
$line = $exception->getLine();
$msg = $exception->getMessage();
Is there a work-around to mock exceptions or am I just doing something wrong?
The Exception class methods that return the error details such as getFile() etc are defined/declared as final methods. And this is one limitation of PHPUnit currently in mocking methods that are protected, private, and final.
Limitations
Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.
As seen here: https://phpunit.de/manual/current/en/test-doubles.html
It's a bit of a hack, but try adding something like this to your TestCase:
/**
* #param object $object The object to update
* #param string $attributeName The attribute to change
* #param mixed $value The value to change it to
*/
protected function setObjectAttribute($object, $attributeName, $value)
{
$reflection = new \ReflectionObject($object);
$property = $reflection->getProperty($attributeName);
$property->setAccessible(true);
$property->setValue($object, $value);
}
Now you can change the values.
$exception = $this->getMock('Exception');
$this->setObjectAttribute($exception, 'file', '/file/name');
$this->setObjectAttribute($exception, 'line', 3069);
$this->setObjectAttribute($exception, 'message', 'Error test');
Of course, you haven't really mocked the class, though this can still be useful if you have a more complex custom Exception. Also you won't be able to count how many times the method is called, but since you were using $this->any(), I assume that doesn't matter.
It's also useful when you're testing how an Exception is handled, for example to see if another method (such as a logger) was called with the the exception message as a parameter
The throwException() in PHPUnit TestCase class can take any instance of Throwable as param.
Here is an example that should pass if you have try/catch in FileWriterToBeTested and will fail if you do not have try/catch:
$this->reader = $this->getMockBuilder(Reader::class)->getMock();
$this->reader->method('getFile')->will(static::throwException(new \Exception()));
$file = new FileWriterToBeTested($this->reader);
static::assertNull($file->getFile('someParamLikePath'));
tested class sample:
class FileWriterToBeTested
{
/**
* #var Reader
*/
private $reader;
public function __construct(Reader $reader): void
{
$this->reader = $reader;
}
/**
* #return Reader
*/
public function getFile(string $path): void
{
try {
$this->reader->getFile($path);
} catch (\Exception $e) {
$this->error->exception($e);
}
}
}

How to mock HTTPSession/FlexSession with TestNG and some Mocking Framework

I'm developing a web application running on Tomcat 6, with Flex as Frontend. I'm testing my backend with TestNG. Currently, I'm trying to test the following method in my Java-Backend:
public class UserDAO extends AbstractDAO {
(...)
public UserPE login(String mail, String password) {
UserPE dbuser = findUserByMail(mail);
if (dbuser == null || !dbuser.getPassword().equals(password))
throw new RuntimeException("Invalid username and/or password");
// Save logged in user
FlexSession session = FlexContext.getFlexSession();
session.setAttribute("user", dbuser);
return dbuser;
}
}
The method needs access to the FlexContext which only exists when i run it on the Servlet container (don't bother if you don't know Flex, it's more a Java-Mocking question in general). Otherwise i get a Nullpointer exception when calling session.setAttribute().
Unfortunately, I cannot set the FlexContext from outside, which would make me able to set it from my tests. It's just obtained inside the method.
What would be the best way to test this method with a Mocking framework, without changing the method or the class which includes the method? And which framework would be the easiest for this use case (there are hardly other things i have to mock in my app, it's pretty simple)?
Sorry I could try out all of them for myself and see how i could get this to work, but i hope that i'll get a quickstart with some good advices!
Obvious one approach is to re-factor it in a way that lets you inject things like the FlexContext. However this is not always possible. Some time ago a team I was part of hit a situation where we had to mock out some internal class stuff that we didn't have access to (like your context). We ended up using an api called jmockit which allows you to effective mock individual methods, including static calls.
Using this technology we where able to get around a very messy server implementation and rather than having to deploy to live servers and black box test, we were able to unit test at a fine level by overriding the server technology that was effective hard coded.
The only recommendation I would make about using something like jmockit is to ensure that in your test code there is clear documentation and seperation of jomockit from you main mocking framework (easymock or mockito would be my recommendations). Otherwise you risk confusing developers about the various responsibilities of each part of the puzzle, which usually leads to poor quality tests or tests that don't work that well. Ideally, as we ended up doing, wrap the jmockit code into you testing fixtures so the developers don't even know about it. Dealing with 1 api is enough for most people.
Just for the hell of it, here's the code we used to fix testing for an IBM class. WE basically need to do two things,
Have the ability to inject out own mocks to be returned by a method.
Kill off a constructor that went looking for a running server.
Do the above without having access to the source code.
Here's the code:
import java.util.HashMap;
import java.util.Map;
import mockit.Mock;
import mockit.MockClass;
import mockit.Mockit;
import com.ibm.ws.sca.internal.manager.impl.ServiceManagerImpl;
/**
* This class makes use of JMockit to inject it's own version of the
* locateService method into the IBM ServiceManager. It can then be used to
* return mock objects instead of the concrete implementations.
* <p>
* This is done because the IBM implementation of SCA hard codes the static
* methods which provide the component lookups and therefore there is no method
* (including reflection) that developers can use to use mocks instead.
* <p>
* Note: we also override the constructor because the default implementations
* also go after IBM setup which is not needed and will take a large amount of
* time.
*
* #see AbstractSCAUnitTest
*
* #author Derek Clarkson
* #version ${version}
*
*/
// We are going to inject code into the service manager.
#MockClass(realClass = ServiceManagerImpl.class)
public class ServiceManagerInterceptor {
/**
* How we access this interceptor's cache of objects.
*/
public static final ServiceManagerInterceptor INSTANCE = new ServiceManagerInterceptor();
/**
* Local map to store the registered services.
*/
private Map<String, Object> serviceRegistry = new HashMap<String, Object>();
/**
* Before runnin your test, make sure you call this method to start
* intercepting the calls to the service manager.
*
*/
public static void interceptServiceManagerCalls() {
Mockit.setUpMocks(INSTANCE);
}
/**
* Call to stop intercepting after your tests.
*/
public static void restoreServiceManagerCalls() {
Mockit.tearDownMocks();
}
/**
* Mock default constructor to stop extensive initialisation. Note the $init
* name which is a special JMockit name used to denote a constructor. Do not
* remove this or your tests will slow down or even crash out.
*/
#Mock
public void $init() {
// Do not remove!
}
/**
* Clears all registered mocks from the registry.
*
*/
public void clearRegistry() {
this.serviceRegistry.clear();
}
/**
* Override method which is injected into the ServiceManager class by
* JMockit. It's job is to intercept the call to the serviceManager's
* locateService() method and to return an object from our cache instead.
* <p>
* This is called from the code you are testing.
*
* #param referenceName
* the reference name of the service you are requesting.
* #return
*/
#Mock
public Object locateService(String referenceName) {
return serviceRegistry.get(referenceName);
}
/**
* Use this to store a reference to a service. usually this will be a
* reference to a mock object of some sort.
*
* #param referenceName
* the reference name you want the mocked service to be stored
* under. This should match the name used in the code being tested
* to request the service.
* #param serviceImpl
* this is the mocked implementation of the service.
*/
public void registerService(String referenceName, Object serviceImpl) {
serviceRegistry.put(referenceName, serviceImpl);
}
}
And here's the abstract class we used as a parent for tests.
public abstract class AbstractSCAUnitTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
ServiceManagerInterceptor.INSTANCE.clearRegistry();
ServiceManagerInterceptor.interceptServiceManagerCalls();
}
protected void tearDown() throws Exception {
ServiceManagerInterceptor.restoreServiceManagerCalls();
super.tearDown();
}
}
Thanks to Derek Clarkson, I successfully mocked the FlexContext, making the login testable. Unfortunately, it's only possible with JUnit, as far as i see (tested all versions of TestNG with no success - the JMockit javaagent does not like TestNG, See this and this issues).
So this is how i'm doing it now:
public class MockTests {
#MockClass(realClass = FlexContext.class)
public static class MockFlexContext {
#Mock
public FlexSession getFlexSession() {
System.out.println("I'm a Mock FlexContext.");
return new FlexSession() {
#Override
public boolean isPushSupported() {
return false;
}
#Override
public String getId() {
return null;
}
};
}
}
#BeforeClass
public static void setUpBeforeClass() throws Exception {
Mockit.setUpMocks(MockFlexContext.class);
// Test user is registered here
(...)
}
#Test
public void testLoginUser() {
UserDAO userDAO = new UserDAO();
assertEquals(userDAO.getUserList().size(), 1);
// no NPE here
userDAO.login("asdf#asdf.de", "asdfasdf");
}
}
For further testing i now have to implement things like the session map myself. But thats okay as my app and my test cases are pretty simple.