Carsten Ziegeler | e58ddff | 2013-08-14 05:52:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.scrplugin; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.io.FileOutputStream; |
| 23 | import java.io.IOException; |
| 24 | import java.util.Collections; |
| 25 | import java.util.UUID; |
| 26 | |
| 27 | import org.apache.commons.io.FileUtils; |
| 28 | import org.apache.commons.io.IOUtils; |
| 29 | import org.easymock.EasyMock; |
| 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | public class SCRDescriptorGeneratorTest { |
| 35 | |
| 36 | File folder; |
| 37 | File dest; |
| 38 | |
| 39 | @Before |
| 40 | public void setup() throws IOException { |
| 41 | folder = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString()); |
| 42 | FileUtils.forceMkdir(folder); |
| 43 | |
| 44 | dest = new File(folder, "testComponents"); |
| 45 | FileUtils.forceMkdir(dest); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | @After |
| 50 | public void tearDown() throws IOException { |
| 51 | FileUtils.deleteDirectory(folder); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testSimpleComponent() throws SCRDescriptorException, SCRDescriptorFailureException, IOException { |
| 56 | Env env = new Env("SimpleComponent").invoke(); |
| 57 | EasyMock.replay(env.log()); |
| 58 | env.generator().execute(); |
| 59 | EasyMock.verify(env.log()); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testComponentWithClassReferenceAndMissingInterface() throws SCRDescriptorException, SCRDescriptorFailureException, IOException { |
| 64 | Env env = new Env("ComponentWithClassReferenceAndMissingInterface").invoke(); |
| 65 | EasyMock.replay(env.log()); |
| 66 | try { |
| 67 | env.generator().execute(); |
| 68 | } catch ( final SCRDescriptorFailureException e) { |
| 69 | // this is expected as the interface for a reference is missing |
| 70 | } |
| 71 | EasyMock.verify(env.log()); |
| 72 | } |
| 73 | |
| 74 | private void unpackSource(String resource, File dest) throws IOException { |
| 75 | IOUtils.copy(getClass().getResourceAsStream(resource), new FileOutputStream(dest)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Setups a minimal environment. |
| 80 | */ |
| 81 | private class Env { |
| 82 | private String className; |
| 83 | private Log log; |
| 84 | private SCRDescriptorGenerator gen; |
| 85 | |
| 86 | public Env(String className) { |
| 87 | this.className = className; |
| 88 | } |
| 89 | |
| 90 | public Log log() { |
| 91 | return log; |
| 92 | } |
| 93 | |
| 94 | public SCRDescriptorGenerator generator() { |
| 95 | return gen; |
| 96 | } |
| 97 | |
| 98 | public Env invoke() throws IOException { |
| 99 | File aFile = new File(dest, className + ".class"); |
| 100 | unpackSource("/testComponents/" + className + ".class", aFile); |
| 101 | |
| 102 | log = EasyMock.createNiceMock(Log.class); |
| 103 | gen = new SCRDescriptorGenerator(log); |
| 104 | Project p = new Project(); |
| 105 | p.setClassLoader(getClass().getClassLoader()); |
| 106 | p.setSources(Collections.<Source>singletonList(new SourceImpl("testComponents." + className, aFile))); |
| 107 | |
| 108 | Options o = new Options(); |
| 109 | o.setOutputDirectory(folder); |
| 110 | gen.setProject(p); |
| 111 | gen.setOptions(o); |
| 112 | return this; |
| 113 | } |
| 114 | } |
| 115 | } |