当前位置: 首页>>代码示例>>Java>>正文


Java Koan类代码示例

本文整理汇总了Java中com.sandwich.koan.Koan的典型用法代码示例。如果您正苦于以下问题:Java Koan类的具体用法?Java Koan怎么用?Java Koan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Koan类属于com.sandwich.koan包,在下文中一共展示了Koan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: switchStatementSwitchValues

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void switchStatementSwitchValues() {
    // Try different (primitive) types for 'c'
    // Which types do compile?
    // Does boxing work?
    char c = 'a';
    String result = "Basic ";
    switch (c) {
        case 'a':
            result += "One";
            break;
        default:
            result += "Nothing";
    }
    assertEquals(result, "Basic One");
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:17,代码来源:AboutConditionals.java

示例2: forLoopContinueLabel

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void forLoopContinueLabel() {
    int count = 0;
    outerLabel:
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            count++;
            if (count > 2) {
                continue outerLabel;
            }
        }
        count += 10;
    }
    // What does continue with a label mean?
    // What gets executed? Where does the program flow continue?
    assertEquals(count, 8);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:18,代码来源:AboutLoops.java

示例3: customObjectSerializationWithTransientFields

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void customObjectSerializationWithTransientFields() throws FileNotFoundException, IOException, ClassNotFoundException {
    // Note that this kind of access of fields is not good OO practice.
    // But let's focus on serialization here :)
    Car car = new Car();
    car.engine = new Engine("diesel");
    File file = new File("SerializeFile");
    file.deleteOnExit();
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
    os.writeObject(car);
    os.close();

    ObjectInputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream("SerializeFile"));
        Car deserializedCar = (Car) is.readObject();
        assertEquals(deserializedCar.engine.type, __);
    } finally {
        closeStream(is);
    }
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:22,代码来源:AboutSerialization.java

示例4: customSerializationWithUnserializableFields

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void customSerializationWithUnserializableFields() throws FileNotFoundException, IOException {
    Boat boat = new Boat();
    boat.engine = new Engine("diesel");
    File file = new File("SerializeFile");
    file.deleteOnExit();
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
    String marker = "Start ";
    try {
        os.writeObject(boat);
    } catch (NotSerializableException e) {
        marker += "Exception";
    }
    os.close();
    assertEquals(marker, __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:17,代码来源:AboutSerialization.java

示例5: serializeWithInheritanceWhenParentNotSerializable

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void serializeWithInheritanceWhenParentNotSerializable() throws FileNotFoundException, IOException, ClassNotFoundException {
    MilitaryPlane p = new MilitaryPlane("F22");

    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("SerializeFile"));
    os.writeObject(p);
    os.close();

    ObjectInputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream("SerializeFile"));
        MilitaryPlane otherPlane = (MilitaryPlane) is.readObject();
        // Does this surprise you?
        assertEquals(otherPlane.name, __);

        // Think about how serialization creates objects...
        // It does not use constructors! But if a parent object is not serializable
        // it actually uses constructors and if the fields are not in a serializable class...
        // unexpected things - like this - may happen
    } finally {
        closeStream(is);
    }
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:24,代码来源:AboutSerialization.java

示例6: betterFileWritingAndReading

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void betterFileWritingAndReading() throws IOException {
    File file = new File("file.txt");
    file.deleteOnExit();
    FileWriter fw = new FileWriter(file);
    PrintWriter pw = new PrintWriter(fw);
    pw.println("First line");
    pw.println("Second line");
    pw.close();

    FileReader fr = new FileReader(file);
    BufferedReader br = null;
    try {
        br = new BufferedReader(fr);
        assertEquals(br.readLine(), __); // first line
        assertEquals(br.readLine(), __); // second line
        assertEquals(br.readLine(), __); // what now?
    } finally {
        // anytime you open access to a file, you should close it or you may
        // lock it from other processes (ie frustrate people)
        closeStream(br);
    }
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:24,代码来源:AboutFileIO.java

示例7: lookMaNoCloseWithMultipleResources

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void lookMaNoCloseWithMultipleResources() throws IOException {
    String str = "first line"
            + System.lineSeparator()
            + "second line";
    InputStream is = new ByteArrayInputStream(str.getBytes());
    String line;
    //multiple resources in the same try declaration
    try (BufferedReader br =
                 new BufferedReader(
                         new FileReader("I do not exist!"));
         BufferedReader brFromString =
                 new BufferedReader(
                         new InputStreamReader(is))
    ) {
        line = br.readLine();
        line += brFromString.readLine();
    } catch (IOException e) {
        line = "error";
    }
    assertEquals(line, __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:23,代码来源:AboutTryWithResources.java

示例8: lookMaNoClose

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void lookMaNoClose() {
    String str = "first line"
            + System.lineSeparator()
            + "second line";
    InputStream is = new ByteArrayInputStream(str.getBytes());
    String line;
    /* BufferedReader implementing @see java.lang.AutoCloseable interface */
    try (BufferedReader br =
                 new BufferedReader(
                         new InputStreamReader(is))) {
        line = br.readLine();
        //br guaranteed to be closed
    } catch (IOException e) {
        line = "error";
    }
    assertEquals(line, __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:19,代码来源:AboutTryWithResources.java

示例9: switchStatementFallThrough

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void switchStatementFallThrough() {
    int i = 1;
    String result = "Basic ";
    switch (i) {
        case 1:
            result += "One";
        case 2:
            result += "Two";
        default:
            result += "Nothing";
    }
    assertEquals(result, "Basic OneTwoNothing");
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:15,代码来源:AboutConditionals.java

示例10: stringIsEmpty

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void stringIsEmpty() {
    assertEquals("".isEmpty(), true);
    assertEquals("one".isEmpty(), false);
    assertEquals(new String().isEmpty(), true);
    assertEquals(new String("").isEmpty(), true);
    assertEquals(new String("one").isEmpty(), false);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:9,代码来源:AboutStrings.java

示例11: returnInFinallyBlock

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void returnInFinallyBlock() {
    StringBuilder whatHappened = new StringBuilder();
    // Which value will be returned here?
    assertEquals(returnStatementsEverywhere(whatHappened), "from finally");
    assertEquals(whatHappened.toString(), "try, catch, finally");
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:8,代码来源:AboutExceptions.java

示例12: stringTrim

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void stringTrim() {
    assertEquals("".trim(), "");
    assertEquals("one".trim(), "one");
    assertEquals(" one more time".trim(), "one more time");
    assertEquals(" one more time         ".trim(), "one more time");
    assertEquals(" and again\t".trim(), "and again");
    assertEquals("\t\t\twhat about now?\t".trim(), "what about now?");
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:10,代码来源:AboutStrings.java

示例13: base64Decoding

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void base64Decoding() {
    // Decode the Base64 encodedText
    // This uses the basic Base64 decoding scheme but there are corresponding
    // getMimeDecoder and getUrlDecoder methods available if you require a
    // different format/Base64 Alphabet
    byte[] decodedBytes = Base64.getDecoder().decode(__);
    try {
        String decodedText = new String(decodedBytes, "utf-8");
        assertEquals(plainText, decodedText);
    } catch (UnsupportedEncodingException ex) {}
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:13,代码来源:AboutBase64.java

示例14: mapReduce

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void mapReduce() {
    OptionalDouble averageLengthOptional = places.stream()
            .mapToInt(String::length)
            .average();
    double averageLength = Math.round(averageLengthOptional.getAsDouble());
    assertEquals(averageLength, __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:9,代码来源:AboutStreams.java

示例15: parallelMapReduce

import com.sandwich.koan.Koan; //导入依赖的package包/类
@Koan
public void parallelMapReduce() {
    int lengthSum = places.parallelStream()
            .mapToInt(String::length)
            .sum();
    assertEquals(lengthSum, __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:8,代码来源:AboutStreams.java


注:本文中的com.sandwich.koan.Koan类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。