View Javadoc

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