1 package storagetesting;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
10 
11 import com.sun.star.embed.*;
12 import com.sun.star.container.XNameAccess;
13 
14 import storagetesting.TestHelper;
15 import storagetesting.StorageTest;
16 
17 public class Test03 implements StorageTest {
18 
19 	XMultiServiceFactory m_xMSF;
20 	XSingleServiceFactory m_xStorageFactory;
21 	TestHelper m_aTestHelper;
22 
23 	public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
24 	{
25 		m_xMSF = xMSF;
26 		m_xStorageFactory = xStorageFactory;
27 		m_aTestHelper = new TestHelper( "Test03: " );
28 	}
29 
30     public boolean test()
31 	{
32 		try
33 		{
34 			// create temporary storage based on arbitrary medium
35 			// after such a storage is closed it is lost
36 			Object oTempStorage = m_xStorageFactory.createInstance();
37 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
38 			if ( xTempStorage == null )
39 			{
40 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
41 				return false;
42 			}
43 
44 			// open a new substorage
45 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
46 																		"SubStorage1",
47 																		ElementModes.ELEMENT_WRITE );
48 			if ( xTempSubStorage == null )
49 			{
50 				m_aTestHelper.Error( "Can't create substorage!" );
51 				return false;
52 			}
53 
54 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
55 
56 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
57 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
58 				return false;
59 
60 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
61 
62 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
63 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
64 				return false;
65 
66 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
67 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
68 															"MediaType3",
69 															false,
70 															ElementModes.ELEMENT_WRITE ) )
71 				return false;
72 
73 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
74 				return false;
75 
76 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
77 				return false;
78 
79 			// ================================================
80 			// check storage hyerarchy tree
81 			// ================================================
82 
83 			// check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
84 			try {
85 				xTempStorage.isStorageElement( "does not exist" );
86 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
87 				return false;
88 			}
89 			catch( com.sun.star.container.NoSuchElementException ne )
90 			{
91 			}
92 			catch( Exception e )
93 			{
94 				m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
95 				return false;
96 			}
97 
98 			try {
99 				xTempStorage.isStreamElement( "does not exist" );
100 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
101 				return false;
102 			}
103 			catch( com.sun.star.container.NoSuchElementException ne )
104 			{
105 			}
106 			catch( Exception e )
107 			{
108 				m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
109 				return false;
110 			}
111 
112 			XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
113 			if ( xRootNameAccess == null )
114 			{
115 				m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
116 				return false;
117 			}
118 
119 			try {
120 				if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
121 				{
122 					m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
123 					return false;
124 				}
125 
126 				if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
127 				{
128 					m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
129 					return false;
130 				}
131 			}
132 			catch( Exception e )
133 			{
134 				m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
135 				return false;
136 			}
137 
138 
139 			// check that root storage contents are represented correctly
140 			String sRootCont[] = xRootNameAccess.getElementNames();
141 
142 			if ( sRootCont.length != 2 )
143 			{
144 				m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
145 				return false;
146 			}
147 
148 			if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
149 			     || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
150 			  || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
151 			{
152 				m_aTestHelper.Error( "Root storage contains wrong list of children!" );
153 				return false;
154 			}
155 
156 			// get storage through XNameAccess
157 			XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
158 			if ( xResultSubStorage == null )
159 				return false;
160 
161 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
162 				return false;
163 
164 			XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
165 			if ( xChildAccess == null )
166 			{
167 				m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
168 				return false;
169 			}
170 
171 			if ( !xChildAccess.hasByName( "SubStream2" )
172 			  || !xResultSubStorage.isStreamElement( "SubStream2" )
173 			  || xResultSubStorage.isStorageElement( "SubStream2" ) )
174 			{
175 				m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
176 				return false;
177 			}
178 
179 			return true;
180 		}
181 		catch( Exception e )
182 		{
183 			m_aTestHelper.Error( "Exception: " + e );
184 			return false;
185 		}
186     }
187 
188 	public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
189 	{
190 		try
191 		{
192 			Object oStorage = xAccess.getByName( sName );
193 			XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
194 
195 			if ( xResult != null )
196 				return xResult;
197 			else
198 				m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
199 		}
200 		catch( Exception e )
201 		{
202 			m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
203 		}
204 
205 		return null;
206 	}
207 
208 }
209 
210