From 7b861b5cf5c7f3fc738e8b45b1d75362330235fd Mon Sep 17 00:00:00 2001 From: Alexander Pelov Date: Thu, 1 Dec 2005 12:26:39 +0000 Subject: ResourcesService renamed to FileAccessService. --- .../slick/fileaccess/FileAccessServiceLick.java | 57 ++++ .../slick/fileaccess/TestFileAccessService.java | 296 +++++++++++++++++++++ .../slick/fileaccess/fileaccess.slick.manifest.mf | 19 ++ .../slick/history/history.slick.manifest.mf | 2 - .../slick/media/media.slick.manifest.mf | 3 +- .../slick/resources/ResourcesServicesLick.java | 59 ---- .../slick/resources/TestFileAccessService.java | 296 --------------------- .../slick/resources/resources.slick.manifest.mf | 19 -- 8 files changed, 373 insertions(+), 378 deletions(-) create mode 100644 test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java create mode 100644 test/net/java/sip/communicator/slick/fileaccess/TestFileAccessService.java create mode 100644 test/net/java/sip/communicator/slick/fileaccess/fileaccess.slick.manifest.mf delete mode 100644 test/net/java/sip/communicator/slick/resources/ResourcesServicesLick.java delete mode 100644 test/net/java/sip/communicator/slick/resources/TestFileAccessService.java delete mode 100644 test/net/java/sip/communicator/slick/resources/resources.slick.manifest.mf (limited to 'test/net/java') diff --git a/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java b/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java new file mode 100644 index 0000000..684c0b3 --- /dev/null +++ b/test/net/java/sip/communicator/slick/fileaccess/FileAccessServiceLick.java @@ -0,0 +1,57 @@ +package net.java.sip.communicator.slick.fileaccess; + +import org.osgi.framework.*; +import junit.framework.*; +import net.java.sip.communicator.service.fileaccess.FileAccessService; + +import java.util.*; + +import net.java.sip.communicator.util.*; + +/** + * This class launches the bundle which tests the fileaccess bundle. + * This bundle is a set of (j)unit tests. It should be launched by the + * cruisecontrol module. + * + * @author Alexander Pelov + */ +public class FileAccessServiceLick + extends TestSuite + implements BundleActivator +{ + private Logger logger = Logger.getLogger(getClass().getName()); + + protected static FileAccessService fileAccessService = null; + protected static BundleContext bc = null; + public static TestCase tcase = new TestCase(){}; + + /** + * Start the File Access Sevice Implementation Compatibility Kit. + * + * @param bundleContext BundleContext + * @throws Exception + */ + public void start(BundleContext bundleContext) throws Exception + { + FileAccessServiceLick.bc = bundleContext; + setName("FileAccessServiceLick"); + Hashtable properties = new Hashtable(); + properties.put("service.pid", getName()); + + addTestSuite(TestFileAccessService.class); + bundleContext.registerService(getClass().getName(), this, properties); + + logger.debug("Successfully registered " + getClass().getName()); + } + + /** + * stop + * + * @param bundlecontext BundleContext + * @throws Exception + */ + public void stop(BundleContext bundlecontext) throws Exception + { + } + +} diff --git a/test/net/java/sip/communicator/slick/fileaccess/TestFileAccessService.java b/test/net/java/sip/communicator/slick/fileaccess/TestFileAccessService.java new file mode 100644 index 0000000..d8da85a --- /dev/null +++ b/test/net/java/sip/communicator/slick/fileaccess/TestFileAccessService.java @@ -0,0 +1,296 @@ +package net.java.sip.communicator.slick.fileaccess; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Random; + +import junit.framework.TestCase; +import net.java.sip.communicator.service.fileaccess.FileAccessService; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +public class TestFileAccessService extends TestCase { + + /** + * The ConfigurationService that we will be testing. + */ + private FileAccessService fileAccessService = null; + + /** + * Some sample data to be writen. + */ + private static final byte[] testData = + "The quick brown fox jumped over the lazy dog".getBytes(); + + /** + * Random data to be added to the sample data. + */ + private static final Random randomData = new Random(); + + /** + * The persistent directory's name. + */ + private static final String dirName = "fileaccessservice.dir.tst"; + + /** + * The persistent directory's name. + */ + private static final String[] dirNames = { + "fileaccessservice.dir.tst", + "subdir1", + "subdir2" + }; + + /** + * The persistent file's name. + */ + private static final String fileName = "fileaccessservice.tst"; + + + public TestFileAccessService(String name) { + super(name); + BundleContext context = FileAccessServiceLick.bc; + ServiceReference ref = context.getServiceReference( + FileAccessService.class.getName()); + this.fileAccessService = (FileAccessService)context.getService(ref); + } + + + /* + * Test method for 'net.java.sip.communicator.service.fileaccess.FileAccessServiceImpl.getTemporaryFile()' + */ + public void testCreateReadWriteTemporaryFile() { + try { + File tempFile = this.fileAccessService.getTemporaryFile(); + + // The file should be new! + assertEquals(tempFile.length(), 0); + + writeReadFile(tempFile); + } catch (IOException e) { + fail("Error while opening the temp file: " + e.getMessage()); + } + } + + /* + * Test method for 'net.java.sip.communicator.service.fileaccess.FileAccessServiceImpl.getTemporaryFile()' + */ + public void testCreateTemporaryDirectory() throws Exception { + try { + this.fileAccessService.getTemporaryDirectory(); + } catch(IOException e) { + fail("Error creating the temp directory: " + e.getMessage()); + } + } + + /* + * Test method for 'net.java.sip.communicator.service.fileaccess.FileAccessServiceImpl.getTemporaryFile()' + */ + public void testCreateReadWriteFileInTemporaryDirectory() throws Exception { + int testFiles = 10; + File[] files = new File[testFiles]; + byte[][] randomData = new byte[testFiles][]; + + for(int i = 0; i < testFiles; i++) { + File tempDir = null; + try { + tempDir = this.fileAccessService.getTemporaryDirectory(); + } catch(IOException e) { + fail("Error creating the temp directory: " + e.getMessage()); + } + + files[i] = new File(tempDir, fileName); + assertTrue("Error creating file in temp dir", + files[i].createNewFile()); + + randomData[i] = generateRandomData(); + this.writeFile(files[i], randomData[i]); + } + + // Read all files afterwards to ensure that temp directories + // are different + for(int i = 0; i < testFiles; i++) { + this.readFile(files[i], randomData[i]); + } + } + + /* + * Tests if it is possible to create a persistent directory. + */ + public void testCreatePersistentDirectory() throws Exception { + try { + this.fileAccessService.getPrivatePersistentDirectory(dirName); + this.fileAccessService.getPrivatePersistentDirectory(dirNames); + } catch(IOException e) { + fail("Error creating the temp directory: " + e.getMessage()); + } + } + + /* + * Tests if it is possible to create a persistent directory and + * a create file and write and read data to this file. + */ + public void testCreateReadWriteFileInPersistentDirectory() throws Exception { + File privateDir = null; + try { + privateDir = this.fileAccessService.getPrivatePersistentDirectory(dirName); + } catch(IOException e) { + fail("Error creating the private directory: " + e.getMessage()); + } + + File file = new File(privateDir, fileName); + if(file.exists()) { + assertTrue("Persistent file exists. Delete attempt failed. " + + "Have you ran the tests with other user? " + + "Is the file locked?" + file.getAbsolutePath(), file.delete()); + } + assertTrue("Error creating file in dir" + file.getAbsolutePath(), + file.createNewFile()); + this.writeReadFile(file); + + file.delete(); + + assertFalse("Could not clean up created file " + + file.getAbsolutePath(), file.exists()); + } + + /* + * Tests if it is possible for a file to be created if it does not exist + */ + public void testCreatePersistentFile() { + try { + File file = this.fileAccessService.getPrivatePersistentFile(fileName); + + if(!file.exists()) { + // Assert that we CAN create the file if it does not exist + assertTrue(file.createNewFile()); + } + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + /* + * This test will always pass, because it is not guaranteed that + * it is possible for the supplied file to be deleted. It is used in conjunction + * with the other tests + */ + public void testDeletePersistentFile() { + try { + File file = this.fileAccessService.getPrivatePersistentFile(fileName); + + if(file.exists()) { + file.delete(); + } + + } catch (Exception e) { + } + } + + /* + * Tests if it is possible for a file to be created if it does not exist + */ + public void testCreateReadWritePersistentFile() { + + try { + File file = this.fileAccessService.getPrivatePersistentFile(fileName); + + if(!file.exists()) { + assertTrue(file.createNewFile()); + } + + writeReadFile(file); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + + /* + * Tests if it data actually persists between calls + */ + public void testPersistentFilePersistency() { + + try { + File file = this.fileAccessService.getPrivatePersistentFile(fileName); + + if(!file.exists()) { + assertTrue(file.createNewFile()); + } + + writeReadFile(file); + + File newFile = this.fileAccessService.getPrivatePersistentFile(fileName); + + // Assert that those files are in fact the same + assertEquals(file, newFile); + + // and with the same size + assertEquals(file.length(), newFile.length()); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + private void writeReadFile(File file) { + byte[] randomData = generateRandomData(); + writeFile(file, randomData); + readFile(file, randomData); + } + + private byte[] generateRandomData() { + int rndInt = TestFileAccessService.randomData.nextInt(Integer.MAX_VALUE); + return Integer.toHexString(rndInt).getBytes(); + } + + private void writeFile(File file, byte[] randomData) { + assertTrue(file.canWrite()); + + FileOutputStream output = null; + + try { + output = new FileOutputStream(file); + output.write(testData); + output.write(randomData); + output.flush(); + } catch(Exception e) { + fail("Could not write to file: " + e.getMessage()); + } finally { + try { + output.close(); + } catch (IOException e) {} + } + } + + private void readFile(File file, byte[] randomData) { + assertTrue(file.canRead()); + + FileInputStream input = null; + byte[] readBuff = new byte[testData.length + randomData.length]; + + try { + input = new FileInputStream(file); + input.read(readBuff); + } catch(Exception e) { + fail("Could not read from file: " + e.getMessage()); + } finally { + try { + input.close(); + } catch (IOException e) {} + } + + // Check if testData was correctly written + for(int i = 0; i < testData.length; i++) { + assertEquals(readBuff[i], testData[i]); + } + + // Check if randomData was correctly written + for(int i = 0; i < randomData.length; i++) { + assertEquals(readBuff[testData.length+i], randomData[i]); + } + } +} diff --git a/test/net/java/sip/communicator/slick/fileaccess/fileaccess.slick.manifest.mf b/test/net/java/sip/communicator/slick/fileaccess/fileaccess.slick.manifest.mf new file mode 100644 index 0000000..5d36ea4 --- /dev/null +++ b/test/net/java/sip/communicator/slick/fileaccess/fileaccess.slick.manifest.mf @@ -0,0 +1,19 @@ +Bundle-Activator: net.java.sip.communicator.slick.fileaccess.FileAccessServiceLick +Bundle-Name: File Access Service Implementation Compatibility Kit +Bundle-Description: A Service Implementation Compatibility Kit for the File Access Services +Bundle-Vendor: sip-communicator.org +Bundle-Version: 0.0.1 +Import-Package: junit.framework, + net.java.sip.communicator.slick.fileaccess, + net.java.sip.communicator.service.fileaccess, + net.java.sip.communicator.service.configuration, + org.osgi.framework, + org.w3c.dom, + javax.xml.parsers, + net.java.sip.communicator.util, + net.java.sip.communicator.util.xml, + javax.xml.transform, + javax.xml.transform.dom, + javax.xml.transform.stream, + org.apache.xml.serializer, +Export-Package: net.java.sip.communicator.slick.fileaccess, diff --git a/test/net/java/sip/communicator/slick/history/history.slick.manifest.mf b/test/net/java/sip/communicator/slick/history/history.slick.manifest.mf index f77518b..f8757c8 100644 --- a/test/net/java/sip/communicator/slick/history/history.slick.manifest.mf +++ b/test/net/java/sip/communicator/slick/history/history.slick.manifest.mf @@ -6,10 +6,8 @@ Bundle-Version: 0.0.1 Import-Package: junit.framework, net.java.sip.communicator.slick.history, net.java.sip.communicator.service.history.records, - net.java.sip.communicator.service.history.query, net.java.sip.communicator.service.history, net.java.sip.communicator.service.configuration, - net.java.sip.communicator.service.protocol, org.osgi.framework, org.w3c.dom, javax.xml.parsers, diff --git a/test/net/java/sip/communicator/slick/media/media.slick.manifest.mf b/test/net/java/sip/communicator/slick/media/media.slick.manifest.mf index b7cc1f4..7fa5bc7 100644 --- a/test/net/java/sip/communicator/slick/media/media.slick.manifest.mf +++ b/test/net/java/sip/communicator/slick/media/media.slick.manifest.mf @@ -5,10 +5,9 @@ Bundle-Vendor: sip-communicator.org Bundle-Version: 0.0.1 Import-Package: junit.framework, org.osgi.framework, + net.java.sip.communicator.slick.media, net.java.sip.communicator.service.media, net.java.sip.communicator.service.media.event, - net.java.sip.communicator.service.configuration, - net.java.sip.communicator.service.configuration.event, net.java.sip.communicator.util, javax.media, javax.media.format, diff --git a/test/net/java/sip/communicator/slick/resources/ResourcesServicesLick.java b/test/net/java/sip/communicator/slick/resources/ResourcesServicesLick.java deleted file mode 100644 index ffb64c2..0000000 --- a/test/net/java/sip/communicator/slick/resources/ResourcesServicesLick.java +++ /dev/null @@ -1,59 +0,0 @@ -package net.java.sip.communicator.slick.resources; - -import org.osgi.framework.*; -import junit.framework.*; -import net.java.sip.communicator.service.resources.FileAccessService; - -import java.util.*; - -import net.java.sip.communicator.util.*; - -/** - * This class launches the bundle of which test the resources bundle. - * this bundle is a set of (j)unit tests. It should be launched by the - * cruisecontrol module. - * - * @author Alexander Pelov - */ -public class ResourcesServicesLick - extends TestSuite - implements BundleActivator -{ - private Logger logger = Logger.getLogger(getClass().getName()); - - protected static FileAccessService fileAccessService = null; - protected static BundleContext bc = null; - public static TestCase tcase = new TestCase(){}; - - /** - * Start the Resources Sevice Implementation Compatibility Kit. - * - * @param bundleContext BundleContext - * @throws Exception - */ - public void start(BundleContext bundleContext) throws Exception - { - ResourcesServicesLick.bc = bundleContext; - setName("ResourcesServicesLick"); - Hashtable properties = new Hashtable(); - properties.put("service.pid", getName()); - - addTestSuite(TestFileAccessService.class); - bundleContext.registerService(getClass().getName(), this, properties); - - logger.debug("Successfully registered " + getClass().getName()); - } - - /** - * stop - * - * @param bundlecontext BundleContext - * @throws Exception - */ - public void stop(BundleContext bundlecontext) throws Exception - { - } - - - -} diff --git a/test/net/java/sip/communicator/slick/resources/TestFileAccessService.java b/test/net/java/sip/communicator/slick/resources/TestFileAccessService.java deleted file mode 100644 index 73be00d..0000000 --- a/test/net/java/sip/communicator/slick/resources/TestFileAccessService.java +++ /dev/null @@ -1,296 +0,0 @@ -package net.java.sip.communicator.slick.resources; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Random; - -import junit.framework.TestCase; -import net.java.sip.communicator.service.resources.FileAccessService; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -public class TestFileAccessService extends TestCase { - - /** - * The ConfigurationService that we will be testing. - */ - private FileAccessService fileAccessService = null; - - /** - * Some sample data to be writen. - */ - private static final byte[] testData = - "The quick brown fox jumped over the lazy dog".getBytes(); - - /** - * Random data to be added to the sample data. - */ - private static final Random randomData = new Random(); - - /** - * The persistent directory's name. - */ - private static final String dirName = "fileaccessservice.dir.tst"; - - /** - * The persistent directory's name. - */ - private static final String[] dirNames = { - "fileaccessservice.dir.tst", - "subdir1", - "subdir2" - }; - - /** - * The persistent file's name. - */ - private static final String fileName = "fileaccessservice.tst"; - - - public TestFileAccessService(String name) { - super(name); - BundleContext context = ResourcesServicesLick.bc; - ServiceReference ref = context.getServiceReference( - FileAccessService.class.getName()); - this.fileAccessService = (FileAccessService)context.getService(ref); - } - - - /* - * Test method for 'net.java.sip.communicator.service.resources.FileAccessServiceImpl.getTemporaryFile()' - */ - public void testCreateReadWriteTemporaryFile() { - try { - File tempFile = this.fileAccessService.getTemporaryFile(); - - // The file should be new! - assertEquals(tempFile.length(), 0); - - writeReadFile(tempFile); - } catch (IOException e) { - fail("Error while opening the temp file: " + e.getMessage()); - } - } - - /* - * Test method for 'net.java.sip.communicator.service.resources.FileAccessServiceImpl.getTemporaryFile()' - */ - public void testCreateTemporaryDirectory() throws Exception { - try { - this.fileAccessService.getTemporaryDirectory(); - } catch(IOException e) { - fail("Error creating the temp directory: " + e.getMessage()); - } - } - - /* - * Test method for 'net.java.sip.communicator.service.resources.FileAccessServiceImpl.getTemporaryFile()' - */ - public void testCreateReadWriteFileInTemporaryDirectory() throws Exception { - int testFiles = 10; - File[] files = new File[testFiles]; - byte[][] randomData = new byte[testFiles][]; - - for(int i = 0; i < testFiles; i++) { - File tempDir = null; - try { - tempDir = this.fileAccessService.getTemporaryDirectory(); - } catch(IOException e) { - fail("Error creating the temp directory: " + e.getMessage()); - } - - files[i] = new File(tempDir, fileName); - assertTrue("Error creating file in temp dir", - files[i].createNewFile()); - - randomData[i] = generateRandomData(); - this.writeFile(files[i], randomData[i]); - } - - // Read all files afterwards to ensure that temp directories - // are different - for(int i = 0; i < testFiles; i++) { - this.readFile(files[i], randomData[i]); - } - } - - /* - * Tests if it is possible to create a persistent directory. - */ - public void testCreatePersistentDirectory() throws Exception { - try { - this.fileAccessService.getPrivatePersistentDirectory(dirName); - this.fileAccessService.getPrivatePersistentDirectory(dirNames); - } catch(IOException e) { - fail("Error creating the temp directory: " + e.getMessage()); - } - } - - /* - * Tests if it is possible to create a persistent directory and - * a create file and write and read data to this file. - */ - public void testCreateReadWriteFileInPersistentDirectory() throws Exception { - File privateDir = null; - try { - privateDir = this.fileAccessService.getPrivatePersistentDirectory(dirName); - } catch(IOException e) { - fail("Error creating the private directory: " + e.getMessage()); - } - - File file = new File(privateDir, fileName); - if(file.exists()) { - assertTrue("Persistent file exists. Delete attempt failed. " + - "Have you ran the tests with other user? " + - "Is the file locked?" + file.getAbsolutePath(), file.delete()); - } - assertTrue("Error creating file in dir" + file.getAbsolutePath(), - file.createNewFile()); - this.writeReadFile(file); - - file.delete(); - - assertFalse("Could not clean up created file " + - file.getAbsolutePath(), file.exists()); - } - - /* - * Tests if it is possible for a file to be created if it does not exist - */ - public void testCreatePersistentFile() { - try { - File file = this.fileAccessService.getPrivatePersistentFile(fileName); - - if(!file.exists()) { - // Assert that we CAN create the file if it does not exist - assertTrue(file.createNewFile()); - } - - } catch (Exception e) { - fail(e.getMessage()); - } - } - - /* - * This test will always pass, because it is not guaranteed that - * it is possible for the supplied file to be deleted. It is used in conjunction - * with the other tests - */ - public void testDeletePersistentFile() { - try { - File file = this.fileAccessService.getPrivatePersistentFile(fileName); - - if(file.exists()) { - file.delete(); - } - - } catch (Exception e) { - } - } - - /* - * Tests if it is possible for a file to be created if it does not exist - */ - public void testCreateReadWritePersistentFile() { - - try { - File file = this.fileAccessService.getPrivatePersistentFile(fileName); - - if(!file.exists()) { - assertTrue(file.createNewFile()); - } - - writeReadFile(file); - } catch (Exception e) { - fail(e.getMessage()); - } - } - - - /* - * Tests if it data actually persists between calls - */ - public void testPersistentFilePersistency() { - - try { - File file = this.fileAccessService.getPrivatePersistentFile(fileName); - - if(!file.exists()) { - assertTrue(file.createNewFile()); - } - - writeReadFile(file); - - File newFile = this.fileAccessService.getPrivatePersistentFile(fileName); - - // Assert that those files are in fact the same - assertEquals(file, newFile); - - // and with the same size - assertEquals(file.length(), newFile.length()); - } catch (Exception e) { - fail(e.getMessage()); - } - } - - private void writeReadFile(File file) { - byte[] randomData = generateRandomData(); - writeFile(file, randomData); - readFile(file, randomData); - } - - private byte[] generateRandomData() { - int rndInt = TestFileAccessService.randomData.nextInt(Integer.MAX_VALUE); - return Integer.toHexString(rndInt).getBytes(); - } - - private void writeFile(File file, byte[] randomData) { - assertTrue(file.canWrite()); - - FileOutputStream output = null; - - try { - output = new FileOutputStream(file); - output.write(testData); - output.write(randomData); - output.flush(); - } catch(Exception e) { - fail("Could not write to file: " + e.getMessage()); - } finally { - try { - output.close(); - } catch (IOException e) {} - } - } - - private void readFile(File file, byte[] randomData) { - assertTrue(file.canRead()); - - FileInputStream input = null; - byte[] readBuff = new byte[testData.length + randomData.length]; - - try { - input = new FileInputStream(file); - input.read(readBuff); - } catch(Exception e) { - fail("Could not read from file: " + e.getMessage()); - } finally { - try { - input.close(); - } catch (IOException e) {} - } - - // Check if testData was correctly written - for(int i = 0; i < testData.length; i++) { - assertEquals(readBuff[i], testData[i]); - } - - // Check if randomData was correctly written - for(int i = 0; i < randomData.length; i++) { - assertEquals(readBuff[testData.length+i], randomData[i]); - } - } -} diff --git a/test/net/java/sip/communicator/slick/resources/resources.slick.manifest.mf b/test/net/java/sip/communicator/slick/resources/resources.slick.manifest.mf deleted file mode 100644 index 74b88dc..0000000 --- a/test/net/java/sip/communicator/slick/resources/resources.slick.manifest.mf +++ /dev/null @@ -1,19 +0,0 @@ -Bundle-Activator: net.java.sip.communicator.slick.resources.ResourcesServicesLick -Bundle-Name: Resources Services Implementation Compatibility Kit -Bundle-Description: A Service Implementation Compatibility Kit for the Resources Services -Bundle-Vendor: sip-communicator.org -Bundle-Version: 0.0.1 -Import-Package: junit.framework, - net.java.sip.communicator.slick.resources, - net.java.sip.communicator.service.resources, - net.java.sip.communicator.service.configuration, - org.osgi.framework, - org.w3c.dom, - javax.xml.parsers, - net.java.sip.communicator.util, - net.java.sip.communicator.util.xml, - javax.xml.transform, - javax.xml.transform.dom, - javax.xml.transform.stream, - org.apache.xml.serializer, -Export-Package: net.java.sip.communicator.slick.resources, -- cgit v1.1