alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.lib.cvsclient;

import junit.framework.*;
import org.netbeans.junit.*;

import org.netbeans.lib.cvsclient.CVSRoot;

/**
 * Test of CVSRoot class.
 *
 * @author  Martin Entlicher
 */
public class CVSRootTest extends NbTestCase {
    
    /** Creates a new instance of CVSRootTest */
    public CVSRootTest(String name) {
        super(name);
    }
    
    private void compareRoot(CVSRoot root, String method, String user,
                             String password, String host, int port, String repository) {
        if (!(method == root.getMethod())) {
            fail("Bad connection method is parsed: '"+root.getMethod()+"', expected was '"+method+"'");
        }
        if (user != null && !user.equals(root.getUserName())) {
            fail("Bad user name is parsed: '"+root.getUserName()+"', expected was '"+user+"'");
        }
        if (password != null && !password.equals(root.getPassword())) {
            fail("Bad password is parsed: '"+root.getPassword()+"', expected was '"+password+"'");
        }
        if (host != null && !host.equals(root.getHostName())) {
            fail("Bad host name is parsed: '"+root.getHostName()+"', expected was '"+host+"'");
        }
        if (port != root.getPort()) {
            fail("Bad port is parsed: '"+root.getPort()+"', expected was '"+port+"'");
        }
        if (!repository.equals(root.getRepository())) {
            fail("Bad repository is parsed: '"+root.getRepository()+"', expected was '"+repository+"'");
        }
    }
    
    /**
     * Test of CVSRoot.parse() method.
     * It should parse the CVSROOT String of the form
     * [:method:][[user][:password]@][hostname:[port]]/path/to/repository
     *
     * For remote repositories the colon between hostname and path to repository
     * is optional when port is not specified:
     * [:method:][[user][:password]@]hostname[:[port]]/path/to/repository
     */
    public void testParseCorrectURLS() {
        CVSRoot root = CVSRoot.parse("/path/to/repository");
        compareRoot(root, null, null, null, null, 0, "/path/to/repository");
        root = CVSRoot.parse(":local:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_LOCAL, null, null, null, 0, "/path/to/repository");
        root = CVSRoot.parse(":local:user@hostname:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_LOCAL, null, null, null, 0, "user@hostname:/path/to/repository");
        root = CVSRoot.parse("hostname:/path/to/repository");
        compareRoot(root, null, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse("hostname:/path:/to/repository");
        compareRoot(root, null, null, null, "hostname", 0, "/path:/to/repository");
        root = CVSRoot.parse(":server:hostname:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_SERVER, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:hostname:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:user@hostname:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, "user", null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:user:password@hostname:/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, "user", "password", "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:hostname:2403/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, null, null, "hostname", 2403, "/path/to/repository");
        root = CVSRoot.parse(":pserver:user:password@hostname:2403/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, "user", "password", "hostname", 2403, "/path/to/repository");
        
        // No last colon:
        root = CVSRoot.parse("hostname/path/to/repository");
        compareRoot(root, null, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":server:hostname/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_SERVER, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:hostname/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, null, null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:user@hostname/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, "user", null, "hostname", 0, "/path/to/repository");
        root = CVSRoot.parse(":pserver:user:password@hostname/path/to/repository");
        compareRoot(root, CVSRoot.METHOD_PSERVER, "user", "password", "hostname", 0, "/path/to/repository");
    }
    
    /**
     * Test of CVSRoot.parse() method.
     * It should not parse the CVSROOT String if not of the form
     * [:method:][[user][:password]@][hostname:[port]]/path/to/repository
     */
    public void testParseBadURLS() {
        CVSRoot root;
        boolean isBad = false;
        try {
            root = CVSRoot.parse(":pserver:/path/to/repository");
        } catch (IllegalArgumentException iaex) {
            isBad = true;
        }
        if (!isBad) fail("CVSROOT ':pserver:/path/to/repository' is not considered as bad.");
        isBad = false;
        try {
            root = CVSRoot.parse(":somethig that does not end with a colon");
        } catch (IllegalArgumentException iaex) {
            isBad = true;
        }
        if (!isBad) fail("CVSROOT ':somethig that does not end with a colon' is not considered as bad.");
        isBad = false;
        try {
            root = CVSRoot.parse("somethig that does not have neither slash, nor a colon");
        } catch (IllegalArgumentException iaex) {
            isBad = true;
        }
        if (!isBad) fail("CVSROOT 'somethig that does not have neither slash, nor a colon' is not considered as bad.");
    }
    
    public static Test suite() {
        TestSuite suite = new NbTestSuite(CVSRootTest.class);
        return suite;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.