Skip to content

Commit bdfd6d4

Browse files
committed
Adapt fragment to dual netcats.
Move almost all test functions to NetCatTest. Get rid of Dagger.
1 parent 0af157d commit bdfd6d4

File tree

10 files changed

+104
-121
lines changed

10 files changed

+104
-121
lines changed

netcat/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ dependencies {
7171
compile 'com.android.support:support-v4:19.+'
7272
compile 'com.android.support:appcompat-v7:19.+'
7373
compile 'com.jakewharton:butterknife:5.0.+'
74-
compile 'com.squareup.dagger:dagger:1.2.+'
7574
compile 'de.greenrobot:eventbus:2.2.+'
76-
provided 'com.squareup.dagger:dagger-compiler:1.2.+'
7775

7876
testCompile('com.squareup:fest-android:1.0.+')
7977
testCompile('junit:junit:4.11')

netcat/src/main/java/com/github/dddpaul/netcat/NetCatModule.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

netcat/src/main/java/com/github/dddpaul/netcat/TcpNetCat.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class TcpNetCat extends NetCat
2525
private ServerSocketChannel serverChannel;
2626
private Socket socket;
2727

28+
public TcpNetCat()
29+
{
30+
super();
31+
}
32+
2833
public TcpNetCat( NetCatListener listener )
2934
{
3035
super( listener );
@@ -76,19 +81,17 @@ protected Result doInBackground( String... params )
7681
int port;
7782
switch( op ) {
7883
case CONNECT:
79-
Proto proto = Proto.valueOf( params[1] );
80-
String host = params[2];
81-
port = Integer.parseInt( params[3] );
82-
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (%s)", host, port, proto ) );
84+
String host = params[1];
85+
port = Integer.parseInt( params[2] );
86+
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (TCP)", host, port ) );
8387
socket = new Socket();
8488
socket.connect( new InetSocketAddress( host, port ), 3000 );
8589
publishProgress( CONNECTED.toString() );
8690
result.object = socket;
8791
break;
8892
case LISTEN:
89-
proto = Proto.valueOf( params[1] );
90-
port = Integer.parseInt( params[2] );
91-
Log.d( CLASS_NAME, String.format( "Listening on %d (%s)", port, proto ) );
93+
port = Integer.parseInt( params[1] );
94+
Log.d( CLASS_NAME, String.format( "Listening on %d (TCP)", port ) );
9295
serverChannel = ServerSocketChannel.open();
9396
serverChannel.configureBlocking( false );
9497
serverChannel.socket().bind( new InetSocketAddress( port ) );

netcat/src/main/java/com/github/dddpaul/netcat/UdpNetCat.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public UdpNetCat( NetCatListener listener )
2323
super( listener );
2424
}
2525

26+
public UdpNetCat()
27+
{
28+
super();
29+
}
30+
2631
@Override
2732
public void cancel()
2833
{
@@ -69,19 +74,17 @@ protected Result doInBackground( String... params )
6974
int port;
7075
switch( op ) {
7176
case CONNECT:
72-
Proto proto = Proto.valueOf( params[1] );
73-
String host = params[2];
74-
port = Integer.parseInt( params[3] );
75-
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (%s)", host, port, proto ) );
77+
String host = params[1];
78+
port = Integer.parseInt( params[2] );
79+
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (UDP)", host, port ) );
7680
socket = new DatagramSocket();
7781
socket.connect( new InetSocketAddress( host, port ) );
7882
publishProgress( CONNECTED.toString() );
7983
result.object = socket;
8084
break;
8185
case LISTEN:
82-
proto = Proto.valueOf( params[1] );
83-
port = Integer.parseInt( params[2] );
84-
Log.d( CLASS_NAME, String.format( "Listening on %d (%s)", port, proto ) );
86+
port = Integer.parseInt( params[1] );
87+
Log.d( CLASS_NAME, String.format( "Listening on %d (UDP)", port ) );
8588
socket = new DatagramSocket( port );
8689
result.object = socket;
8790
break;

netcat/src/main/java/com/github/dddpaul/netcat/ui/NetCatFragment.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
import android.os.Bundle;
44
import android.support.v4.app.Fragment;
55

6-
import com.github.dddpaul.netcat.NetCatModule;
76
import com.github.dddpaul.netcat.NetCater;
7+
import com.github.dddpaul.netcat.TcpNetCat;
8+
import com.github.dddpaul.netcat.UdpNetCat;
89

9-
import javax.inject.Inject;
10-
11-
import dagger.ObjectGraph;
10+
import static com.github.dddpaul.netcat.NetCater.*;
1211

1312
public class NetCatFragment extends Fragment
1413
{
1514
private final String CLASS_NAME = ( (Object) this ).getClass().getSimpleName();
1615

17-
@Inject
18-
protected NetCater netCat;
16+
private NetCater netCat;
1917

2018
public NetCatFragment() {}
2119

@@ -29,11 +27,27 @@ public void onCreate( Bundle savedInstanceState )
2927
{
3028
super.onCreate( savedInstanceState );
3129
setRetainInstance( true );
32-
ObjectGraph.create( new NetCatModule() ).inject( this );
3330
}
3431

3532
public NetCater getNetCat()
3633
{
3734
return netCat;
3835
}
36+
37+
public NetCater getOrCreateNetCat( Proto proto )
38+
{
39+
switch( proto ) {
40+
case TCP:
41+
if( netCat != null && netCat instanceof TcpNetCat ) {
42+
return netCat;
43+
}
44+
return new TcpNetCat();
45+
case UDP:
46+
if( netCat != null && netCat instanceof UdpNetCat ) {
47+
return netCat;
48+
}
49+
return new UdpNetCat();
50+
}
51+
return null;
52+
}
3953
}

netcat/src/main/java/com/github/dddpaul/netcat/ui/ResultFragment.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.support.v4.app.Fragment;
55
import android.text.Editable;
66
import android.text.method.ScrollingMovementMethod;
7-
import android.util.Log;
87
import android.view.LayoutInflater;
98
import android.view.View;
109
import android.view.ViewGroup;
@@ -26,10 +25,10 @@
2625
import events.FragmentEvent;
2726

2827
import java.io.ByteArrayInputStream;
29-
import java.net.Socket;
3028

3129
import static com.github.dddpaul.netcat.Constants.NETCAT_FRAGMENT_TAG;
3230
import static com.github.dddpaul.netcat.Constants.RECEIVED_TEXT_KEY;
31+
import static com.github.dddpaul.netcat.NetCater.*;
3332
import static com.github.dddpaul.netcat.NetCater.Op.*;
3433
import static com.github.dddpaul.netcat.NetCater.Result;
3534
import static com.github.dddpaul.netcat.NetCater.State.*;
@@ -39,6 +38,7 @@ public class ResultFragment extends Fragment implements NetCatListener
3938
private final String CLASS_NAME = ( (Object) this ).getClass().getSimpleName();
4039

4140
private NetCater netCat;
41+
private NetCatFragment netCatFragment;
4242
private TextWatcherAdapter watcher;
4343

4444
@InjectView( R.id.et_input )
@@ -105,7 +105,7 @@ public void onResume()
105105
{
106106
super.onResume();
107107
updateUIWithValidation();
108-
NetCatFragment netCatFragment = (NetCatFragment) getFragmentManager().findFragmentByTag( NETCAT_FRAGMENT_TAG );
108+
netCatFragment = (NetCatFragment) getFragmentManager().findFragmentByTag( NETCAT_FRAGMENT_TAG );
109109
if( netCatFragment != null ) {
110110
netCat = netCatFragment.getNetCat();
111111
netCat.setListener( this );
@@ -187,6 +187,10 @@ public void setNetCat( NetCater netCat )
187187
{
188188
this.netCat = netCat;
189189
}
190+
public void setNetCatFragment( NetCatFragment netCatFragment )
191+
{
192+
this.netCatFragment = netCatFragment;
193+
}
190194

191195
public void connect( String connectTo )
192196
{
@@ -199,7 +203,9 @@ public void connect( String connectTo )
199203
return;
200204
}
201205
String[] tokens = connectTo.split( ":" );
202-
netCat.execute( CONNECT.toString(), tokens[0], tokens[1], tokens[2] );
206+
netCat = netCatFragment.getOrCreateNetCat( Proto.valueOf( tokens[0] ) );
207+
netCat.setListener( this );
208+
netCat.execute( CONNECT.toString(), tokens[1], tokens[2] );
203209
}
204210

205211
public void listen( String listenOn )
@@ -213,7 +219,9 @@ public void listen( String listenOn )
213219
return;
214220
}
215221
String[] tokens = listenOn.split( ":" );
216-
netCat.execute( LISTEN.toString(), tokens[0], tokens[1] );
222+
netCat = netCatFragment.getOrCreateNetCat( Proto.valueOf( tokens[0] ) );
223+
netCat.setListener( this );
224+
netCat.execute( LISTEN.toString(), tokens[1] );
217225
}
218226

219227
private void send()

netcat/src/test/java/com/github/dddpaul/netcat/NetCatTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import java.util.concurrent.TimeUnit;
1717

1818
import static com.github.dddpaul.netcat.NetCater.*;
19+
import static com.github.dddpaul.netcat.NetCater.Op.CONNECT;
1920
import static com.github.dddpaul.netcat.NetCater.Op.DISCONNECT;
21+
import static com.github.dddpaul.netcat.NetCater.Op.LISTEN;
2022
import static com.github.dddpaul.netcat.NetCater.Op.RECEIVE;
2123
import static com.github.dddpaul.netcat.NetCater.Op.SEND;
2224
import static org.hamcrest.core.Is.is;
@@ -57,6 +59,28 @@ public List<String> prepareNetCatProcess( Proto proto, boolean listen, int port
5759
return result;
5860
}
5961

62+
public void connect( int port ) throws InterruptedException
63+
{
64+
netCat.execute( CONNECT.toString(), HOST, String.valueOf( port ) );
65+
latch.await( 5, TimeUnit.SECONDS );
66+
67+
assertNotNull( result );
68+
assertNull( result.exception );
69+
assertThat( result.op, is( CONNECT ));
70+
assertNotNull( result.getSocket() );
71+
}
72+
73+
public void listen( int port ) throws InterruptedException
74+
{
75+
netCat.execute( LISTEN.toString(), String.valueOf( port ) );
76+
latch.await( 5, TimeUnit.SECONDS );
77+
78+
assertNotNull( result );
79+
assertNull( result.exception );
80+
assertThat( result.op, is( LISTEN ));
81+
assertNotNull( result.getSocket() );
82+
}
83+
6084
public void disconnect() throws InterruptedException
6185
{
6286
netCat.execute( DISCONNECT.toString() );

netcat/src/test/java/com/github/dddpaul/netcat/ResultFragmentTest.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.dddpaul.netcat;
22

3-
import android.util.Log;
43
import android.widget.Button;
54
import android.widget.EditText;
65

6+
import com.github.dddpaul.netcat.ui.NetCatFragment;
77
import com.github.dddpaul.netcat.ui.ResultFragment;
88

99
import org.junit.Before;
@@ -17,6 +17,7 @@
1717
import org.robolectric.shadows.ShadowLog;
1818
import org.robolectric.shadows.ShadowToast;
1919

20+
import static com.github.dddpaul.netcat.NetCater.*;
2021
import static com.github.dddpaul.netcat.NetCater.Op.CONNECT;
2122
import static com.github.dddpaul.netcat.NetCater.Op.LISTEN;
2223
import static com.github.dddpaul.netcat.NetCater.Proto.*;
@@ -35,7 +36,10 @@ public class ResultFragmentTest
3536
private ResultFragment fragment;
3637

3738
@Mock
38-
private NetCater mockNetCat;
39+
private NetCater netCatMock;
40+
41+
@Mock
42+
private NetCatFragment netCatFragmentMock;
3943

4044
@BeforeClass
4145
public static void init()
@@ -47,6 +51,8 @@ public static void init()
4751
public void setUp()
4852
{
4953
MockitoAnnotations.initMocks( this );
54+
when( netCatFragmentMock.getNetCat() ).thenReturn( netCatMock );
55+
when( netCatFragmentMock.getOrCreateNetCat( any( Proto.class ) ) ).thenReturn( netCatMock );
5056
fragment = new ResultFragment();
5157
}
5258

@@ -57,8 +63,8 @@ public void setUp()
5763
public void testSendButton()
5864
{
5965
startFragment( fragment );
60-
when( mockNetCat.isConnected() ).thenReturn( true );
61-
fragment.setNetCat( mockNetCat );
66+
when( netCatMock.isConnected() ).thenReturn( true );
67+
fragment.setNetCat( netCatMock );
6268

6369
Button sendButton = (Button) fragment.getView().findViewById( R.id.b_send );
6470

@@ -77,34 +83,35 @@ public void testSendButton()
7783
public void testConnect()
7884
{
7985
startFragment( fragment );
86+
fragment.setNetCatFragment( netCatFragmentMock );
8087

8188
// When netCat is connected error should be toasted
82-
when( mockNetCat.isConnected() ).thenReturn( true );
83-
fragment.setNetCat( mockNetCat );
89+
when( netCatMock.isConnected() ).thenReturn( true );
90+
fragment.setNetCat( netCatMock );
8491
fragment.connect( "" );
8592

8693
assertThat( ShadowToast.getTextOfLatestToast(), is( fragment.getString( R.string.error_disconnect_first ) ) );
8794

8895
// When netCat is listening error should be toasted
89-
when( mockNetCat.isConnected() ).thenReturn( false );
90-
when( mockNetCat.isListening() ).thenReturn( true );
91-
fragment.setNetCat( mockNetCat );
96+
when( netCatMock.isConnected() ).thenReturn( false );
97+
when( netCatMock.isListening() ).thenReturn( true );
98+
fragment.setNetCat( netCatMock );
9299
fragment.connect( "" );
93100

94101
assertThat( ShadowToast.getTextOfLatestToast(), is( fragment.getString( R.string.error_disconnect_first ) ) );
95102

96103
// When netCat is not connected nor listening but connectTo string has invalid format error should be toasted too
97-
when( mockNetCat.isConnected() ).thenReturn( false );
98-
when( mockNetCat.isListening() ).thenReturn( false );
99-
fragment.setNetCat( mockNetCat );
104+
when( netCatMock.isConnected() ).thenReturn( false );
105+
when( netCatMock.isListening() ).thenReturn( false );
106+
fragment.setNetCat( netCatMock );
100107
fragment.connect( "some improper connectTo string" );
101108

102109
assertThat( ShadowToast.getTextOfLatestToast(), is( fragment.getString( R.string.error_host_port_format ) ) );
103110

104111
// Test proper connect at last
105112
fragment.connect( "TCP:127.0.0.1:9999" );
106113

107-
verify( mockNetCat ).execute( CONNECT.toString(), TCP.toString(), "127.0.0.1", "9999" );
114+
verify( netCatMock ).execute( CONNECT.toString(), "127.0.0.1", "9999" );
108115
}
109116

110117
/**
@@ -114,24 +121,25 @@ public void testConnect()
114121
public void testListen()
115122
{
116123
startFragment( fragment );
124+
fragment.setNetCatFragment( netCatFragmentMock );
117125

118126
// When netCat is listening error should be toasted
119-
when( mockNetCat.isListening() ).thenReturn( true );
120-
fragment.setNetCat( mockNetCat );
127+
when( netCatMock.isListening() ).thenReturn( true );
128+
fragment.setNetCat( netCatMock );
121129
fragment.connect( "" );
122130

123131
assertThat( ShadowToast.getTextOfLatestToast(), is( fragment.getString( R.string.error_disconnect_first ) ) );
124132

125133
// When netCat is not listening but connectTo string has invalid format error should be toasted too
126-
when( mockNetCat.isListening() ).thenReturn( false );
127-
fragment.setNetCat( mockNetCat );
134+
when( netCatMock.isListening() ).thenReturn( false );
135+
fragment.setNetCat( netCatMock );
128136
fragment.listen( "some improper port string" );
129137

130138
assertThat( ShadowToast.getTextOfLatestToast(), is( fragment.getString( R.string.error_port_format ) ) );
131139

132140
// Test proper listen at last
133141
fragment.listen( "UDP:9999" );
134142

135-
verify( mockNetCat ).execute( LISTEN.toString(), UDP.toString(), "9999" );
143+
verify( netCatMock ).execute( LISTEN.toString(), "9999" );
136144
}
137145
}

0 commit comments

Comments
 (0)