How to mock an outgoing Socket connection? - java

In integration tests (JDK 6) I'm trying to catch all outgoing TCP connections and mock them. Looks like I should use java.net.Socket#setSocketImplFactory() method. Works fine at the moment, but I can't understand how I can get an access to original factory, in order to instantiate original JDK-provided SocketImpl class. I need this mostly because I want to let some connections to go out freely, without mocking. Can you suggest some manuals/guidelines/instructions about this problem?

Instead of mocking a Socket I would create a Socket service for the Socket to talk to. This can capture all the data written and reply in any manner you wish. It can be run in the same test and possibly in the same thread.

Looking at the source code of the Socket class, there is no original factory - the constructors check to see if factory is null, and if it is, they just assign impl to be a new PlainSocketImpl().

According to the javadoc, you should be able to use SocketFactory#getDefault() to get the default SocketFactory for your environment.
BTW: You might also want to look at this RFE/bug which declares that SocketImplFactory is basically a dead-end: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4245730

Related

Reuse Provide in Guice based on parameters

I've tried searching for this but haven't really found a solution so decided to post a question.
I'm working on an application where a user will input an IP (an SNMP device) and my application needs to connect to it and work with it. During runtime of the application, the user may provide another IP address and then I need to connect to the second one also keeping both the connections alive (as singletons).
My problem is I'm not able to wrap my head around this conceptually. My connection module is right now something like the following:
#Override
Configure() {
String ip = first ip;
}
#Provides
Connect connect() {
// connect to ip
return connection;
}
Can anyone give me some hints here?
You should probably pass the IP address as an argument for your Connect class constructor. You can then look at FactoryModuleBuilder so that you can inject dependencies to your Connect class in your code. As for your requirement about singletons, I am not too sure of what you mean there. A singleton means, by definition, that there's only one instance of a class. Here, you want two (or possibly more). What I suspect is that you want at most a single Connect instance per IP String in the entire application. If that is the case, your factory needs to be a bit cleverer that the one created automatically by FactoryModuleBuilder. It could be a singleton itself and store an index (map? concurrent map? cache? It depends on your thread-safety requirements) of ip -> connect instances for those that have been already created.
Hope it helps.

how can i check if a proxy is alive or not in JAVA

I'm creating a 'bot' opening a page with random proxies obtained from a file.txt, but i wanna check first if the proxy is alive or not before using them.
I'm not going to put any code here because I am only doing a request if somebody have a simple method to do this task.
I read about InetSocketAddress but I have the problem that I can only use it with (String Host, Int port). How can I pass Host+port together in a simple String?
If this is not the solution, can someone explain me another method?
This approach is invalid.
The only viable way to test whether any resource is available is to try to use it in the normal course of your program, and handle the errors if it fails.
Otherwise you're involved in predicting the future. It might be up when you test and down when you use. Or the other way around. Or you might test it in a different way from how you will use it, and so, again, get a different result.
I read about "InetSocketAddress" ...
If you are going to test the proxy, this is one approach. (Just test that you can open a TCP/IP connection to the proxy, and then close it). Another way would be to set up your own tiny webserver with a text page, and test the proxy by using it to access your test page. (That gives a more accurate "read" on the liveness of a proxy.)
... but i have the problem that only can i (String Host, Int port) how can i pass Host+port in a simple String?
That is easy. You don't.
You need to parse the string and extract the hostname and the port number. (Java 101 stuff ...) Then pass you pass them as separate arguments.
If the "string" is actually a URL or URI, then you can use java.net.URI to do the parsing.
However, there's another approach. Don't test the proxy. Just use it, and if it doesn't respond then mark it as bad.
IMO, you pretty much have to adopt this approach to make your code robust. If you probe a proxy and find it to be reachable and working at time T, there is no guarantee that it will still be working at time T + 1. No amount of testing will solve that.

Kryonet: Do classes being registered have to be identical?

I am sending some packets through kryonet that simply hold an "Entity" variable. I created the entity class myself. The thing is that when registering the entity class, the class file on the server and the client are not exactly the same.
On the client side, I did not include some methods because they rely on accessing variable that are only server side and I completely removed all constructors because the client will not be the one creating entities, the server will. On the server wide I left out the render method since the server will not be rendering.
Does it really matter what methods and constructors are there? Does kryonet only look to see if the variables are the same (cause they are)? Thanks!
By the way, if you were wondering, Entity is an abstract method and therefore when I create new types of entities like "Player" for example, they extend it and add even more methods and variable. I hope that is alright for sending those in a packet too.
I am not sure what you are asking but as far as I understand I will try my best to answer your question.
So I think what you are trying to do is you have a base class called Entity and you extend it to different classes. You implement some methods in class which will be sent to client and some which will be sent to server.
So as you asked does it matter what methods and constructor are there, the answer is NO. Till the time you have an empty Constructor (required by Kryo serializer) kryonet is fine with whatever constructor you have. Just you need to make sure you have an empty constructor. I have many classes in kryonet with more than 2 constructors and they work perfectly fine.
A tip, since you are sending data on network, if I was you I would have removed all the data variables which will not be used in client and abstracted out the classes even more.
Also why are you sending methods in classes? Just curious. I think you should have methods in server and client and you should take the data out of the packet (classes) and then send it to the method in your server or client.
If anything is not clear let me know.

How to send class over TCP connection?

I am trying to send a class over a TCP connection using java. When the client receives the class it should invoke the methods of that class and return the result to the server.
I have serialized the class to a byte array to send to the client, but I don't know with it once the client receives it.
Thank you.
Your question is a bit ambiguous. Are you sending a *.class file or an instance of the class? I'll bet that you actually mean an instance of the class since you literally said that you want to send it back. This makes only sense if it were an instance. The other side should then have the class file in its classpath as well. Then you can just import the class and cast to the desired class on readObject(). Finally you'll be able to invoke methods on it according the class' contract.
See also:
Basic Serialization tutorial
Advanced Serialization tutorial
If you're actually sending a *.class file, using a ClassLoader to load it would indeed be the answer.
You probably need to use a ClassLoader.
If possible for you, you may want to look at RMI where clients can provide classes for the server to invoke.

Why is JavaMail Transport.send() a static method?

I'm revising code I did not write that uses JavaMail, and having a little trouble understanding why the JavaMail API is designed the way it is. I have the feeling that if I understood, I could be doing a better job.
We call:
transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);
So why is Eclipse warning me that this:
transport.send(message, message.getAllRecipients());
is a call to a static method?
Why am I getting a Transport object and providing settings that are specific to it if I can't use that object to send the message? How does the Transport class even know what server and other settings to use to send the message? It's working fine, which is hard to believe. What if I had instantiated Transport objects for two different servers; how would it know which one to use?
In the course of writing this question, I've discovered that I should really be calling:
transport.sendMessage(message, message.getAllRecipients());
So what is the purpose of the static Transport.send() method? Is this just poor design, or is there a reason it is this way?
Transport.send() is basically a convenience method. Yes, if you're managing your own Transport instance, call sendMessage().
I'm not sure I consider it bad design, since frequently you don't care to manage the details of sending and monitoring transport. Crack open the send() method to see what it does for you. Other ways of naming or placing this method could be marginally better.
The javadoc says:
Send is a static method that creates and manages its own connection. Any connection associated with any Transport instance used to invoke this method is ignored and not used. This method should only be invoked using the form Transport.send(msg);, and should never be invoked using an instance variable.
And yes probably, the design is not good

Categories