View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.reboot;
8   
9   import java.io.BufferedReader;
10  import java.io.ByteArrayInputStream;
11  import java.io.ByteArrayOutputStream;
12  import java.io.File;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.io.InputStreamReader;
16  import java.net.URL;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class MuleContainerBootstrapUtilsTestCase
27  {
28  
29      @Before
30      public void setUp()
31      {
32          System.setProperty("mule.home", "foo");
33      }
34  
35      /**
36       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#isStandalone()}.
37       */
38      @Test
39      public void testIsStandaloneTrue()
40      {        
41          assertTrue(MuleContainerBootstrapUtils.isStandalone());
42      }
43  
44      /**
45       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#getMuleHome()}.
46       */
47      @Test
48      public void testGetMuleHomeFile()
49      {
50          File muleHome = MuleContainerBootstrapUtils.getMuleHome();
51          assertNotNull(muleHome.getAbsolutePath());
52      }
53  
54      /**
55       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#getMuleAppsDir()}.
56       */
57      @Test
58      public void testGetMuleAppsFile()
59      {
60          File muleApps = MuleContainerBootstrapUtils.getMuleAppsDir();
61          assertNotNull(muleApps.getAbsolutePath());
62      }
63  
64      /**
65       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#getMuleLibDir()}.
66       */
67      @Test
68      public void testGetMuleLibDir()
69      {   File muleLib = MuleContainerBootstrapUtils.getMuleLibDir();
70          assertNotNull(muleLib.getAbsolutePath());
71      }
72  
73      /**
74       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#getMuleLocalJarFile()}.
75       */
76      @Test
77      public void testGetMuleLocalJarFile()
78      {   File muleLocalJar = MuleContainerBootstrapUtils.getMuleLocalJarFile(); 
79          assertNotNull(muleLocalJar.getAbsolutePath());
80      }
81  
82      /**
83       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#getResource(java.lang.String, java.lang.Class)}.
84       * @throws IOException 
85       */
86      @Test
87      public void testGetResource() throws IOException
88      {
89          URL resource = MuleContainerBootstrapUtils.getResource("test-resource.txt", this.getClass());        
90          assertNotNull(resource);
91          Object content = resource.getContent();
92          assertTrue(content instanceof InputStream);
93          BufferedReader in = new BufferedReader(new InputStreamReader((InputStream)content));
94          assertEquals("msg=Hello World", in.readLine());
95      }
96  
97      /**
98       * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#renameFile(java.io.File, java.io.File)}.
99       * @throws IOException 
100      */
101     @Test
102     public void testRenameFile() throws IOException
103     {
104         File source = File.createTempFile("foo", ".tmp");
105         File dest = new File(source.getParent() + File.separatorChar + "dest" + System.currentTimeMillis() + ".tmp");
106         assertFalse(dest.exists());
107         MuleContainerBootstrapUtils.renameFile(source, dest);
108         assertTrue(dest.exists());
109         assertFalse(source.exists());
110         source.delete();
111         dest.delete();
112     }
113 
114     /**
115      * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#renameFileHard(java.io.File, java.io.File)}.
116      * @throws IOException 
117      */
118     @Test
119     public void testRenameFileHard() throws IOException
120     {
121         File source = File.createTempFile("foo2", ".tmp");
122         File dest = new File(source.getParent() + File.separatorChar + "dest2" + System.currentTimeMillis() + ".tmp");
123         assertFalse(dest.exists());
124         MuleContainerBootstrapUtils.renameFileHard(source, dest);
125         assertTrue(dest.exists());
126         assertFalse(source.exists());
127         source.delete();
128         dest.delete();
129     }
130 
131     /**
132      * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#copy(java.io.InputStream, java.io.OutputStream)}.
133      * @throws IOException 
134      */
135     @Test
136     public void testCopy() throws IOException
137     {
138         byte[] b = {0,1,2};
139         ByteArrayInputStream input = new ByteArrayInputStream(b, 0, Integer.MAX_VALUE);
140         ByteArrayOutputStream output = new ByteArrayOutputStream();
141         int i = MuleContainerBootstrapUtils.copy(input, output);
142         assertEquals(b.length, i);
143     }
144 
145     /**
146      * Test method for {@link org.mule.module.reboot.MuleContainerBootstrapUtils#copyLarge(java.io.InputStream, java.io.OutputStream)}.
147      * @throws IOException 
148      */
149     @Test
150     public void testCopyLarge() throws IOException
151     {
152         byte[] b = {0,1,2};
153         ByteArrayInputStream input = new ByteArrayInputStream(b, 0, Integer.MAX_VALUE);
154         ByteArrayOutputStream output = new ByteArrayOutputStream();
155         long i = MuleContainerBootstrapUtils.copyLarge(input, output);
156         assertEquals(b.length, i);
157     }
158 }
159 
160