IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

JUnit RMI : besoin d'aide


Sujet :

Java

  1. #1
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Décembre 2017
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2017
    Messages : 1
    Points : 1
    Points
    1
    Par défaut JUnit RMI : besoin d'aide
    Bonjour à tous,

    Je dois créer un tchat avec RMI, donc le tchat est fonctionnel mais je n'arrive pas élaborer des tests unitaires.
    Est ce que quelqu'un serait dans la mesure de m'aider ? (exemple concret, ...)

    Le server
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package tchat;
     
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Vector;
     
    public class Server extends UnicastRemoteObject implements IServer {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	private Vector<IClient> vector = new Vector<IClient>();
     
    	public Server() throws RemoteException {
    	}
     
    	public boolean login(IClient login) throws RemoteException {
    		System.out.println(login.getName() + " s'est connecté(e)....");
    		login.tell("Connection réussie");
    		publish(login.getName() + " vient de se connecter");
    		vector.add(login);
    		return true;
    	}
     
    	public void publish(String publish) throws RemoteException {
    		System.out.println(publish);
    		for (int i = 0; i < vector.size(); i++) {
    			try {
    				IClient tmp = (IClient) vector.get(i);
    				tmp.tell(publish);
    			} catch (Exception e) {
    				// problem with the client not connected.
     
    			}
    		}
    	}
     
    	public Vector<IClient> getConnected() throws RemoteException {
    		return vector;
    	}
    }
    Le Client
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package tchat;
     
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
     
    public class Client extends UnicastRemoteObject implements IClient {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	private String name;
    	private ChatUI ui;
     
    	public Client(String n) throws RemoteException {
    		setName(n);
    	}
     
    	public void tell(String st) throws RemoteException {
    		System.out.println(st);
    		ui.writeMsg(st);
    	}
     
    	public String getName() throws RemoteException {
    		return name;
    	}
     
    	public void setGUI(ChatUI t) {
    		ui = t;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    Pour démarrer le serveur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package tchat;
     
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.rmi.Naming;
     
    public class StartServer {
    	static String adServer;
     
    	public static void AdressServer() throws UnknownHostException {
    		InetAddress monIP = InetAddress.getLocalHost();
    		adServer = monIP.getHostAddress();
     
    		return;
    	}
     
    	public static void main(String[] args) throws UnknownHostException {
     
    		try {
     
    			java.rmi.registry.LocateRegistry.createRegistry(1099);
    			AdressServer();
    			IServer iserver = new Server();
    			Naming.rebind("rmi://" + adServer + "/myabc", iserver);
    			System.out.println("[System] Le server est prêt");
    		} catch (Exception e) {
    			System.out.println(" Server échec: " + e);
    		}
     
    		System.out.println("IP of my system is := " + adServer);
    	}
     
    }
    Enfin l'interface graphique
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    package tchat;
     
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Vector;
     
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
     
    public class ChatUI implements Remote {
    	private Client client;
    	private IServer server;
    	String connexion = "Connexion";
    	JTextArea tx;
    	JTextField tf, ip, name;
    	JButton connect, send, disconnect;
    	JList<String> lst;
    	JFrame frame;
     
    	public void doConnect() {
    		if (connect.getText().equals(connexion)) {
    			if (name.getText().length() < 2) {
    				JOptionPane.showMessageDialog(frame, "Veuillez écrire un nom");
    				return;
    			}
    			if (ip.getText().length() < 2) {
    				JOptionPane.showMessageDialog(frame, "Veuillez entrer une IP");
    				return;
    			}
    			try {
    				client = new Client(name.getText());
    				client.setGUI(this);
    				server = (IServer) Naming.lookup("rmi://" + ip.getText() + "/myabc");
    				server.login(client);
    				updateUsers(server.getConnected());
    				connect.setText("Déconnexion");
    			} catch (Exception e) {
    				e.printStackTrace();
    				JOptionPane.showMessageDialog(frame, "ERREUR, Connexion impossible ...");
    			}
    		} else {
    			updateUsers(null);
    			connect.setText(connexion);
     
    		}
    	}
     
    	public void disconnectServer() throws RemoteException {
    		try {
    			// Unregister ourself
    			Naming.unbind("rmi://" + StartServer.adServer + "/myabc");
    			JOptionPane.showMessageDialog(frame, "Serveur éteint");
    			// Unexport; this will also remove us from the RMI runtime
    			UnicastRemoteObject.unexportObject(this, true);
     
    		} catch (Exception e) {
    		}
    	}
     
    	public void sendText() {
    		if (connect.getText().equals(connexion)) {
    			JOptionPane.showMessageDialog(frame, "Veuillez vous connecter d'abord");
    			return;
    		}
    		String st = tf.getText();
    		st = "[" + name.getText() + "] " + st;
    		tf.setText("");
    		// Remove if you are going to implement for remote invocation
    		try {
    			server.publish(st);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void writeMsg(String st) {
    		tx.setText(tx.getText() + "\n" + st);
    	}
     
    	public void updateUsers(@SuppressWarnings("rawtypes") Vector vector) {
    		DefaultListModel<String> listModel = new DefaultListModel<String>();
    		if (vector != null)
    			for (int i = 0; i < vector.size(); i++) {
    				try {
    					String tmp = ((IClient) vector.get(i)).getName();
    					listModel.addElement(tmp);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		lst.setModel(listModel);
    	}
     
    	public static void main(String[] args) {
    		System.out.println("Hello World !");
    		@SuppressWarnings("unused")
    		ChatUI c = new ChatUI();
    	}
     
    	// Interface utilisateur
    	public ChatUI() {
     
    		// InetAddress monIP=InetAddress.getLocalHost();
    		// String nosIp= monIP.getHostName();
     
    		frame = new JFrame("Chat");
    		JPanel main = new JPanel();
    		JPanel top = new JPanel();
    		JPanel cn = new JPanel();
    		JPanel bottom = new JPanel();
    		JPanel left = new JPanel();
    		ip = new JTextField();
    		tf = new JTextField();
    		name = new JTextField();
    		tx = new JTextArea();
    		connect = new JButton("Connexion");
    		send = new JButton("Envoyer");
    		disconnect = new JButton("Eteindre Serveur");
    		lst = new JList<String>();
    		main.setLayout(new BorderLayout(5, 5));
    		top.setLayout(new GridLayout(1, 0, 5, 5));
    		cn.setLayout(new BorderLayout(5, 5));
    		bottom.setLayout(new BorderLayout(5, 5));
    		left.setLayout(new GridLayout(1, 0, 5, 5));
     
    		top.add(new JLabel("Pseudo: "));
    		top.add(name);
    		top.add(new JLabel("Adresse Server: "));
    		top.add(ip);
    		top.add(connect);
    		top.add(disconnect);
     
    		left.add(new JLabel(" serveur dispo :"));
    		// left.add(adServer);
     
    		cn.add(new JScrollPane(tx), BorderLayout.CENTER);
    		cn.add(lst, BorderLayout.EAST);
     
    		bottom.add(tf, BorderLayout.CENTER);
    		bottom.add(send, BorderLayout.EAST);
     
    		main.add(top, BorderLayout.NORTH);
    		main.add(cn, BorderLayout.CENTER);
    		main.add(bottom, BorderLayout.SOUTH);
    		main.add(left, BorderLayout.WEST);
    		main.setBorder(new EmptyBorder(10, 10, 10, 10));
     
    		// Evenements
    		connect.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				doConnect();
    			}
    		});
    		send.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				sendText();
    			}
    		});
    		tf.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				sendText();
    			}
    		});
    		disconnect.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				try {
    					disconnectServer();
    					System.out.println("Le server est éteint");
    				} catch (RemoteException e1) {
    					e1.printStackTrace();
    				}
     
    			}
    		});
     
    		frame.setContentPane(main);
    		frame.setSize(600, 600);
    		frame.setVisible(true);
    	}
     
    }

  2. #2
    Membre expérimenté
    Avatar de yotta
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Septembre 2006
    Messages
    1 088
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2006
    Messages : 1 088
    Points : 1 540
    Points
    1 540
    Par défaut
    Bonjour,
    Avez-vous parcouru la FAQ dédiée à JUnit de ce Forum (FAQ Tests) ?
    Une technologie n'est récalcitrante que par ce qu'on ne la connait et/ou comprend pas, rarement par ce qu'elle est mal faite.
    Et pour cesser de subir une technologie récalcitrante, n'hésitez surtout pas à visiter les Guides/Faq du site !

    Voici une liste non exhaustive des tutoriels qui me sont le plus familiers :
    Tout sur Java, du débutant au pro : https://java.developpez.com/cours/
    Tout sur les réseaux : https://reseau.developpez.com/cours/
    Tout sur les systèmes d'exploitation : https://systeme.developpez.com/cours/
    Tout sur le matériel : https://hardware.developpez.com/cours/

Discussions similaires

  1. Besoin d'aide (TOMCAT + MYSQL + RMI )
    Par dheos dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 08/11/2011, 01h12
  2. Besoin d'Aide Java RMI
    Par Invité dans le forum Java EE
    Réponses: 2
    Dernier message: 16/05/2011, 15h25
  3. SCJD - Besoin d'aide avec RMI
    Par nuriel2 dans le forum Persistance des données
    Réponses: 4
    Dernier message: 04/01/2011, 22h17
  4. probléme avec Rmi besoin d'aide
    Par seifdev dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 27/12/2009, 06h27
  5. Besoin d'aide pour l'I.A. d'un puissance 4
    Par Anonymous dans le forum C
    Réponses: 2
    Dernier message: 25/04/2002, 17h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo