Skip to content

Commit

Permalink
Added some implementation to Environment Addon
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jan 22, 2013
1 parent 0e5bd46 commit 325d860
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.environment;

/**
* Marker interface needed to restrict the possible keys in an {@link Environment}
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public interface Category
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@

package org.jboss.forge.environment;

import java.util.Map;

/**
* An {@link Environment} stores information separated into categories
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public interface Environment
{

/**
* Returns a mutable map that contains the properties for a specific {@link Category}
*
* @param key
* @return
*/
<K, V> Map<K, V> get(Class<? extends Category> key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,36 @@

package org.jboss.forge.environment.impl;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Singleton;

import org.jboss.forge.container.services.Exported;
import org.jboss.forge.environment.Category;
import org.jboss.forge.environment.Environment;

@Exported
@Singleton
public class EnvironmentImpl implements Environment
{
private Map<Class<? extends Category>, Map<Object, Object>> categorizedMap =
Collections.synchronizedMap(
new IdentityHashMap<Class<? extends Category>, Map<Object, Object>>());

public EnvironmentImpl()
@SuppressWarnings("unchecked")
@Override
public <K, V> Map<K, V> get(Class<? extends Category> key)
{
// TODO Auto-generated constructor stub
Map<Object, Object> map = categorizedMap.get(key);
if (map == null)
{
map = new ConcurrentHashMap<Object, Object>();
categorizedMap.put(key, map);
}
return (Map<K, V>) map;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.convert;
package test.org.jboss.forge.convert;

import java.util.Map;

import javax.inject.Inject;

Expand Down Expand Up @@ -33,6 +35,7 @@ public static ForgeArchive getDeployment()
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
.addPackages(true, CDIEnvironmentTest.class.getPackage())
.addAsLibraries(
Maven.resolver().offline().loadPomFromFile("pom.xml").importRuntimeDependencies().asFile())
.addAsAddonDependencies(
Expand Down Expand Up @@ -60,4 +63,23 @@ public void testNotNull() throws Exception
Assert.assertNotNull(environment);
}

@Test
public void testGetCategory() throws Exception
{
Map<Object, Object> mapTest = environment.get(TestCategory.class);
Map<Object, Object> mapUI = environment.get(UserInterfaceCategory.class);

Assert.assertNotNull(mapTest);
Assert.assertNotNull(mapUI);

Assert.assertNotSame(mapTest, mapUI);

mapTest.put("Key", "Value");
Assert.assertFalse(mapTest.isEmpty());

Map<Object, Object> newMap = environment.get(TestCategory.class);

Assert.assertSame(mapTest, newMap);
Assert.assertEquals("Value", newMap.get("Key"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package test.org.jboss.forge.convert;

import org.jboss.forge.environment.Category;

public interface TestCategory extends Category
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package test.org.jboss.forge.convert;

import org.jboss.forge.environment.Category;

public interface UserInterfaceCategory extends Category
{

}

0 comments on commit 325d860

Please sign in to comment.