The purpose of this tutorial is to allow you to become familiar with the first Software Testing Practical, raise any immediate questions you have with your tutor, think about what kind of information you might need to know, and to look at the code you are about to test. You should do the following:
toRelativeURL
method by
changing only one character in the file and then try to work out a
test that would uncover the error.
Source extract for toRelativeURL() : |
---|
/** This method converts an absolute url to an url relative to a given * base-url. The algorithm is somewhat chaotic, but it works (Maybe rewrite * it). Be careful, the method is ".mm"-specific. Something like this should * be included in the librarys, but I couldn't find it. You can create a new * absolute url with "new URL(URL context, URL relative)". */ public static String toRelativeURL(URL base, URL target) { // Precondition: If URL is a path to folder, then it must end with '/' // character. if( (base.getProtocol().equals(target.getProtocol())) && (base.getHost().equals(target.getHost()))) { String baseString = base.getFile(); String targetString = target.getFile(); String result = ""; //remove filename from URL baseString = baseString.substring(0, baseString.lastIndexOf("/")+1); //remove filename from URL targetString = targetString.substring(0, targetString.lastIndexOf("/")+1); //Maybe this causes problems under windows StringTokenizer baseTokens = new StringTokenizer(baseString,"/"); //Maybe this causes problems under windows StringTokenizer targetTokens = new StringTokenizer(targetString,"/"); String nextBaseToken = "", nextTargetToken = ""; //Algorithm while(baseTokens.hasMoreTokens() && targetTokens.hasMoreTokens()) { nextBaseToken = baseTokens.nextToken(); nextTargetToken = targetTokens.nextToken(); if (!(nextBaseToken.equals(nextTargetToken))) { while(true) { result = result.concat("../"); if (!baseTokens.hasMoreTokens()) { break; } nextBaseToken = baseTokens.nextToken(); } while(true) { result = result.concat(nextTargetToken+"/"); if (!targetTokens.hasMoreTokens()) { break; } nextTargetToken = targetTokens.nextToken(); } String temp = target.getFile(); result = result.concat( temp.substring(temp.lastIndexOf("/")+1,temp.length())); return result; } } while(baseTokens.hasMoreTokens()) { result = result.concat("../"); baseTokens.nextToken(); } while(targetTokens.hasMoreTokens()) { nextTargetToken = targetTokens.nextToken(); result = result.concat(nextTargetToken + "/"); } String temp = target.getFile(); result = result.concat( temp.substring(temp.lastIndexOf("/")+1,temp.length())); return result; } return target.toString(); } |
Version 1.3, 2010/01/21 19:01:21
Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail: school-office@inf.ed.ac.uk Please contact our webadmin with any comments or corrections. Logging and Cookies Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh |