KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > extension > test > PackageRepositoryTestCase


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.extension.test;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.jar.Manifest JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.avalon.extension.Extension;
29 import org.apache.avalon.extension.manager.ExtensionManager;
30 import org.apache.avalon.extension.manager.OptionalPackage;
31 import org.apache.avalon.extension.manager.PackageManager;
32 import org.apache.avalon.extension.manager.impl.DefaultExtensionManager;
33
34 /**
35  * TestCases for PackageRepository.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version $Revision: 1.2 $ $Date: 2004/02/24 22:39:31 $
39  */

40 public class PackageRepositoryTestCase
41     extends TestCase
42 {
43     private File JavaDoc m_baseDirectory;
44     private File JavaDoc m_pathElement1;
45     private File JavaDoc m_pathElement2;
46     private File JavaDoc[] m_path;
47
48     public PackageRepositoryTestCase( String JavaDoc name )
49         throws IOException JavaDoc
50     {
51         super( name );
52
53         File JavaDoc root = new File JavaDoc( System.getProperty( "basedir" ) );
54         m_baseDirectory =
55             ( new File JavaDoc( root, "target/test-classes/org/apache/avalon/extension/test/" ) ).getCanonicalFile();
56
57         m_pathElement1 = new File JavaDoc( m_baseDirectory, "path1" );
58         m_pathElement2 = new File JavaDoc( m_baseDirectory, "path2" );
59         m_path = new File JavaDoc[]{m_pathElement1, m_pathElement2};
60     }
61
62     public void testGoodPath()
63         throws Exception JavaDoc
64     {
65         new DefaultExtensionManager( m_path );
66     }
67
68     public void testBadPath()
69         throws Exception JavaDoc
70     {
71         try
72         {
73             final File JavaDoc pathElement3 = new File JavaDoc( m_baseDirectory, "path3" );
74             final File JavaDoc[] path = new File JavaDoc[]{m_pathElement1, m_pathElement2, pathElement3};
75             new DefaultExtensionManager( path );
76         }
77         catch( final IllegalArgumentException JavaDoc iae )
78         {
79             return;
80         }
81
82         assertTrue( "Exceptected to fail with bad path element", false );
83     }
84
85     public void testBasicScanDependencies()
86         throws Exception JavaDoc
87     {
88         final ExtensionManager repository = newPackagerepository();
89         doRepositoryTest( repository );
90     }
91
92     public void testFSScanDependencies()
93         throws Exception JavaDoc
94     {
95         final ExtensionManager repository = new DefaultExtensionManager( m_path );
96         doRepositoryTest( repository );
97     }
98
99     private void doRepositoryTest( final ExtensionManager repository )
100         throws Exception JavaDoc
101     {
102         final PackageManager manager = new PackageManager( repository );
103
104         final Manifest JavaDoc manifest2 = getManifest( "manifest-2.mf" );
105         final Extension extension1 = Extension.getRequired( manifest2 )[ 0 ];
106
107         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
108         final ArrayList JavaDoc unsatisfied = new ArrayList JavaDoc();
109
110         manager.scanDependencies( extension1, new Extension[ 0 ], dependencies, unsatisfied );
111
112         assertEquals( "dependencies Count", 2, dependencies.size() );
113         assertEquals( "unsatisfied Count", 0, unsatisfied.size() );
114
115         final int size = dependencies.size();
116         for( int i = 0; i < size; i++ )
117         {
118             final OptionalPackage optionalPackage = (OptionalPackage)dependencies.get( i );
119             final Extension[] extensions = optionalPackage.getAvailableExtensions();
120             for( int j = 0; j < extensions.length; j++ )
121             {
122                 final String JavaDoc name = extensions[ j ].getExtensionName();
123                 if( !name.equals( "avalon.required1" ) &&
124                     !name.equals( "avalon.required2" ) )
125                 {
126                     assertTrue( "Unexpected extension: " + name, false );
127                 }
128             }
129         }
130     }
131
132     private Manifest JavaDoc getManifest( final String JavaDoc name )
133         throws Exception JavaDoc
134     {
135         final InputStream JavaDoc inputStream = getClass().getResourceAsStream( name );
136         return new Manifest JavaDoc( inputStream );
137     }
138
139     private ExtensionManager newPackagerepository()
140         throws Exception JavaDoc
141     {
142         final TestPackageRepository repository = new TestPackageRepository();
143         repository.addEntry( "manifest-1.mf" );
144         repository.addEntry( "manifest-2.mf" );
145         repository.addEntry( "manifest-3.mf" );
146         repository.addEntry( "manifest-4.mf" );
147         repository.addEntry( "manifest-5.mf" );
148         repository.addEntry( "manifest-6.mf" );
149         repository.addEntry( "manifest-7.mf" );
150         repository.addEntry( "manifest-8.mf" );
151         return repository;
152     }
153 }
154
155 class TestPackageRepository
156     extends DefaultExtensionManager
157 {
158     TestPackageRepository()
159         throws Exception JavaDoc
160     {
161         super( new File JavaDoc[ 0 ] );
162     }
163
164     void addEntry( final String JavaDoc manifestLocation )
165         throws Exception JavaDoc
166     {
167         final InputStream JavaDoc inputStream = getClass().getResourceAsStream( manifestLocation );
168         final Manifest JavaDoc manifest = new Manifest JavaDoc( inputStream );
169         final File JavaDoc file = new File JavaDoc( manifestLocation );
170         final Extension[] available = Extension.getAvailable( manifest );
171         final Extension[] required = Extension.getRequired( manifest );
172
173         cacheOptionalPackage( new OptionalPackage( file, available, required ) );
174     }
175 }
176
Popular Tags