package com.ibm.tck.javax.microedition.io.file.FileConnection; import java.io.IOException; import javax.microedition.io.Connector; import javax.microedition.io.file.ConnectionClosedException; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.IllegalModeException; import com.ibm.tck.javax.microedition.io.file.support.TestCaseWithLog; /* * Licensed Materials - Property of IBM, * (c) Copyright IBM Corp. 2003 All Rights Reserved */ public class Mkdir extends TestCaseWithLog { /** * @see com.ibm.tck.client.TestCase#runTests() */ public void runTests() { if (isTestValid("test0001")) test0001(); if (isTestValid("test0002")) test0002(); if (isTestValid("test0003")) test0003(); if (isTestValid("test0004")) test0004(); if (isTestValid("test0005")) test0005(); if (isTestValid("test0006")) test0006(); if (isTestValid("test0007")) test0007(); if (isTestValid("test0008")) test0008(); if (isTestValid("test0009")) test0009(); } /* * mkdir() creates a new directory */ public void test0001() { boolean passed = false; try { FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE); try { addOperationDesc("Deleting directory: " + conn.getURL()); ensureNotExists(conn); addOperationDesc("Creating directory"); conn.mkdir(); boolean exists = conn.exists(); addOperationDesc("exists() returned " + exists); passed = exists == true; } finally { conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("mkdir() creates a new directory", passed); } /* * IOException thrown if parent directory does not exist */ public void test0002() { boolean passed = false; try { FileConnection conn1 = null; FileConnection conn2 = null; try { conn1 = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE); conn2 = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/subdir/", Connector.READ_WRITE); addOperationDesc("Deleting directory: " + conn1.getURL()); ensureNotExists(conn1); addOperationDesc("Attempting to create directory: " + conn2.getURL()); try { conn2.mkdir(); addOperationDesc("No IOException thrown"); passed = false; } catch (IOException e) { addOperationDesc("Expected IOException thrown"); passed = true; } } finally { if (conn1 != null) conn1.close(); if (conn2 != null) conn2.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IOException thrown if parent directory does not exist", passed); } /* * IOException thrown if target is a file */ public void test0003() { boolean passed = false; try { FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"test", Connector.READ_WRITE); try { addOperationDesc("Creating file: " + conn.getURL()); ensureFileExists(conn); addOperationDesc("Attempting to mkdir file"); try { conn.mkdir(); addOperationDesc("No IOException thrown"); passed = false; } catch (IOException e) { addOperationDesc("Expected IOException thrown"); boolean exists= conn.exists(); passed = exists==true; } } finally { conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IOException thrown if target is a file", passed); } /* * IOException thrown if target is an existing directory */ public void test0004() { boolean passed = false; try { FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE); try { addOperationDesc("Creating directory: " + conn.getURL()); ensureDirExists(conn); addOperationDesc("Attempting to mkdir()"); try { conn.mkdir(); addOperationDesc("No IOException thrown"); passed = false; } catch (IOException e) { addOperationDesc("Expected IOException thrown"); boolean exists= conn.exists(); passed = exists==true; } } finally { conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IOException thrown if target is an existing directory", passed); } /* * ConnectionClosedException thrown if connection is closed */ public void test0005() { boolean passed = false; try { FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE); try { addOperationDesc("Deleting directory: " + conn.getURL()); ensureNotExists(conn); addOperationDesc("Closing connection"); conn.close(); try { addOperationDesc("Attempting to call mkdir() on closed connection"); conn.mkdir(); addOperationDesc("No ConnectionClosedException thrown"); passed = false; } catch (ConnectionClosedException e) { addOperationDesc("Expected ConnectionClosedException thrown"); passed = true; } } finally { conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("ConnectionClosedException thrown if invoked on a closed connection", passed); } /* * IllegalModeException thrown for connections opened in Connector.READ mode */ public void test0006() { boolean passed = false; try { addOperationDesc("Opening connection in READ mode"); FileConnection conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ); try { try { conn.mkdir(); addOperationDesc("No IllegalModeException thrown"); passed = false; } catch (IllegalModeException e) { passed = true; addOperationDesc("Expected IllegalModeException thrown"); } } finally { conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IllegalModeException thrown for connections opened in Connector.WRITE mode", passed); } /* * Tests mkdir() in Connector.WRITE mode */ public void test0007() { boolean passed = false; try { addOperationDesc("Opening connection in WRITE mode"); addOperationDesc("Opening connection in WRITE mode"); FileConnection conn = null; FileConnection conn2 = null; try { conn = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.WRITE); conn2 = (FileConnection)Connector.open("file://"+getTestPath()+"testdir/", Connector.READ_WRITE); addOperationDesc("Deleting directory: " + conn.getURL()); ensureNotExists(conn2); addOperationDesc("Creating directory"); conn.mkdir(); boolean exists = conn2.exists(); addOperationDesc("exists() returned " + exists); passed = exists==true; } finally { if (conn != null) conn.close(); if (conn2 != null) conn2.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("Tests mkdir() in Connector.WRITE mode", passed); } /** * IOException or SecurityException thrown if connection is opened on a non-existent file system */ public void test0008() { boolean passed = false; try { FileConnection conn = null; try { String possibleNonExistentFilesystem ="/TCKFileSystem/"; try { conn = (FileConnection)Connector.open("file://"+possibleNonExistentFilesystem+"test", Connector.READ_WRITE); addOperationDesc("Opened connection on a non existent file system: " + conn.getURL()); } catch (SecurityException e) { passed = true; addOperationDesc("Expected SecurityException thrown"); } if (conn!=null) { conn.mkdir(); addOperationDesc("No IOException thrown"); passed = false; } } catch (IOException e) { passed = true; addOperationDesc("Expected IOException thrown"); } finally { if (conn!=null) conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IOException or SecurityException thrown if connection is opened on a non-existent file system", passed); } /** * IOException or SecurityException thrown if connection is opened on a non-existent host */ public void test0009() { boolean passed = false; try { FileConnection conn = null; try { String possibleNonExistentFilesystem ="TCKBogusHost/TCKFileSystem/"; try { conn = (FileConnection)Connector.open("file://"+possibleNonExistentFilesystem+"test", Connector.READ_WRITE); addOperationDesc("Opened connection on a non existent host: " + conn.getURL()); } catch (SecurityException e) { passed = true; addOperationDesc("Expected SecurityException thrown"); } if (conn!=null) { conn.mkdir(); addOperationDesc("No IOException thrown"); passed = false; } } catch (IOException e) { passed = true; addOperationDesc("Expected IOException thrown"); } finally { if (conn!=null) conn.close(); } } catch (Exception e) { logUnexpectedExceptionDesc(e); passed = false; } assertTrueWithLog("IOException or SecurityException thrown if connection is opened on a non-existent host", passed); } }