KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.extension.test;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.jar.Manifest JavaDoc;
15 import junit.framework.TestCase;
16 import org.apache.avalon.excalibur.extension.DefaultPackageRepository;
17 import org.apache.avalon.excalibur.extension.Extension;
18 import org.apache.avalon.excalibur.extension.OptionalPackage;
19 import org.apache.avalon.excalibur.extension.PackageManager;
20 import org.apache.avalon.excalibur.extension.PackageRepository;
21
22 /**
23  * TestCases for PackageRepository.
24  *
25  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
26  * @version $Revision: 1.9 $ $Date: 2001/12/11 09:53:35 $
27  */

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