001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.rule.callback;
016    
017    import aQute.bnd.osgi.Builder;
018    import aQute.bnd.osgi.Jar;
019    
020    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
021    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
022    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
023    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
024    
025    import java.io.File;
026    import java.io.InputStream;
027    
028    import java.net.URL;
029    
030    import java.util.Properties;
031    
032    import org.junit.runner.Description;
033    
034    /**
035     * @author Raymond Aug??
036     */
037    public class SyntheticBundleTestCallback extends BaseTestCallback<Long, Long> {
038    
039            public SyntheticBundleTestCallback(String bundlePackageName) {
040                    _bundlePackageName = bundlePackageName;
041            }
042    
043            @Override
044            public void afterClass(Class<?> clazz, Long bundleId) throws Throwable {
045                    if (bundleId == null) {
046                            return;
047                    }
048    
049                    ModuleFrameworkUtilAdapter.stopBundle(bundleId);
050    
051                    ModuleFrameworkUtilAdapter.uninstallBundle(bundleId);
052            }
053    
054            @Override
055            public Long beforeClass(Class<?> clazz) throws Throwable {
056                    InputStream inputStream = createBundle(clazz);
057    
058                    Long bundleId = ModuleFrameworkUtilAdapter.addBundle(
059                            clazz.getName(), inputStream);
060    
061                    ModuleFrameworkUtilAdapter.startBundle(bundleId);
062    
063                    return bundleId;
064            }
065    
066            @Override
067            public void doAfterClass(Description description, Long bundleId)
068                    throws Throwable {
069    
070                    afterClass(description.getTestClass(), bundleId);
071            }
072    
073            @Override
074            public Long doBeforeClass(Description description) throws Throwable {
075                    return beforeClass(description.getTestClass());
076            }
077    
078            protected InputStream createBundle(Class<?> clazz) throws Exception {
079                    Builder builder = new Builder();
080    
081                    builder.setBundleSymbolicName(clazz.getName());
082    
083                    try {
084                            URL url = clazz.getResource("");
085    
086                            String protocol = url.getProtocol();
087    
088                            if (!protocol.equals("file")) {
089                                    throw new IllegalStateException(
090                                            "Test classes are not on the file system");
091                            }
092    
093                            String basePath = url.getPath();
094    
095                            Package pkg = clazz.getPackage();
096    
097                            String packageName = pkg.getName();
098    
099                            int index = basePath.indexOf(packageName.replace('.', '/') + '/');
100    
101                            basePath = basePath.substring(0, index);
102    
103                            File baseDir = new File(basePath);
104    
105                            builder.setBase(baseDir);
106                            builder.setClasspath(new File[] {baseDir});
107                            builder.setProperty(
108                                    "bundle.package", packageName + "." + _bundlePackageName);
109    
110                            Properties properties = builder.getProperties();
111    
112                            InputStream inputStream = clazz.getResourceAsStream(
113                                    _bundlePackageName.replace('.', '/') + "/bnd.bnd");
114    
115                            properties.load(inputStream);
116    
117                            Jar jar = builder.build();
118    
119                            UnsyncByteArrayOutputStream outputStream =
120                                    new UnsyncByteArrayOutputStream();
121    
122                            jar.write(outputStream);
123    
124                            return new UnsyncByteArrayInputStream(outputStream.toByteArray());
125                    }
126                    finally {
127                            builder.close();
128                    }
129            }
130    
131            private final String _bundlePackageName;
132    
133    }