Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARQ-622 Support global configuration #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
*/
package org.jboss.arquillian.config.impl.extension;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.core.api.InstanceProducer;
Expand All @@ -34,9 +39,14 @@
*/
public class ConfigurationRegistrar
{
private static final Logger log = Logger.getLogger(ConfigurationRegistrar.class.getName());

static final String ARQUILLIAN_XML_PROPERTY = "arquillian.xml";
static final String ARQUILLIAN_XML_DEFAULT = "arquillian.xml";

static final String ARQUILLIAN_XML_FOLDER_PROPERTY = "arquillian.xml.folder.path";
static final String ARQUILLIAN_XML_FOLDER_DEFAULT = System.getProperty("user.home") + File.separator + ".arquillian";

@Inject @ApplicationScoped
private InstanceProducer<ArquillianDescriptor> descriptorInst;

Expand All @@ -60,6 +70,15 @@ public void loadConfiguration(@Observes ManagerStarted event)
}

private InputStream loadArquillianXml()
{
InputStream arquillianXml = loadLocalArquillianXml();
if (arquillianXml == null)
return loadGlobalArquillianXml();

return arquillianXml;
}

private InputStream loadLocalArquillianXml()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return classLoader.getResourceAsStream(getConfigFileName());
Expand All @@ -74,4 +93,40 @@ private String getConfigFileName()
}
return name;
}

private InputStream loadGlobalArquillianXml()
{
try
{
return getGlobalArquillianXml();
}
catch (FileNotFoundException e)
{
log.log(Level.WARNING, "Exception while reading global configuration", e);
return null;
}
}

private InputStream getGlobalArquillianXml() throws FileNotFoundException
{
File globalFolder = new File(getConfigFolderPath());
if (globalFolder.exists()) {
File globalArquillianXml = new File(globalFolder + File.separator + getConfigFileName());
if (globalArquillianXml.exists()) {
return new FileInputStream(globalArquillianXml);
}
}
return null;
}

private String getConfigFolderPath()
{
String folderPath = System.getProperty(ARQUILLIAN_XML_FOLDER_PROPERTY);

if (folderPath == null)
return ARQUILLIAN_XML_FOLDER_DEFAULT;

return folderPath;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.config.impl.extension;

import java.util.List;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.config.descriptor.impl.AssertXPath;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author Davide D'Alto
*/
public class ConfigurationFolderDoesNotExistsRegistrarTestCase extends AbstractManagerTestBase
{
@Inject
private Instance<ArquillianDescriptor> arquillianXmlDesc;

@BeforeClass
public static void setSysprops()
{
System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY, "WRONG_PATH");
}

@AfterClass
public static void clearSysprops()
{
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY);
}

@Test
public void shouldUseDefaultDescriptorWhenFolderDoesnNotExists() throws Exception
{
final String xml = arquillianXmlDesc.get().exportAsString();
AssertXPath.assertXPath(xml, "/arquillian/*");
}

@Override
protected void addExtensions(final List<Class<?>> extensions)
{
extensions.add(ConfigurationRegistrar.class);
super.addExtensions(extensions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.config.impl.extension;

import java.util.List;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.config.descriptor.impl.AssertXPath;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author Davide D'Alto
*/
public class ConfigurationFolderPrioritiesRegistrarTestCase extends AbstractManagerTestBase
{
private static final String CLASS_NAME = ConfigurationFolderPrioritiesRegistrarTestCase.class.getSimpleName();

@Inject
private Instance<ArquillianDescriptor> arquillianXmlDesc;

@BeforeClass
public static void setSysprops()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String folderPath = classLoader.getResource("settings").getFile();
System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY, folderPath);
System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_PROPERTY, CLASS_NAME + "_arquillian.xml");
}

@AfterClass
public static void clearSysprops()
{
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY);
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_PROPERTY);
}

@Test
public void shouldIgnoreTheGlobalConfigurationAndUseTheLocal() throws Exception
{
final String xml = arquillianXmlDesc.get().exportAsString();
AssertXPath.assertXPath(xml, "/arquillian/container/@qualifier", CLASS_NAME);
}

@Override
protected void addExtensions(final List<Class<?>> extensions)
{
extensions.add(ConfigurationRegistrar.class);
super.addExtensions(extensions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.config.impl.extension;

import java.io.File;
import java.util.List;

import junit.framework.Assert;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.config.descriptor.impl.AssertXPath;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author Davide D'Alto
*/
public class ConfigurationFolderRegistrarTestCase extends AbstractManagerTestBase
{
@Inject
private Instance<ArquillianDescriptor> arquillianXmlDesc;

@BeforeClass
public static void setSysprops()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String folderPath = classLoader.getResource("settings").getFile();
System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY, folderPath);
}

@AfterClass
public static void clearSysprops()
{
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY);
}

@Test
public void testDefaultFolder() throws Exception
{
String expected = System.getProperty("user.home") + File.separator + ".arquillian";
String actual = ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_DEFAULT;

Assert.assertEquals("Unexpected default location", expected, actual);
}

@Test
public void shouldFindArquillianXmlInSelectedFolder() throws Exception
{
final String xml = arquillianXmlDesc.get().exportAsString();
AssertXPath.assertXPath(xml, "/arquillian/container/@qualifier", ConfigurationFolderRegistrarTestCase.class.getSimpleName());
}

@Override
protected void addExtensions(final List<Class<?>> extensions)
{
extensions.add(ConfigurationRegistrar.class);
super.addExtensions(extensions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.config.impl.extension;

import java.util.List;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.config.descriptor.impl.AssertXPath;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author Davide D'Alto
*/
public class ConfigurationPathRegistrarTestCase extends AbstractManagerTestBase
{
private static final String CLASS_NAME = ConfigurationFolderPrioritiesRegistrarTestCase.class.getSimpleName();

@Inject
private Instance<ArquillianDescriptor> arquillianXmlDesc;

@BeforeClass
public static void setSysprops()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String folderPath = classLoader.getResource("settings").getFile();
System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY, folderPath);

System.setProperty(ConfigurationRegistrar.ARQUILLIAN_XML_PROPERTY, CLASS_NAME + "_arquillian.xml");
}

@AfterClass
public static void clearSysprops()
{
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_FOLDER_PROPERTY);
System.clearProperty(ConfigurationRegistrar.ARQUILLIAN_XML_PROPERTY);
}

@Test
public void shouldUseSelectedFileAsArquillianXml() throws Exception
{
final String xml = arquillianXmlDesc.get().exportAsString();
AssertXPath.assertXPath(xml, "/arquillian/container/@qualifier", CLASS_NAME);
}

@Override
protected void addExtensions(final List<Class<?>> extensions)
{
extensions.add(ConfigurationRegistrar.class);
super.addExtensions(extensions);
}
}
Loading