can I build javafx without start function - swing

I want to develop a javafx application which should not have a start function why?
When I am developing a swing application with JFrame I usually comment the
public static void main(String[] args)
function and build the jar. This ensures me if someone double clicks the jar file will not execute unless proper method. I use this .jar file into another java program which will call this jar and execute it.
I want same functionality in javafx application.
When I comment public void start(Stage primaryStage) function in FX it gives error.
How should I do this.
Thanks in advance
Yogesh.

You could make use of the launch parameters of JavaFX (which is just a beefed up version of the main method parameters).
Lets say we add a new mandatory parameter named launch to our application. You can check if someone (your code) has passed this parameter to the application and start or if the parameter is missing quit the application.
You can access the parameters anywhere in your Application class, but in your case init() seems to be a good place:
public class MyFXClient extends Application {
#Override
public void init() throws Exception
{
Map<String, String> mainParams = getParameters().getNamed();
// TODO check if parameter "launch" is present..
}
public static void main(String[] args) {
launch(MyFXClient.class, args);
}
}
Now you can call the main method from outside and pass along the parameter with --launch=test.

I am using Eclipse
There I can export a project as "Runnable Jar-File" and "Jar-File".
If you want, that nobody double clicks the jar file, maybe you can use the "Jar-File" witch are not runnable
The difference between the two kinds of jar files are the "Runnable Jar-File" contains a Main-Class, and the other not.

Related

Is there a way to share #Before code between tests suite classes?

I am testing a Cordova plugin in Java/Android and I need to initialize my Plugin class and set some state before I run my Tests.
#Before
public void beforeEach() throws Exception {
System.out.println("Creating new Instance ");
PowerMockito.mockStatic(Helpers.class);
PowerMockito.when(Helpers.canUseStorage(any(), any())).thenReturn(true);
MyLogger myLoggerMock = PowerMockito.mock(MyLogger.class);
PowerMockito.doNothing().when(myLoggerMock, "log", anyString());
PowerMockito.whenNew(MyLogger.class).withAnyArguments().thenReturn(myLoggerMock);
this.sut = spy(new FilePicker());
PowerMockito.doNothing().when(this.sut).pick(any(), any());
}
I want to create a Test Suite / Java Class per public function, but I do not want to repeat that code every time.
Is there a way to share that before each between test suites? I have found ClassRule but I think I do not do what I need (or I am understanding it wrong... I am really new in Java)
In Typescript we can share beforeEachfunctions with several suites, and each suite can have their own beforeEach
One possible ways is using inheritance:
Make all test classes extend from one "parent test" class and define a #Before in a parent class.
So it will be called automatically for all the subclasses:
public class ParentTest {
#Before
public void doInitialization() {
....
}
}
public class Test1Class extends ParentClass {
#Test
public void fooTest() {
// doInitialization will be executed before this method
}
#Test
public void barTest() {
// doInitialization will be executed before this method as well
}
}
Two notes:
Note 1
In the code you use sut (subject under test) - this obviously should not be in the parent's doInitialization method, so its possible that Test1Class will also have methods annotated with #Before (read here for information about ordering and so forth)
Then the `sut gets initialized with Spy which is frankly weird IMHO, the Subject Under Test should be a real class that you wrote, but that's beyond the scope of the question, just mentioning it because it can point on mistake.
Note 2
I'm writing it in an an attempt to help because you've said that you're new in Java, this is not strictly related to your question...
While this approach works in general you should be really cautious with PowerMockito. I'm not a PowerMockito expert and try to avoid this type of mocks in my code but in a nutshell the way it manipulates the byte code can clash with other tools. From your code: you can refactor the HelperUtils to be non-static and thus avoid PowerMocking in favor of regular mocking which is faster and much more safe.
As for the Logging - usually you can compromise on it in unit test, if you're using slf4j library you can config it to use "no-op" log for tests, like sending all the logging messages into "nothing", and not-seeing them in the console.

Run code on periodic timer after services registered

I have a class which I'd like to run a method on periodically. I'd prefer to do this with a timer method, so built the class with a method: StartPolling() which would then call my DoSomething() method every 60 seconds.
I tried to start the polling from the Main method in Program.cs:
public static void Main(string[] args)
{
var webHost = BuildWebHost(args);
ConfigureApp();
var cam = IocManager.Instance.Resolve<CameraManager>();
webHost.Run();
}
But when I try resolve an instance (CamerManager) I get an error that other things that CameraManager depends on haven't been registered, e.g. repositories.
What's the best way to do this...and I do not want to use the ABP background jobs as ASP.NET may "go to sleep" so my tasks wouldn't get run on time.
Thanks
Chris

Difference in running Cucumber-JVM vs Cucumber runner(Junit)

I am fairly new to Cucumber. I was experimenting with it by just creating few test features when I noticed the difference when running a single feature vs running the whole suite (from the IntelliJ).
I noticed that when I run single feature it runs using the cucumber-jvm option and in this case, the CucumberConfig(the blank class to define the runner and cucumber options) and the Runner is not utilized. However, when I run the whole suite it runs as a JUnit test and obviously, in this case, the Config class and the runner comes into the picture.
I confirmed this with the following sample code:
#RunWith(CustomRunner.class)
#CucumberOptions()
public class CucumberConfig {
#BeforeClass
public static void beforeClass()
{
System.out.println("This is run before Once: ");
}
#AfterClass
public static void afterClass()
{
System.out.println("This is run after Once: ");
}
}
CustomRunner
public class CustomRunner extends Cucumber {
public CustomRunner(Class clazz) throws InitializationError, IOException {
super(clazz);
System.out.println("I am in the custom runner.");
}
}
Also, I understand that while running as cucumber-junit we can't pass specific feature to run as in cucumber-jvm. Correct me if I am wrong.
My doubt is, is this the default behavior or am I doing something wrong. And, if this is default how can I make cucumber to always use the Config file.
I'll appreciate if someone can provide some insight on this.
When you're using IntelliJ IDEA to run the tests, IDEA will use cucumber.api.Main to run the tests. As such it will ignore CucumberConfig neither will it run #BeforeClass nor #AfterClass, these are only used by the JUnit runner.

Calling C# method from C++ code in WP8

I'm interested in calling a C# method from C++ code in Windows Phone 8. I have already learned how to pass a callback function to C++ code from C# via delegate declarations in my C++ code, but I am looking to see if I can do any of the following:
Call certain methods directly from the C++ code. This would involve somehow inspecting the C# object makeup from C++, and seems unlikely to me, but I thought I'd ask you all anyway
Trigger events in the C# code, which can then be handled by C# methods
Use a dispatcher to call C# callbacks in the Main UI thread so that the callbacks can modify UI elements
Use a dispatcher to trigger events in the C# code, (Essentially a merging of the above two points)
In short, I am looking for as many C++ -->C# communication tips as you guys can throw me, I want to learn it all. :)
By getting an object in C# code to implement a Windows RT interface, and passing down a reference to this object, it is possible to do all of the above with a bit of set-up (if I understand correctly - not sure about exactly what you want to do with your Dispatcher examples - you might want to wrap the Dispatcher on the C# side).
Create a Windows Runtime component library.
Define a public interface class in a C++/CX header for the C# to implement (C++ to call) (e.g. ICallback).
Define a public ref class in a C++/CX header for the C++ to implement (C# to call) (e.g. CppCxClass).
Add a method in CppCxClass that passes and stores an ICallback. (A C++ global variable is shown for consiseness, I recommend you review this to see if you can find a better place to store this in your code-base).
ICallback^ globalCallback;
...
void CppCxClass::SetCallback(ICallback ^callback)
{
globalCallback = callback;
}
Reference the WinRT library in your C# code.
C# code: create an instance of CppCxClass using var cppObject = new CppCxClass().
C# code: create a class which implements ICallback (e.g. CSharpCallbackObject).
C# code: pass an instance of CSharpCallbackObject down to C++. E.g. cppObject.SetCallback(new CSharpCallbackObject()).
You can now call C# with globalCallback->CallCsharp(L"Hello C#");. You should be able to extend either ICallback and/or CppCxObject to do the rest of your tasks.
After a lot of headaches trying to figure out the required code, I think it's worth posting the final version here
C++/CX
//.h
[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:
virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};
//.cpp
ICallback ^CSCallback = nullptr;
void Direct3DInterop::SetCallback( ICallback ^Callback)
{
CSCallback = Callback;
}
//...
if (CSCallback != nullptr)
CSCallback->Exec( "Command", "Param" );
C#
public class CallbackImpl : ICallback
{
public void Exec(String Command, String Param)
{
//Execute some C# code, if you call UI stuff you will need to call this too
//Deployment.Current.Dispatcher.BeginInvoke(() => {
// //Lambda code
//}
}
}
//...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback( CI);

How to grant unmanaged code acess to a windows forms hosted in a html?

I am trying to host a windows forms control in C# inside an html page and then host that web page in IIS in order to be accessible by other client machines.
The problem is: the user control uses some unmanaged code, which triggers a SecurityPermissionException when accessing using another machine.
I've managed to dumb down my code to an elementary example in order to pinpoint the error and I just can't seem to find an answer to this.
Here's my user control:
// To handle strong named assembly
[assembly: AllowPartiallyTrustedCallers]
namespace WinFormsHTMLControl
{
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)] // to allow assertions regarding unmanaged code permissions
public partial class HelloWorldControl : UserControl
{
#region Methods/Consts for Embedding a Window
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
#endregion
public HelloWorldControl()
{
InitializeComponent();
}
private void btnClick_Click(object sender, EventArgs e)
{
new SecurityPermission(PermissionState.Unrestricted).Assert();
IntPtr picBoxHandle = FindWindow("IEFrame", "Internet Explorer");
lblMessage.Text = picBoxHandle.ToString();
SecurityPermission.RevertAssert();
}
}
}
I've signed the assembly with a key, I've created a Permission Set in .NET Configuration Tool in order to grant acess to unmanaged code, and created a CodeGroup pointing to the strong key used to name the assembly.
I've also created an MSI in order to copy these settings to other machines (i've done this both at Enterprise and Machine levels).
Despite all this, this code still triggers a SecurityPermissionException when I click the button...
Am I missing something here?
IE is sandboxed and you're trying to perform an operation that you can't perform-- ie, calling directly into user32 from a website. Imagine how dangerous the internet would be if anybody could do that on any website. Your core architecture is flawed.