Skip to content

Commit

Permalink
Fix bsearch
Browse files Browse the repository at this point in the history
Add test for values
  • Loading branch information
n1hility committed Dec 8, 2010
1 parent 45ae91a commit c1d759c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
14 changes: 11 additions & 3 deletions src/main/java/org/jboss/jandex/AnnotationInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -73,12 +74,19 @@ public AnnotationTarget target() {

/**
* Returns a value that corresponds with the specified parameter name.
* If the parameter was not specified by this instance then null is
* returned. Note that this also applies to a defaulted parameter,
* which is not recorded in the target class.
*
* @param name the parameter name
* @return the value of the specified parameter
* @return the value of the specified parameter, or null if not provided
*/
public AnnotationValue value(String name) {
int result = Arrays.binarySearch(values, name);
public AnnotationValue value(final String name) {
int result = Arrays.binarySearch(values, name, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
return ((AnnotationValue)o1).name().compareTo(name);
}
});
return result >= 0 ? values[result] : null;
}

Expand Down
107 changes: 107 additions & 0 deletions src/test/java/org/jboss/jandex/test/BasicTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.jandex.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexWriter;
import org.jboss.jandex.Indexer;
import org.junit.Test;

public class BasicTestCase {
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String name();
int[] ints();
Class<?> klass();
NestedAnnotation nested();
ElementType[] enums();
NestedAnnotation[] nestedArray();
}

public @interface NestedAnnotation {
float value();
}

@TestAnnotation(name = "Test", ints = { 1, 2, 3, 4, 5 }, klass = Void.class, nested = @NestedAnnotation(1.34f), nestedArray = {
@NestedAnnotation(3.14f), @NestedAnnotation(2.27f) }, enums = { ElementType.TYPE, ElementType.PACKAGE })
public class DummyClass {
}

@Test
public void testIndexer() throws IOException {
Indexer indexer = new Indexer();
InputStream stream = getClass().getClassLoader().getResourceAsStream(DummyClass.class.getName().replace('.', '/') + ".class");
indexer.index(stream);
Index index = indexer.complete();
AnnotationInstance instance = index.getAnnotationTargets(DotName.createSimple(TestAnnotation.class.getName())).get(0);

verifyDummy(index);
}

@Test
public void testWriteRead() throws IOException {
Indexer indexer = new Indexer();
InputStream stream = getClass().getClassLoader().getResourceAsStream(DummyClass.class.getName().replace('.', '/') + ".class");
indexer.index(stream);
Index index = indexer.complete();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
new IndexWriter(baos).write(index);

index = new IndexReader(new ByteArrayInputStream(baos.toByteArray())).read();

verifyDummy(index);
}

private void verifyDummy(Index index) {
AnnotationInstance instance = index.getAnnotationTargets(DotName.createSimple(TestAnnotation.class.getName())).get(0);

// Verify values
assertEquals("Test", instance.value("name").asString());
assertTrue(Arrays.equals(new int[] {1,2,3,4,5}, instance.value("ints").asIntArray()));
assertEquals(Void.class.getName(), instance.value("klass").asClass().name().toString());
assertTrue(1.34f == instance.value("nested").asNested().value().asFloat());
assertTrue(3.14f == instance.value("nestedArray").asNestedArray()[0].value().asFloat());
assertTrue(2.27f == instance.value("nestedArray").asNestedArray()[1].value().asFloat());
assertEquals(ElementType.TYPE.name(), instance.value("enums").asEnumArray()[0]);
assertEquals(ElementType.PACKAGE.name(), instance.value("enums").asEnumArray()[1]);

// Verify target
assertEquals(DummyClass.class.getName(), instance.target().toString());
}

}

0 comments on commit c1d759c

Please sign in to comment.