JUnit – Basit bir örnek

http://www.ic.sunysb.edu/stu/yosong/cse219/junit.html

import org.junit.*;

public class ExampleTest {

    /**
     * methods annotated with @BeforeClass are
     * called once before the test methods run
     */
    @BeforeClass
    public static void setUpBeforeClass() {
        ;
    }
   
    /**
     * methods annotated with @AfterClass are
     * called once after all test methods run
     */
    @AfterClass
    public static void tearDownAfterClass() {
        ;
    }
   
    /**
     * methods annotated with @Before are
     * called before every test case method
     */
    @Before
    public void setUp() {
;
    }

    /**
     * methods annotated with @After are
     * called after every test case method
     */
    @After
    public void tearDown() {
;
    }
   
    /**
     * methods annotated with @Test are
     * your actual Unit Test methods
     * test using:
     *    Assert.assertEquals
     *    Assert.assertTrue
     *    Assert.assertFalse
     *    Assert.assertNotNull
     *    Assert.assertNull
     *    etc.
     */
    @Test
    public void firstTest() {
;
    }
   
    @Test
    public void secondTest() {
;
    }

    @Test(expected=Exception.class)
    public void checkExceptionTest() {
;
    }
}


Source: rss

Basit bir portscan programı

portscan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace BaseLib
{
     public class PortScan{

        public static bool Connect(string IPAddress, int Port , int WaitSeconds){

          TcpClient TcpScan = new TcpClient();
try{
bool ConnectSuccess = false;
TcpScan.BeginConnect(IPAddress, Port,
null, null);
for (int i = 0; i <= WaitSeconds; i++) {
System.Threading.
Thread.Sleep(10);
if (TcpScan.Connected){
ConnectSuccess =
true;
break;
}
}

                return ConnectSuccess;
}
catch{

               return false;
}
             finally{
TcpScan.Close();
}
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaseLib;
using System.IO;

namespace WinPortScan{

     public partial class FrmMain : Form{          public FrmMain(){

InitializeComponent();
}

     private void btnScanPort_Click(object sender, EventArgs e){

     btnSave.Enabled = false;
int startPort = Convert.ToInt32(nudMin.Value);
int endPort = Convert.ToInt32(nudMax.Value);
if (startPort > endPort)
{
MessageBox.Show(“End port must greater then the start port”, “Error – PortScan”);
return;
}
Cursor.Current = Cursors.WaitCursor;
for (int CurrPort = startPort; CurrPort <= endPort; CurrPort++)
{
DateTime stopTime;
TimeSpan ts;
DateTime startTime = DateTime.Now;
bool result=PortScan.Connect(this.txtIPNum.Text, CurrPort,int.Parse(nudSeconds.Value.ToString()) );
if (result == true)
{
stopTime =
DateTime.Now;
ts= stopTime – startTime;
txtLog.AppendText(txtIPNum.Text+
“:” + CurrPort + ” open “+DateTime.Now.ToString(“HH:mm:ss:ff”)+“-“+ts.ToString()+“rn”); }
else
{
stopTime =
DateTime.Now;
ts = stopTime – startTime;
txtLog.AppendText(txtIPNum.Text +
“:” + CurrPort + ” closed “ + DateTime.Now.ToString(“HH:mm:ss:ff”) + “-“ + ts.ToString() + “rn”);
}
}
Cursor.Current = Cursors.Arrow;
btnSave.Enabled =
true;
}
     private void btnSave_Click(object sender, EventArgs e){
sfdIpPortScanResults.Filter =
“txt files (*.txt)|*.txt|All files (*.*)|*.*”;
sfdIpPortScanResults.FilterIndex = 1;
sfdIpPortScanResults.RestoreDirectory =
true;
if (sfdIpPortScanResults.ShowDialog()==DialogResult.OK)
{
string fileName = sfdIpPortScanResults.FileName;
SaveFile(fileName);
}
}

     protected void SaveFile(string fileName)
{
try {
Stream stream = File.OpenWrite(fileName);
using(StreamWriter writer=new StreamWriter(stream))
{
writer.Write(txtLog.Text);
}
}

     catch (IOException ex){
MessageBox.Show(ex.Message, “PortScanner”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

   private void btnClear_Click(object sender, EventArgs e){
txtLog.Text =
“”;
btnSave.Enabled =
false;
}
}
}

Source: rss