Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
26   112   6   5.2
2   53   0.23   5
5     1.2  
1    
 
  ResourceLoaderUtilityTest       Line # 30 26 6 93.9% 0.93939394
 
  (3)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.io.File;
19    import java.io.InputStream;
20    import java.net.URL;
21   
22    import org.qedeq.base.test.QedeqTestCase;
23    import org.qedeq.base.utility.StringUtility;
24   
25    /**
26    * Test {@link ResourceLoaderUtility}.
27    *
28    * @author Michael Meyling
29    */
 
30    public class ResourceLoaderUtilityTest extends QedeqTestCase {
31   
32    private static final String ONCE_THE_COW_JUMPED_OVER_THE_MOON = "once=the cow jumped over the moon";
33   
34    /*
35    * @see TestCase#setUp()
36    */
 
37  3 toggle protected void setUp() throws Exception {
38  3 super.setUp();
39    }
40   
41    /*
42    * @see TestCase#tearDown()
43    */
 
44  3 toggle protected void tearDown() throws Exception {
45  3 super.tearDown();
46    }
47   
48    /**
49    * Test {@link ResourceLoaderUtility#getResourceFile(java.io.File, String, String)}.
50    * TODO mime 20090614: This test must be run against a jar file!
51    *
52    * @throws Exception Test failure.
53    */
 
54  1 toggle public void testGetResourceFile() throws Exception {
55  1 File file = new File("./ResourceLoaderUtilityTestFile.txt");
56  1 if (file.exists()) {
57  0 assertTrue(file.delete());
58    }
59  1 file.createNewFile();
60  1 IoUtility.saveFile(file, ONCE_THE_COW_JUMPED_OVER_THE_MOON,
61    IoUtility.getDefaultEncoding());
62  1 assertEquals(ONCE_THE_COW_JUMPED_OVER_THE_MOON, IoUtility.loadFile(
63    ResourceLoaderUtility.getResourceFile(new File("."), ".",
64    "ResourceLoaderUtilityTestFile.txt").toString(), IoUtility.getDefaultEncoding()));
65  1 String name = ResourceLoaderUtility.class.getName();
66  1 String shortName = StringUtility.getClassName(ResourceLoaderUtility.class);
67  1 final String directory = name.substring(0,
68    name.length() - shortName.length()).replace('.', '/');
69  1 file.delete();
70  1 file = ResourceLoaderUtility.getResourceFile(new File("."), directory,
71    shortName + ".class");
72    // long lastModifiedBefore = file.lastModified();
73    // System.out.println(lastModifiedBefore);
74  1 IoUtility.saveFileBinary(file, IoUtility.loadFileBinary(file));
75  1 long lastModifiedAfter = file.lastModified();
76    // System.out.println(lastModifiedAfter);
77  1 file = ResourceLoaderUtility.getResourceFile(new File("."), directory,
78    shortName + ".class");
79  1 assertEquals(lastModifiedAfter, file.lastModified());
80   
81  1 IoUtility.deleteDir(new File("org"), true); // cleanup:
82    }
83   
84    /**
85    * Test {@link ResourceLoaderUtility#getResourceUrl(String)}.
86    *
87    * @throws Exception Test failure.
88    */
 
89  1 toggle public void testGetResourceUrl() throws Exception {
90  1 String name = ResourceLoaderUtilityTest.class.getName().replace('.', '/') + ".class";
91    // System.out.println(name);
92  1 final URL url = ResourceLoaderUtility.getResourceUrl(name);
93    // System.out.println(url);
94  1 final StringBuffer buffer = new StringBuffer();
95  1 IoUtility.loadFile(url, buffer, IoUtility.getDefaultEncoding());
96  1 assertTrue(buffer.toString().indexOf(ONCE_THE_COW_JUMPED_OVER_THE_MOON) >= 0);
97    }
98   
99    /**
100    * Test {@link ResourceLoaderUtility#getResourceAsStream(String)}.
101    *
102    * @throws Exception Test failure.
103    */
 
104  1 toggle public void testGetResourceAsStream() throws Exception {
105  1 String name = ResourceLoaderUtilityTest.class.getName().replace('.', '/') + ".class";
106    // System.out.println(name);
107  1 final InputStream stream = ResourceLoaderUtility.getResourceAsStream(name);
108  1 assertTrue(IoUtility.loadStreamWithoutException(stream, 30000).indexOf(
109    ONCE_THE_COW_JUMPED_OVER_THE_MOON) >= 0);
110    }
111   
112    }