// Path: src/main/java/com/morcinek/android/codegenerator/extractor/string/ResourceIdExtractor.java
// public class ResourceIdExtractor implements StringExtractor<ResourceId> {
// 
//     /**
//      * Extracts resource id from <code>android:id</code> attribute of layout widget.
//      * Attribute values can look like:
//      * - <code>@+id/list</code>
//      * - <code>@android:id/list</code>
//      *
//      * @param idAttribute String value of <code>android:id</code> attribute.
//      * @return ResourceId object
//      */
//     @Override
//     public ResourceId extractFromString(String idAttribute) {
//         IdAttributeUnion attributeUnion = new IdAttributeUnion(idAttribute);
//         return new ResourceId(attributeUnion.name, getNamespace(attributeUnion.prefix));
//     }
// 
//     private String getNamespace(String prefix) {
//         Matcher matcher = Pattern.compile("@([\\w]+):id").matcher(prefix);
//         if (matcher.find()) {
//             return matcher.group(1);
//         }
//         return null;
//     }
// 
//     private class IdAttributeUnion {
// 
//         String prefix;
//         String name;
// 
//         IdAttributeUnion(String idAttribute) {
//             String[] split = idAttribute.split("\\/");
//             prefix = split[0];
//             name = split[1];
//         }
//     }
// }
// 
// Path: src/main/java/com/morcinek/android/codegenerator/extractor/string/ResourceTypeExtractor.java
// public class ResourceTypeExtractor implements StringExtractor<ResourceType> {
// 
//     @Override
//     public ResourceType extractFromString(String typeName) {
//         int lastDot = typeName.lastIndexOf(".");
//         if (lastDot == -1) {
//             return new ResourceType(typeName);
//         } else {
//             return new ResourceType(typeName.substring(lastDot + 1), typeName.substring(0, lastDot));
//         }
//     }
// }
// 
// Path: src/main/java/com/morcinek/android/codegenerator/extractor/model/Resource.java
// public class Resource {
// 
//     private ResourceId resourceId;
// 
//     private ResourceType resourceType;
// 
//     public Resource(ResourceId resourceId, ResourceType resourceType) {
//         this.resourceId = resourceId;
//         this.resourceType = resourceType;
//     }
// 
//     public ResourceId getResourceId() {
//         return resourceId;
//     }
// 
//     public ResourceType getResourceType() {
//         return resourceType;
//     }
// }
// 
// Path: src/test/java/com/morcinek/android/codegenerator/util/InputStreamProvider.java
// public class InputStreamProvider {
// 
//     public InputStream getStreamFromResource(String name) {
//         return getClass().getResourceAsStream("/" + name);
//     }
// }


import com.morcinek.android.codegenerator.extractor.string.ResourceIdExtractor;
import com.morcinek.android.codegenerator.extractor.string.ResourceTypeExtractor;
import com.morcinek.android.codegenerator.extractor.model.Resource;
import com.morcinek.android.codegenerator.util.InputStreamProvider;
import org.fest.assertions.Assertions;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
    @Test
    public void specificTest() throws Exception {
        // given
        InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/specific.xml");

        // when
        List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

        // then
        Assertions.assertThat(resources).isNotNull().hasSize(1);
        assertResource(resources.get(0), "ViewPager", "android.support.v4.view", "pager", null);
    }

    @Test
    public void locationsMapTest() throws Exception {
        // given
        InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/locations_map.xml");

        // when
        List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

        // then
        Assertions.assertThat(resources).isNotNull().hasSize(4);
        assertResource(resources.get(0), "ActionBar", "com.morcinek.budget.ui.widgets", "actionbar", null);
        assertResource(resources.get(1), "RelativeLayout", null, "map_frame_layout", null);
        assertResource(resources.get(2), "FrameLayout", null, "map_container", null);
        assertResource(resources.get(3), "ImageButton", null, "home_button", null);
    }

    private void assertResource(Resource resource, String className, String packageName, String idName, String idNamespace) {
// Path: src/main/java/com/morcinek/android/codegenerator/extractor/string/ResourceIdExtractor.java
// public class ResourceIdExtractor implements StringExtractor<ResourceId> {
// 
//     /**
//      * Extracts resource id from <code>android:id</code> attribute of layout widget.
//      * Attribute values can look like:
//      * - <code>@+id/list</code>
//      * - <code>@android:id/list</code>
//      *
//      * @param idAttribute String value of <code>android:id</code> attribute.
//      * @return ResourceId object
//      */
//     @Override
//     public ResourceId extractFromString(String idAttribute) {
//         IdAttributeUnion attributeUnion = new IdAttributeUnion(idAttribute);
//         return new ResourceId(attributeUnion.name, getNamespace(attributeUnion.prefix));
//     }
// 
//     private String getNamespace(String prefix) {
//         Matcher matcher = Pattern.compile("@([\\w]+):id").matcher(prefix);
//         if (matcher.find()) {
//             return matcher.group(1);
//         }
//         return null;
//     }
// 
//     private class IdAttributeUnion {
// 
//         String prefix;
//         String name;
// 
//         IdAttributeUnion(String idAttribute) {
//             String[] split = idAttribute.split("\\/");
//             prefix = split[0];
//             name = split[1];
//         }
//     }
// }
// 
// Path: src/main/java/com/morcinek/android/codegenerator/extractor/string/ResourceTypeExtractor.java
// public class ResourceTypeExtractor implements StringExtractor<ResourceType> {
// 
//     @Override
//     public ResourceType extractFromString(String typeName) {
//         int lastDot = typeName.lastIndexOf(".");
//         if (lastDot == -1) {
//             return new ResourceType(typeName);
//         } else {
//             return new ResourceType(typeName.substring(lastDot + 1), typeName.substring(0, lastDot));
//         }
//     }
// }
// 
// Path: src/main/java/com/morcinek/android/codegenerator/extractor/model/Resource.java
// public class Resource {
// 
//     private ResourceId resourceId;
// 
//     private ResourceType resourceType;
// 
//     public Resource(ResourceId resourceId, ResourceType resourceType) {
//         this.resourceId = resourceId;
//         this.resourceType = resourceType;
//     }
// 
//     public ResourceId getResourceId() {
//         return resourceId;
//     }
// 
//     public ResourceType getResourceType() {
//         return resourceType;
//     }
// }
// 
// Path: src/test/java/com/morcinek/android/codegenerator/util/InputStreamProvider.java
// public class InputStreamProvider {
// 
//     public InputStream getStreamFromResource(String name) {
//         return getClass().getResourceAsStream("/" + name);
//     }
// }


// Path: src/test/java/com/morcinek/android/codegenerator/extractor/XMLResourceExtractorTest.java
import com.morcinek.android.codegenerator.extractor.string.ResourceIdExtractor;
import com.morcinek.android.codegenerator.extractor.string.ResourceTypeExtractor;
import com.morcinek.android.codegenerator.extractor.model.Resource;
import com.morcinek.android.codegenerator.util.InputStreamProvider;
import org.fest.assertions.Assertions;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;

    @Test
    public void specificTest() throws Exception {
        // given
        InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/specific.xml");

        // when
        List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

        // then
        Assertions.assertThat(resources).isNotNull().hasSize(1);
        assertResource(resources.get(0), "ViewPager", "android.support.v4.view", "pager", null);
    }

    @Test
    public void locationsMapTest() throws Exception {
        // given
        InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/locations_map.xml");

        // when
        List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

        // then
        Assertions.assertThat(resources).isNotNull().hasSize(4);
        assertResource(resources.get(0), "ActionBar", "com.morcinek.budget.ui.widgets", "actionbar", null);
        assertResource(resources.get(1), "RelativeLayout", null, "map_frame_layout", null);
        assertResource(resources.get(2), "FrameLayout", null, "map_container", null);
        assertResource(resources.get(3), "ImageButton", null, "home_button", null);
    }

    private void assertResource(Resource resource, String className, String packageName, String idName, String idNamespace) {
        Assertions.assertThat(resource.getResourceType().getClassName()).isEqualTo(className);