Solved

Integration Testing in eclipse

Posted on 2011-03-12
6
407 Views
Last Modified: 2012-05-11
Is there any good plugins in eclipse that does integration testing? To test method calls between specified classes.
0
Comment
Question by:Atouray
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
There's built in support for JUnit (http://www.junit.org/) and this article explains using it within Eclipse (http://www.junit.org/node/49).

There's nothing about JUnit that limits you to writing unit tests.  You can easily write integration tests - where your test classes just calls to methods in other classes.

Doug
0
 

Author Comment

by:Atouray
Comment Utility
I have two classes, and one of them is calling a method from the other. I want to do an integration testing for this.
Class FirstClass{

 public void openDocument(URL url) throws Exception {
        // Originally, this method determined external application, with which the document
        // should be opened. Which application should open which document type was
        // configured in Vmap properties file. As a result, Vmap tried to solve the
        // problem (of determining application for a file type), which should better be
        // solved somewhere else. Indeed, on Windows, this problem is perfectly solved by
        // Explorer. On KDE, this problem is solved by Konqueror default browser. In
        // general, most WWW browsers have to solve this problem.

        // As a result, the only thing we do here, is to open URL in WWW browser.

        String osName = System.getProperty("os.name");
        if (osName.substring(0,3).equals("Win")) {
            String propertyString = new String("default_browser_command_windows");
			if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
				propertyString += "_9x";
			} else {
				propertyString += "_nt";
			}

            String browser_command=new String();
            String command=new String();
            // Here we introduce " around the parameter of explorer
            // command. This is not because of possible spaces in this
            // parameter - it is because of "=" character, which causes
            // problems. My understanding of MSDOS is not so good, but at
            // least I can say, that "=" is used in general for the purpose
            // of variable assignment.
            //String[] call = { browser_command, "\""+url.toString()+"\"" };
            try  {
                // This is working fine on Windows 2000 and NT as well
                // Below is a piece of code showing how to run executables directly
                // without asking. However, we don't want to do that. Explorer will run
                // executable, but ask before it actually runs it.
                //
                // Imagine you download a package of maps containing also nasty
                // executable. Let's say there is a map "index.mm". This map contains a
                // link to that nasty executable, but the name of the link appearing to the
                // user does not indicate at all that clicking the link leads to execution
                // of a programm.  This executable is located on your local computer, so
                // asking before executing remote executable does not solve the
                // problem. You click the link and there you are running evil executable.

                // build string for default browser:
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty(propertyString));
                browser_command = formatter.format(messageArguments);
        
                if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+url.toString(); 
                } else {
                    command = browser_command; 
                }
                Runtime.getRuntime().exec(command); 
            }
            catch(IOException x) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+command+"\".\n\nYou may look at the user or default property called '"+propertyString+"'.");
                System.err.println("Caught: " + x); 
            }
        } else if (osName.startsWith("Mac OS")) {
             String urlString = url.toString();
             String browser_command=new String();
            try {
                // build string for default browser:
                if (url.getProtocol().equals("file")) {
                    urlString=urlString.replace('\\','/').replaceAll(" ","%20"); 
                }

                Object[] messageArguments = { urlString, urlString };
                // ask for property about browser: fc, 26.11.2003.
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_mac"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); 
            } catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_mac'.");
                System.err.println("Caught: " + ex2); 
            }
        } else {
            // There is no '"' character around url.toString (compare to Windows code
            // above). Putting '"' around does not work on Linux - instead, the '"'
            // becomes part of URL, which is malformed, as a result.	 

//             String urlString = url.toString();
//             if (url.getProtocol().equals("file")) {
//                 urlString = urlString.replace('\\','/').replaceAll(" ","%20"); }
//             // ^ This is more of a heuristic than a "logical" code

            String browser_command=new String();
            try {
                // build string for default browser:
                String correctedUrl = new String(url.toExternalForm());
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { correctedUrl, url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_other_os"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); }
            catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_other_os'.");
                System.err.println("Caught: " + ex2); 
            }
        }
    }

}



Class Tools{

 public static String urlGetFile(URL url) {
       String osNameStart = System.getProperty("os.name").substring(0,3);
       String fileSeparator = System.getProperty("file.separator");
       if (osNameStart.equals("Win") && url.getProtocol().equals("file")) {          
          String fileName = url.toString().replaceFirst("^file:","").replace('/','\\');
          return (fileName.indexOf(':') >= 0) ?
             fileName.replaceFirst("^\\\\*","") :
             fileName; } // Network path
       else {
          return url.getFile(); }}


}

Open in new window

0
 

Author Comment

by:Atouray
Comment Utility
I have to test the coupling between these classes.
  if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {


The method Tools.urlGetFile(url) is in the Tools method. How do i go about with the integration testing. This is what I have done so far but i believe it is more of a unit test than an integration test. Can someone guide me through the integration testing?
@Test
      public void T2() throws MalformedURLException {
            try{
                  URL url=new URL("http://www.yahoo.com/edinburgh/lothian/warrender");
                  
                  m_vmap.openDocument(url);
                  
                  String urlInTools = m_cCountField.urlGetFile(url);
                  System.out.println("URLGetFile: "+urlInTools);
                  // formatter.format(url);
                  //assertEquals("/edinburgh/lothian/warrender",urlInTools);
            }
            catch(Exception e) {
                  System.out.println("Error Found: "+e);
                  assertTrue(true);
            }
      }

Thank you
Class FirstClass{

 public void openDocument(URL url) throws Exception {
        // Originally, this method determined external application, with which the document
        // should be opened. Which application should open which document type was
        // configured in Vmap properties file. As a result, Vmap tried to solve the
        // problem (of determining application for a file type), which should better be
        // solved somewhere else. Indeed, on Windows, this problem is perfectly solved by
        // Explorer. On KDE, this problem is solved by Konqueror default browser. In
        // general, most WWW browsers have to solve this problem.

        // As a result, the only thing we do here, is to open URL in WWW browser.

        String osName = System.getProperty("os.name");
        if (osName.substring(0,3).equals("Win")) {
            String propertyString = new String("default_browser_command_windows");
			if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
				propertyString += "_9x";
			} else {
				propertyString += "_nt";
			}

            String browser_command=new String();
            String command=new String();
            // Here we introduce " around the parameter of explorer
            // command. This is not because of possible spaces in this
            // parameter - it is because of "=" character, which causes
            // problems. My understanding of MSDOS is not so good, but at
            // least I can say, that "=" is used in general for the purpose
            // of variable assignment.
            //String[] call = { browser_command, "\""+url.toString()+"\"" };
            try  {
                // This is working fine on Windows 2000 and NT as well
                // Below is a piece of code showing how to run executables directly
                // without asking. However, we don't want to do that. Explorer will run
                // executable, but ask before it actually runs it.
                //
                // Imagine you download a package of maps containing also nasty
                // executable. Let's say there is a map "index.mm". This map contains a
                // link to that nasty executable, but the name of the link appearing to the
                // user does not indicate at all that clicking the link leads to execution
                // of a programm.  This executable is located on your local computer, so
                // asking before executing remote executable does not solve the
                // problem. You click the link and there you are running evil executable.

                // build string for default browser:
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty(propertyString));
                browser_command = formatter.format(messageArguments);
        
                if (url.getProtocol().equals("file")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+Tools.urlGetFile(url);
                } else if (url.toString().startsWith("mailto:")) {
                    command = "rundll32 url.dll,FileProtocolHandler "+url.toString(); 
                } else {
                    command = browser_command; 
                }
                Runtime.getRuntime().exec(command); 
            }
            catch(IOException x) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+command+"\".\n\nYou may look at the user or default property called '"+propertyString+"'.");
                System.err.println("Caught: " + x); 
            }
        } else if (osName.startsWith("Mac OS")) {
             String urlString = url.toString();
             String browser_command=new String();
            try {
                // build string for default browser:
                if (url.getProtocol().equals("file")) {
                    urlString=urlString.replace('\\','/').replaceAll(" ","%20"); 
                }

                Object[] messageArguments = { urlString, urlString };
                // ask for property about browser: fc, 26.11.2003.
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_mac"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); 
            } catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_mac'.");
                System.err.println("Caught: " + ex2); 
            }
        } else {
            // There is no '"' character around url.toString (compare to Windows code
            // above). Putting '"' around does not work on Linux - instead, the '"'
            // becomes part of URL, which is malformed, as a result.	 

//             String urlString = url.toString();
//             if (url.getProtocol().equals("file")) {
//                 urlString = urlString.replace('\\','/').replaceAll(" ","%20"); }
//             // ^ This is more of a heuristic than a "logical" code

            String browser_command=new String();
            try {
                // build string for default browser:
                String correctedUrl = new String(url.toExternalForm());
                // ask for property about browser: fc, 26.11.2003.
                Object[] messageArguments = { correctedUrl, url.toString() };
                MessageFormat formatter = new MessageFormat(getProperty("default_browser_command_other_os"));
                browser_command = formatter.format(messageArguments);
                Runtime.getRuntime().exec(browser_command); }
            catch(IOException ex2) {
                c.errorMessage("Could not invoke browser.\n\nVmap excecuted the following statement on a command line:\n\""+browser_command+"\".\n\nYou may look at the user or default property called 'default_browser_command_other_os'.");
                System.err.println("Caught: " + ex2); 
            }
        }
    }

}



Class Tools{

 public static String urlGetFile(URL url) {
       String osNameStart = System.getProperty("os.name").substring(0,3);
       String fileSeparator = System.getProperty("file.separator");
       if (osNameStart.equals("Win") && url.getProtocol().equals("file")) {          
          String fileName = url.toString().replaceFirst("^file:","").replace('/','\\');
          return (fileName.indexOf(':') >= 0) ?
             fileName.replaceFirst("^\\\\*","") :
             fileName; } // Network path
       else {
          return url.getFile(); }}


}

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
I'm not very clear on how you see the two methods as being related.

An integration test usually takes the form:
 
public void myTest() {
     IntermediateData myData = callFirstMethod() ;
     assertTrue(myData == expectedIntermediateValue) ; 
    
     FinalResult final = callSecondMethod(myData) ;
    assertTrue(final == expectedFinalValue) ; 
 }

Open in new window


At the moment your test is more taking the form:
 
public void myTest() {
    callMethod1() ;

    // This method should depend in some way on the result from method1
    callMethod2() ;
}

Open in new window



I hope that helps,

Doug
0
 

Author Comment

by:Atouray
Comment Utility
Thank you. I did something similar to what you have done, is this integration as well?
public void testCouplingPath() throws MalformedURLException{
		
		//Testing for the operating System
		String ExpectedSystemOS = "Win";
		String osName = System.getProperty("os.name");
		String actualSystemOS = osName.substring(0,3);
		assertEquals("System OS type",ExpectedSystemOS, actualSystemOS);
		
		//Testing for the url protocol
		URL url = new URL("file://www.gambia.west.africa.gm");
		String urlProtocol = url.getProtocol();
		String expectedProtocol = "file";
		assertEquals("URL Protocol",expectedProtocol,urlProtocol);
		
		//testing for the Tools.urlGetFile(url)
		String urlGetFileReturns = Tools.urlGetFile(url);
		String expectedURLReturn = "\\\\www.gambia.west.africa.gm";
		assertEquals("urlGetFile return answer",expectedURLReturn,urlGetFileReturns);
		
	
	}

Open in new window

0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
That looks like a good test but it's still really just a unit test for urlGetFile() and some parts of the openDocument() method.

I think to make it a full integration test you just need to add a call to:
FirstClass.openDocument(url);
probably at the end of the test.

Since openDocument() doesn't actually return anything, it's hard to test it's behavior - except that a call to it doesn't throw an exception.

Doug
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 56
maven java path setting 5 49
Impossible to extract MSI from new JAVA releases 2 37
maven example error 3 34
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now